forked from 0xdomrom/banterbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.py
executable file
·338 lines (272 loc) · 10.6 KB
/
seeder.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
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
#!/usr/bin/env python3
# Set up the environment so django doesn't throw a hissy fit.
import os
import sys
from django.contrib.auth.hashers import make_password
from django.core.wsgi import get_wsgi_application
os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
application = get_wsgi_application()
# Actual imports
from faker import Factory
from django.utils import timezone
from random import choice, randint, sample
from datetime import date, datetime, timedelta, time
import manage
from banterbox import models
from banterbox import icons
UNITS = 10
LECTURES_PER_UNIT = 5
STUDENTS = 100
COMMENTS_PER_ROOM = 10
fake = Factory.create()
euro_fakers = [Factory.create(locale) for locale in \
["de_DE", "cs_CZ", "dk_DK", "es_ES", "fr_FR",
"it_IT", "no_NO", "pl_PL", "ru_RU", "sl_SI"]]
user_password = make_password("password", salt=fake.bothify("#?#?#?#?#?#"))
def sample_with_dupes(population, num_items):
remaining = num_items
out_list = []
while remaining > 0:
to_remove = min(len(population), remaining)
remaining -= to_remove
out_list += sample(population, to_remove)
return out_list
def add_statuses():
for status in models.Statuses:
m = models.RoomStatus()
m.name = status.value
m.save()
def add_roles():
for role in models.Roles:
m = models.UserRole()
m.name = role.value
m.save()
def make_superuser():
admin = models.User()
admin.username = "admin"
admin.password = make_password("burgertown")
admin.email = 'admin@banterbox.edu.au'
admin.is_superuser = True
admin.is_staff = True
admin.save()
def make_unikey(fname, lname):
unikey_stub = fname[:1].lower() + lname[:3].lower() \
+ "?"*(4 - (len(fname) + len(lname))) + "####"
return fake.bothify(unikey_stub)
def make_users(num):
for _ in range(num):
user = models.User()
user.first_name = fake.first_name()
user.last_name = fake.last_name()
user.username = make_unikey(user.first_name, user.last_name)
user.email = fake.email()
user.password = user_password
user.save()
# Profile is now auto generator when a user is created
# profile = models.Profile()
# profile.user = user
# profile.icon = choice(icons.icons)
# profile.save()
def make_units(num):
all_users = list(models.User.objects.all())
student_role = models.UserRole.objects.get(name=models.Roles.participant.value)
tutor_role = models.UserRole.objects.get(name=models.Roles.moderator.value)
for _ in range(num):
# Generate a pretentious european professor
eu_fake = choice(euro_fakers)
lecturer = models.User()
lecturer.first_name = eu_fake.first_name()
lecturer.last_name = eu_fake.last_name()
lecturer.username = make_unikey(lecturer.first_name, lecturer.last_name)
lecturer.password = user_password
lecturer.email = eu_fake.email()
lecturer.save()
# Make the unit itself.
unit = models.Unit()
unit.name = fake.catch_phrase()
unit.code = fake.bothify("????####").upper()
unit.lecturer = lecturer
unit.icon = choice(icons.icons)
unit.save()
# Attach the lecturer to the unit
role = models.UserUnitRole()
role.user = lecturer
role.unit = unit
role.role = models.UserRole.objects.get(name=models.Roles.owner.value)
role.save()
# Select a bunch of users to be students of this unit.
num_users = randint(STUDENTS//(2*UNITS), (2*STUDENTS)//UNITS)
users = sample_with_dupes(all_users, num_users)
for i, user in enumerate(users):
enrolment = models.UserUnitEnrolment()
enrolment.unit = unit
enrolment.user = user
enrolment.save()
s_role = models.UserUnitRole()
s_role.user = user
s_role.unit = unit
s_role.role = tutor_role if i < 5 else student_role
s_role.save()
def make_schedules(lecs_per_unit):
for unit in models.Unit.objects.all():
for _ in range(lecs_per_unit):
room = models.ScheduledRoom()
room.day = randint(0, 4) # Python considers 0 to be monday and 6 to be sunday
room.unit = unit
room.start_time = time(hour=randint(0, 20), minute=15*randint(0,3))
room.end_time = (datetime.combine(date.today(), room.start_time) \
+ timedelta(minutes=15*randint(1, 12))).time()
room.save()
def make_rooms(comments_per_room):
statuses = list(models.RoomStatus.objects.all())
for cur_unit in models.Unit.objects.all():
room = models.Room()
room.name = cur_unit.code + " Lecture"
room.lecturer = cur_unit.lecturer
room.unit = cur_unit
room.status = choice(statuses)
room.private = choice([True, False])
room.password_protected = choice([True, False])
if room.password_protected:
room.password = "password"
room.save()
unit_user_enrolments = models.UserUnitEnrolment.objects.all().filter(unit=cur_unit)
unit_users = [enrolment.user for enrolment in unit_user_enrolments]
unit_user_sample = sample_with_dupes(unit_users, comments_per_room)
for i in range(comments_per_room):
comment = models.Comment()
comment.room = room
comment.user = unit_user_sample[i]
comment.content = fake.text()
comment.private = choice([True, False])
comment.save()
def add_dummy_unit():
#lecture_name = "The Prawn Hole"
lecturer = models.User()
lecturer.first_name = "Wikus"
lecturer.last_name = "van der Merwe"
lecturer.username = "wikus"
lecturer.password = make_password("popcorn")
lecturer.email = "wikus@d9.co.za"
lecturer.save()
unit = models.Unit()
unit.name = "District 9"
unit.code = "PRWN9001"
unit.lecturer = lecturer
unit.icon = "braille"
unit.save()
dud_unit = models.Unit()
dud_unit.name = 'Intro to Gang Signs'
dud_unit.code = 'WTUP8876'
dud_unit.lecturer = lecturer
dud_unit.icon = 'american-sign-language-interpreting'
dud_unit.save()
#
dud_room = models.ScheduledRoom()
dud_room.day = 2
dud_room.unit = dud_unit
dud_room.start_time = time(hour=10, minute=00, second=00)
dud_room.end_time = time(hour=12, minute=00, second=00)
dud_room.save()
for i in range(7):
room = models.ScheduledRoom()
room.day = i
room.unit = unit
room.start_time = time(hour=0, minute=0, second=0)
room.end_time = time(hour=23, minute=59, second=59)
room.save()
role = models.UserUnitRole()
role.user = lecturer
role.unit = unit
role.role = models.UserRole.objects.get(name=models.Roles.owner.value)
role.save()
enrolment = models.UserUnitEnrolment()
enrolment.unit = unit
enrolment.user = lecturer
enrolment.save()
for uname in ["anton", "dominic", "patrick", "roy", "wafik"]:
user = models.User()
user.username = uname
user.password = make_password("corn")
user.is_staff = True
user.save()
enrolment = models.UserUnitEnrolment()
enrolment.unit = unit
enrolment.user = user
enrolment.save()
def run_step(func, args, pre_string=None, fail_string=None):
if pre_string is None:
print("Running {}...".format(func.__name__), end=" ")
else:
print(pre_string, end=" ")
sys.stdout.flush()
try:
func(*args)
print("OK")
except Exception as e:
if fail_string is None:
print("Failed")
else:
print("\n" + fail_string)
with open("errors.log", "a") as logfile:
logfile.write("Error Logged at {}:\n".format(str(datetime.now())))
logfile.write(" {}\n\n".format(str(e)))
sys.stdout.flush()
def make_old_rooms():
now = datetime.now(tz=timezone.get_current_timezone())
closed = models.RoomStatus.objects.get(name='closed')
for unit in models.Unit.objects.all():
room = models.Room()
room.name = unit.code + " Lecture"
room.lecturer = unit.lecturer
room.unit = unit
room.status = closed
room.private = False
room.password_protected = False
room.history = open('fake_votes_1.txt').read()
room.save()
room.created_at = now - timedelta(days=7)
room.save()
# I know this is pleb , idgaf
room = models.Room()
room.name = unit.code + " Lecture"
room.lecturer = unit.lecturer
room.unit = unit
room.status = closed
room.private = False
room.password_protected = False
room.history = open('fake_votes_2.txt').read()
room.save()
room.created_at = now - timedelta(days=14)
room.save()
def hard_reset_db():
def remove_migrations():
for filename in os.listdir("banterbox/migrations/"):
if not os.path.isdir(filename) and filename != "__init__.py":
os.remove(filename)
#run_step(remove_migrations, [], "Removing all migrations.")
run_step(manage.passthrough, [['manage.py', 'migrate', 'banterbox', 'zero']], "Removing all migrations.")
run_step(manage.passthrough, [['manage.py', 'makemigrations']], "Making migrations...\n")
run_step(manage.passthrough, [['manage.py', 'migrate']], "Migrating...\n")
print("Database purged.")
def populate_db():
run_step(manage.passthrough, [['manage.py', 'flush']], "Flushing database...\n")
run_step(add_roles, [], "Setting Roles...", "Roles already exist.")
run_step(add_statuses, [], "Setting Statuses...", "Statuses already exist.")
run_step(make_superuser, [], "Adding super-user...", "User 'admin' already exists.")
run_step(make_users, [STUDENTS], "Adding {} users...".format(STUDENTS))
run_step(make_units, [UNITS], "Adding {} units...".format(UNITS))
run_step(make_schedules, [LECTURES_PER_UNIT], \
"Adding {} scheduled lectures per unit...".format(LECTURES_PER_UNIT))
run_step(add_dummy_unit, [], "Adding prawns...")
run_step(make_old_rooms, [], "Adding old rooms...")
print("All Done.")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Flags:")
print(" --populate : populate the database with example data.")
print(" --purge : delete the database and migrations.")
elif sys.argv[1] == "--populate":
populate_db()
elif sys.argv[1] == "--purge":
hard_reset_db()