-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopulate_db.py
More file actions
414 lines (379 loc) · 18.2 KB
/
Copy pathpopulate_db.py
File metadata and controls
414 lines (379 loc) · 18.2 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'art_echo.settings')
import django
django.setup()
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.files import File
from artecho.models import UserProfile
from artecho.models import Category, Image, User
from art_echo import settings
from django.template.defaultfilters import slugify
from PIL import Image as img
import io
import sys
import glob
def populate():
c = Category.objects.get_or_create(name = "none", slug = "none")[0]
c.save()
###creates a base root for all other trees to build off of
try:
Image.objects.get(name = "base_root")
except(Image.DoesNotExist):
base_image = img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'base_root.png'))
i = Image.objects.get_or_create(name = "base_root", isAI = False, file = convert_to_file(base_image, "base_root.png"), parent = None,
category = c, poster = None, description = "The base root")[0]
i.save()
categories = ["movies", "TV shows", "nature", "Art", "Sports", "building"]
users = [
{"username": 'Gary17',
"email": 'gary17@gmail.com',
"password": "password1"},
{"username": 'JoeJ',
"email": 'Joe89@gmail.com',
"password": "password7"},
{"username": 'ogrant22',
"email": 'ogrant22@gmail.com',
"password": "webApp7"},
{"username": 'Angel',
"email": 'angelina24@gmail.com',
"password": "October74"},
]
###adds all the users and categories to the database
for c in categories:
u = add_category(c)
print("added " + u.name)
for user_data in users:
u, slug= add_user(user_data["username"],user_data["email"],user_data["password"])
base_images = [
{"name" : "Darth Vader",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'dart_vader.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "movies"),
"desc" : "Image of the star wars villian",
"AI": True
},
{"name" : "Landscape",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'landscape.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "JoeJ"),
"category" : Category.objects.get(name = "nature"),
"desc" : "A painting of a landscape",
"AI": True
},
{"name" : "Metropolis",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'metropolis.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "none"),
"desc" : "An image of a futuristic city",
"AI": True
},
{"name" : "Grafiti",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'grafiti.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "Art"),
"desc" : "An image of a futuristic city",
"AI": True
},
{"name" : "Swimmer",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'swimmer.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "Sports"),
"desc" : "An image of a swimmer underwater",
"AI": True
},
{"name" : "Droplet",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'water_drop.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Gary17"),
"category" : Category.objects.get(name = "nature"),
"desc" : "An image of a water drop splashing",
"AI": False
},
{"name" : "Flower Woman",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'flower_woman.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "nature"),
"desc" : "A woman surrounded by flowers",
"AI": False
},
{"name" : "Green wall",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'green-wall.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "nature"),
"desc" : "A green spiky wall",
"AI": True
},
{"name" : "Greek Gods",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'greek_gods.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "building"),
"desc" : "A green spiky wall",
"AI": True
},
{"name" : "White arch",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'white_arch.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "building"),
"desc" : "A sunny white arch",
"AI": True
},
{"name" : "Flowers",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'flowers.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "JoeJ"),
"category" : Category.objects.get(name = "nature"),
"desc" : "A field of flowers",
"AI": True
},
{"name" : "Boat building",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'boat_building.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "JoeJ"),
"category" : Category.objects.get(name = "building"),
"desc" : "A boat and a building",
"AI": True
},
{"name" : "Spaceman",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'spaceman.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "none"),
"desc" : "An astronaut",
"AI": True
},
{"name" : "Stripes",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'stripes.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Gary17"),
"category" : Category.objects.get(name = "none"),
"desc" : "some guys with some stripes",
"AI": True
},
{"name" : "Modern House",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'modern.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "building"),
"desc" : "A modern house",
"AI": True
},
{"name" : "Blue Sky",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'blue_sky.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "JoeJ"),
"category" : Category.objects.get(name = "none"),
"desc" : "A blue sky and sea",
"AI": True
},
{"name" : "Fortress",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'mountain_fortress.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "building"),
"desc" : "A fortress in a mountain",
"AI": True
},
{"name" : "Pillars",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'pillars.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "building"),
"desc" : "A bunch of pillars",
"AI": True
},
{"name" : "Alleyways",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'alleys.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Gary17"),
"category" : Category.objects.get(name = "building"),
"desc" : "A town with long alleyways",
"AI": True
},
{"name" : "Witch",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'witch.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "Art"),
"desc" : "A witch with her face distorted",
"AI": True
},
{"name" : "Face",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'face.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "Art"),
"desc" : "A colourful face",
"AI": True
},
{"name" : "Robot artist",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'robot.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "Gary17"),
"category" : Category.objects.get(name = "Art"),
"desc" : "A robot painting a picture",
"AI": True
},
{"name" : "Jellyfish",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'jellyfish.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "nature"),
"desc" : "A few jellyfish",
"AI": True
},
{"name" : "Swings",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'swings.png')),
"parent" : Image.objects.get(name = "base_root"),
"poster" : User.objects.get(username = "JoeJ"),
"category" : Category.objects.get(name = "none"),
"desc" : "A number of swings",
"AI": True
},
]
### adds 3 images with the "base_root" as their parent to the database
for image_data in base_images:
i = add_image(image_data["file"], image_data["name"], image_data["parent"], image_data["poster"],
image_data["desc"], image_data["category"],False)
print("added " + image_data["name"])
child_images = [
{"name" : "Coffee splash",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'coffee_splash.png')),
"parent" : Image.objects.get(slug = "gary17-droplet"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "none"),
"desc" : "A painting of a coffee cup spilling",
"AI": False
},
{"name" : "Realistic splash",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'realistic_splash.png')),
"parent" : Image.objects.get(slug = "gary17-droplet"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "none"),
"desc" : "A photo of a splash of water",
"AI": False
},
{"name" : "Watercolour splash",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'watercolour_splash.png')),
"parent" : Image.objects.get(slug = "gary17-droplet"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "none"),
"desc" : "A watercolour painting of a splash of water",
"AI": False
},
{"name" : "Darth Vader vs Jedi",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'vader_vs_jedi.png')),
"parent" : Image.objects.get(slug = "ogrant22-darth-vader"),
"poster" : User.objects.get(username = "JoeJ"),
"category" : Category.objects.get(name = "movies"),
"desc" : "Vader playing handball against a jedi",
"AI": True
},
{"name" : "Darth Vader Baseball",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'vader_baseball.png')),
"parent" : Image.objects.get(slug = "ogrant22-darth-vader"),
"poster" : User.objects.get(username = "Gary17"),
"category" : Category.objects.get(name = "movies"),
"desc" : "Vader playing baseball",
"AI": True
},
{"name" : "Darth Vader Fighting",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'vader_fight.png')),
"parent" : Image.objects.get(slug = "ogrant22-darth-vader"),
"poster" : User.objects.get(username = "Gary17"),
"category" : Category.objects.get(name = "movies"),
"desc" : "Vader fighting on a starship",
"AI": True
},
{"name" : "Darth Vader Water Fountain",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'vader_water_fountain.png')),
"parent" : Image.objects.get(slug = "ogrant22-darth-vader"),
"poster" : User.objects.get(username = "Gary17"),
"category" : Category.objects.get(name = "movies"),
"desc" : "Vader drinking from a water fountain",
"AI": True
},
{"name" : "Darth Vader (Clay)",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'vader_clay.png')),
"parent" : Image.objects.get(slug = "ogrant22-darth-vader"),
"poster" : User.objects.get(username = "Angel"),
"category" : Category.objects.get(name = "movies"),
"desc" : "Vader sculpted from clay",
"AI": True
},
{"name" : "Spaceman in a jungle",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'jungle-spaceman.png')),
"parent" : Image.objects.get(slug = "ogrant22-spaceman"),
"poster" : User.objects.get(username = "ogrant22"),
"category" : Category.objects.get(name = "none"),
"desc" : "An astronaut in a jungle",
"AI": True
},
]
### adds images which have parents to the database
for image_data in child_images:
i = add_image(image_data["file"], image_data["name"], image_data["parent"], image_data["poster"],
image_data["desc"], image_data["category"], image_data["AI"])
print("added " + image_data["name"])
tier3images = [
{"name" : "Darth Vader Baseball (Oil)",
"file" : img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'vader_baseball_oil.png')),
"parent" : Image.objects.get(slug = "gary17-darth-vader-baseball"),
"poster" : User.objects.get(username = "JoeJ"),
"category" : Category.objects.get(name = "movies"),
"desc" : "Vader fighting on a starship",
"AI": True
},
]
for image_data in tier3images:
i = add_image(image_data["file"], image_data["name"], image_data["parent"], image_data["poster"],
image_data["desc"], image_data["category"], image_data["AI"])
print("added " + image_data["name"])
def add_user(username, email, password):
user, created = User.objects.get_or_create(username=username, defaults={
"email": email,
})
if created:
user.set_password(password)
user.save()
slug = slugify(username)
user_profile = UserProfile.objects.create(user=user, slug=slug)
blank = img.open(os.path.join(os.path.join(settings.STATIC_DIR, 'images'), 'blank_pfp.png'))
user_profile.profilePicture = convert_to_file(blank, username + "_pfp.png")
user_profile.save()
return user, slug
def add_image(file, name, parent, poster, desc, category, AI):
i = Image.objects.get_or_create(name = name, file = convert_to_file(file, name + ".png"), poster = poster, category = category,
parent = parent, description = desc, isAI = AI)[0]
i.save()
return i
def add_category(name):
c = Category.objects.get_or_create(name = name)[0]
c.save()
return c
def convert_to_file(image, name): ##function to convert PIL image to an uploadable file with a name
output = io.BytesIO()
image.save(output, format='PNG', quality=85)
output.seek(0)
return InMemoryUploadedFile(output, 'ImageField', name, 'image/png', sys.getsizeof(output), None)
if __name__ == '__main__':
print('Starting ArtEcho population script...')
#clears previous contents of database
Image.objects.all().delete()
User.objects.all().delete()
Category.objects.all().delete()
imageFiles = glob.glob(os.path.join(settings.MEDIA_DIR, 'images') + "/*.png")
for file in imageFiles:
os.remove(file)
pfpFiles = glob.glob(os.path.join(settings.MEDIA_DIR, 'profilePics') + "/*.png")
for file in pfpFiles:
os.remove(file)
populate()
print("population complete")