forked from TimmyTomTom/communityshare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·374 lines (324 loc) · 10.1 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·374 lines (324 loc) · 10.1 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
import logging
import random
from sqlalchemy.exc import IntegrityError, InvalidRequestError
from community_share.models.search import Label, Search
from community_share.models.user import User, UserReview
from community_share.models.secret import Secret
from community_share.models.survey import Question, SuggestedAnswer
from community_share.models.conversation import Conversation, Message
from community_share.models.institution import InstitutionAssociation, Institution
from community_share.models.share import Share, Event, EventReminder
from community_share import store, Base, config, setup_data
logger = logging.getLogger(__name__)
skills = [
'accounting',
'biology',
'cooking',
'dinosaurs',
'farming',
'fish',
'horses',
'meteorology',
'paleontology',
'rockets',
]
first_names = [
'Ai',
'Antonia',
'Antonio',
'Bob',
'Cary',
'Charlotte',
'Emma',
'Esmerelda',
'Ethelmay',
'Ethelred',
'Fang',
'Fatima',
'Joe',
'John',
'Mary',
'Mina',
'Mohammed',
'Pedro',
'Pierre',
'Robert',
'Rufus',
'Sam',
'Susan',
]
last_names = [
'Campbell',
'Carter',
'Davis',
'Evans',
'Garcia',
'Gonzalez',
'Johnson',
'Jones',
'King',
'Lopez',
'Perez',
'Robinson',
'Rodriguez',
'Smith',
'Walker',
'White',
'Williams',
'Wright',
'Wu',
]
labels = {
'GradeLevels': [
'K-5', '6-8', '9-12', 'College', 'Adult',
],
'SubjectAreas': [
'STEM', 'Arts',
'Science', 'Technology', 'Engineering', 'Math',
'Visual Arts', 'Digital Media', 'Film & Photography', 'Literature',
'Performing Arts',
'Social Studies', 'English/Language Arts', 'Foreign Languages', 'PE/Health/Sports',
'Mathematics', 'Goverment',
],
'LevelOfEngagement': [
'Guest', 'Speaker', 'Field Trip Host', 'Student Competition Judge',
'Individual/Group Mentor', 'Share Curriculum Ideas', 'Curriculuum Development',
'Career Day Participant', 'Collaborator on a Class Project'
],
}
label_probabilities = [
(labels['GradeLevels'], 0.5),
(labels['SubjectAreas'], 0.2),
(labels['LevelOfEngagement'], 0.2),
(skills, 0.3),
]
profile_picture_filenames = [
'aardvark.jpg',
'ant.jpg',
'dolphin.jpg',
'gila_monster.jpg',
'honey_badger.jpg',
'llama.jpg',
'octopus.jpg',
'pig.jpg',
'shiba.jpg',
'sloth.jpg',
]
school_infos = [
('School Number 42', 'School'),
('School Number 101', 'School')
]
educator_roles = [
'Classroom Teacher',
'Curriculum Coordinator',
]
partner_roles = [
'CEO',
'Engineer',
'Intern',
'Sales Representative',
]
company_infos = [
('Big University', 'University'),
('Acme', 'Company'),
('City Opera', 'Nonprofit'),
('Widget Factory', 'Company'),
('Secret Government Research Lab', 'Government'),
('Myself', 'Freelancer'),
('Copper Mine', 'Company'),
]
specialties = [
'classical portraiture',
'interpretative dance',
'laying fiber-optic cable',
'mass-surveillance',
'postmodern composition',
'robotic molluscs',
]
hobbies = [
'underwater skiing.',
'playing competitive hide-and-seek.',
'playing minecraft',
'french cooking',
'watching reality TV',
'training for marathons',
'juggling'
]
def gen_email(first_name, last_name):
return '{0}.{1}@example.com'.format(first_name, last_name)
def gen_labels():
return [
label
for label_set, probability in label_probabilities
for label in label_set
if random.random() < probability
]
def make_institutions(infos):
return [
Institution(name=name,institution_type=institution_type)
for name, institution_type in infos
]
companies = make_institutions(company_infos)
schools = make_institutions(school_infos)
def generate_expert_bio():
specialty = random.choice(specialties)
hobby = random.choice(hobbies)
return "I specialize in the area of {0}. My main hobby is {1}.".format(specialty, hobby)
def generate_educator_bio():
bio = "My main hobby is {0}".format(random.choice(hobbies))
return bio
def make_random_location():
latitude = 32.223303 + (random.random()-0.5)+0.1
longitude = -110.970905 + (random.random()-0.5)+0.1
return (latitude, longitude)
user_names_used = set()
def gen_new_name(existing_users, first_names, last_names):
max_combinations = len(first_names) * len(last_names)
# stop trying after a while to keep from infinite iteration
for attempt_count in range(max_combinations):
name_pair = (random.choice(first_names), random.choice(last_names))
if name_pair not in existing_users:
return name_pair
return None, None
def gen_random_institution(institutions, roles):
return InstitutionAssociation(
institution=random.choice(institutions),
role=random.choice(roles)
)
def make_random_user():
# Make the user
first_name, last_name = gen_new_name(user_names_used, first_names, last_names)
if first_name is None:
return
user_names_used.add((first_name, last_name))
password = Secret.make_key(20)
password_hash = User.pwd_context.encrypt(password)
if random.randint(0, 1):
searcher_role = 'educator'
searching_for_role = 'partner'
bio = generate_educator_bio()
associations = [
gen_random_institution(schools, educator_roles)
]
else:
searcher_role = 'partner'
searching_for_role = 'educator'
bio = generate_expert_bio()
associations = [
gen_random_institution(companies, partner_roles)
for _ in range(random.randint(1, 2))
]
new_user = User(
name='{0} {1}'.format(first_name, last_name),
email=gen_email(first_name, last_name),
password_hash=password_hash,
picture_filename=random.choice(profile_picture_filenames),
bio=bio,
institution_associations=associations,
is_administrator=False,
email_confirmed=True
)
store.session.add(new_user)
store.session.commit()
# Make the search
latitude, longitude = make_random_location()
search = Search(
searcher_user_id=new_user.id,
searcher_role=searcher_role,
searching_for_role=searching_for_role,
latitude=latitude,
longitude=longitude,
)
search.labels = Label.name_list_to_object_list(gen_labels())
store.session.add(search)
store.session.commit()
if search.searcher_role == 'educator':
new_user.educator_profile_search = search
else:
new_user.community_partner_profile_search = search
store.session.add(new_user)
store.session.commit()
def make_labels():
all_labels = labels['GradeLevels'] + labels['SubjectAreas'] + labels['LevelOfEngagement']
for name in all_labels:
label = Label(name=name)
store.session.add(label)
try:
store.session.commit()
except (IntegrityError, InvalidRequestError):
store.session.rollback()
def make_admin_user(name, email, password):
password_hash = User.pwd_context.encrypt(password)
new_user = User(name=name, email=email, password_hash=password_hash,
is_administrator=True, email_confirmed=True)
store.session.add(new_user)
try:
store.session.commit()
except (IntegrityError, InvalidRequestError):
store.session.rollback()
new_user = None
return new_user
def update_questions(questions):
new_hashs = set([question.make_hash() for question in questions])
existing_questions = store.session.query(Question).filter(Question.active == True).all()
old_hashs = set([question.make_hash() for question in existing_questions])
hashs_to_add = new_hashs - old_hashs
hashs_to_delete = old_hashs - new_hashs
for question in existing_questions:
if question.make_hash() in hashs_to_delete:
question.active = False
store.session.add(question)
for question in questions:
if question.make_hash() in hashs_to_add:
store.session.add(question)
def init_db():
Base.metadata.reflect(store.engine)
logger.info('Dropping all tables.')
Base.metadata.drop_all(store.engine);
logger.info('Creating all tables.')
Base.metadata.create_all(store.engine);
def update_db():
Base.metadata.reflect(store.engine)
logger.info('Creating all tables.')
Base.metadata.create_all(store.engine, checkfirst=True);
logger.info('Created all tables.')
def get_creator():
admin_emails = config.ADMIN_EMAIL_ADDRESSES.split(',')
admin_emails = [x.strip() for x in admin_emails]
admin_user = None
for admin_email in admin_emails:
admin_user = store.session.query(User).filter(
User.active==True, User.email==admin_email).first()
if admin_user is not None:
break
return admin_user
def setup(n_random_users=100):
logger.info('Starting setup script.')
init_db()
logger.info('Making labels.')
make_labels()
from community_share.models.secret import Secret
logger.info('Making Admin Users')
make_admin_user('admin@example.com', 'admin@example.com', 'admin')
admin_emails = config.ADMIN_EMAIL_ADDRESSES.split(',')
admin_emails = [x.strip() for x in admin_emails]
logger.info('admin_emails is {0}'.format(admin_emails))
for email in admin_emails:
make_admin_user(email, email, Secret.make_key(20))
logger.info('Making {0} random users'.format(n_random_users))
for i in range(n_random_users):
make_random_user()
creator = get_creator()
logger.info('Creator of questions is {}'.format(creator.email))
questions = setup_data.get_questions(creator)
update_questions(questions)
store.session.commit()
creator = get_creator()
questions = setup_data.get_questions(creator)
update_questions(questions)
store.session.commit()
if __name__ == '__main__':
logger.info('Loading settings from environment')
config.load_from_file()
logger.info('Finished loading settings')
setup(n_random_users=40)