-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_uploader.py
46 lines (36 loc) · 1.05 KB
/
data_uploader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import csv
import redis
import redisco
import urlparse
from models import *
import settings
url = 'REDIS_URL_HERE'
url = settings.redis_url
redis_url = urlparse.urlparse(url)
redisco.connection_setup(host=redis_url.hostname, port=redis_url.port, db=0, password=redis_url.password)
def import_data(filename=None):
filename = filename or 'meetups.csv'
with open(filename, 'rb') as csvfile:
freader = csv.reader(csvfile)
for row in freader:
print ', '.join(row)
try:
add_meetup(row)
except Exception as e:
import pdb; pdb.set_trace()
print 'Skipped row'
def add_meetup(row):
m = Meetup(city=row[0], name=row[1], website=row[2])
try:
m.logo = row[3]
except:
print 'No logo found'
m_id = m.save()
print 'Saved new Meetup {} {} {}'.format(row[0], row[1], m_id)
def flush_database():
meetups = Meetup.objects.all()
for m in meetups:
m.delete()
if __name__ == '__main__':
flush_database()
import_data()