This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate.py
232 lines (197 loc) · 8.21 KB
/
populate.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 -*-
"""Import models from mvp/data/xxx.csv."""
import csv
import datetime
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'artinbar.settings'
django.setup()
from mvp.models import Artist
from mvp.models import Venue
from mvp.models import User
from mvp.models import Geolocation
from mvp.models import Message
from mvp.models import Photo
from mvp.models import Tag
from mvp.models import Media
from mvp.models import Offer
from mvp.models import Performance
from mvp.models import Payment
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def createPermissions():
ct = ContentType.objects.get(app_label='mvp', model='user')
new = Permission.objects.create(codename='artist', name='Can View Artist', content_type=ct)
new.save()
new = Permission.objects.create(codename='venue', name='Can View Venue', content_type=ct)
new.save()
def importArtists():
"""Import artist from artists.csv to database."""
permission = Permission.objects.get(codename='artist')
with open('mvp/data/artists.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
nombre = line[0]
email = line[1]
description = line[2]
logo = line[3]
integrantes = line[4]
artisId = line[5]
username = line[6]
new = Artist.objects.create(username=username, email=email,
name=nombre, description=description,
logo=logo, artistNumber=integrantes,
id=artisId)
new.set_password(username)
new.user_permissions.add(permission)
new.save()
def importVenues():
"""Import venues from venues.csv to database."""
permission = Permission.objects.get(codename='venue')
with open('mvp/data/venues.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
nombre = line[0]
email = line[1]
description = line[2]
logo = line[3]
geo = line[4]
address = line[5]
capacity = line[6]
venueId = line[7]
username = line[8]
geo = geo.split('/')
geos = Geolocation.objects.create(
latitude=float(geo[0]), longitude=float(geo[1]))
new = Venue.objects.create(username=username, email=email,
name=nombre, description=description,
logo=logo, geolocation=geos,
address=address, capacity=capacity,
id=venueId)
new.set_password(username)
new.user_permissions.add(permission)
new.save()
def importMessages():
"""Import messages from messages.csv to database."""
with open('mvp/data/messages.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
timeStamp = datetime.datetime.strptime(line[0], '%Y-%m-%d %I:%M%p')
text = line[1]
sender = User.objects.get(pk=line[2])
receiver = User.objects.get(pk=line[3])
messageId = line[4]
new = Message.objects.create(timeStamp=timeStamp, text=text,
sender=sender, receiver=receiver,
id=messageId)
new.save()
def importPhotos():
"""Import photos from photos.csv to database."""
with open('mvp/data/photos.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
photoUrl = line[0]
userId = User.objects.get(pk=line[1])
photoId = line[2]
new = Photo.objects.create(url=photoUrl, user=userId, id=photoId)
new.save()
def importTags():
"""Import tags from tags.csv to database."""
with open('mvp/data/tags.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
name = line[0]
artisId = Artist.objects.get(pk=line[1])
tagId = line[2]
new = Tag.objects.create(name=name, artist=artisId, id=tagId)
new.save()
def importMedias():
"""Import Medias from medias.csv to database."""
with open('mvp/data/medias.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
url = line[0]
artisId = Artist.objects.get(pk=line[1])
mediaId = line[2]
new = Media.objects.create(url=url, artist=artisId, id=mediaId)
new.save()
def importOffers():
"""Import Offers from offers.csv to database."""
with open('mvp/data/offers.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
name = line[0]
description = line[1]
offeredAmount = line[2]
date = datetime.datetime.strptime(line[3], '%Y-%m-%d')
venue = Venue.objects.get(pk=line[4])
offerId = line[5]
new = Offer.objects.create(name=name, description=description,
offeredAmount=offeredAmount, date=date,
venue=venue, id=offerId)
new.save()
def importPerfomances():
"""Import performances from performances.csv to database."""
with open('mvp/data/performances.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
name = line[0]
description = line[1]
date = datetime.datetime.strptime(line[2], '%Y-%m-%d')
public = line[3]
artist = Artist.objects.get(pk=line[4])
venue = Venue.objects.get(pk=line[5])
performanceId = line[6]
new = Performance.objects.create(name=name,
description=description,
date=date, public=public,
artist=artist, venue=venue,
id=performanceId)
new.save()
def importPayments():
"""Import Payment from payment.csv to database."""
with open('mvp/data/payments.csv', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(spamreader.__next__())
for line in spamreader:
amount = line[0]
date = datetime.datetime.strptime(line[1], '%Y-%m-%d')
user = User.objects.get(pk=line[2])
performance = Performance.objects.get(pk=line[3])
paymentId = line[4]
new = Payment.objects.create(amount=amount,
date=date, user=user,
performance=performance,
id=paymentId)
new.save()
"""Delete all data from database"""
Geolocation.objects.all().delete()
Permission.objects.all().delete()
Artist.objects.all().delete()
Venue.objects.all().delete()
Message.objects.all().delete()
Photo.objects.all().delete()
Tag.objects.all().delete()
Media.objects.all().delete()
Offer.objects.all().delete()
Performance.objects.all().delete()
Payment.objects.all().delete()
"""Import new data"""
createPermissions()
importArtists()
importVenues()
importMessages()
importPhotos()
importTags()
importMedias()
importOffers()
importPerfomances()
importPayments()