-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_database.py
More file actions
177 lines (140 loc) · 6.63 KB
/
seed_database.py
File metadata and controls
177 lines (140 loc) · 6.63 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
"""Script to seed database."""
import os
from random import choice
from datetime import datetime
import pytz
from data.exercises import seed_experts, seed_exercise_details
from constants import PACIFIC_TIMEZONE_CITY
from flask_sqlalchemy import SQLAlchemy
from model import User, Exercise, Prompt
import crud
import model
import server
import werkzeug.security
# Remove the existing database called "health"
os.system("dropdb health")
# Create an empty database called "health"
os.system("createdb health")
model.connect_to_db(server.app)
model.db.create_all()
# Create test users
for n in range(10):
email = f"user{n}@test.com" # Voila! A unique email!
password = "test"
first_name = f"Fname{n}"
last_name = f"Lname{n}"
pen_name = f"Mysterious{n}"
# Seed some users who are only Experts, some who are only Consumers, and some who are both
if n in range(0, 4): # Users 0, 1, 2, 3
is_expert = True
is_consumer = False
elif n in range(4, 7): # Users 4, 5, 6
is_expert = False
is_consumer = True
else: # Users 7, 8, 9
is_expert = True
is_consumer = True
# Hash password for security
hashpw = werkzeug.security.generate_password_hash(password)
# Create user
user = crud.create_user(email=email,
password=hashpw,
first_name=first_name,
last_name=last_name,
is_expert=is_expert,
is_consumer=is_consumer,
pen_name=pen_name)
# Add new record. `db.session` stores planned modifications to database
model.db.session.add(user)
# Commit transaction
model.db.session.commit()
# Prepare list of respondents who make test responses to exercises
respondents = User.query.filter(User.is_consumer == True).all()
# Create renowned expert users and author their exercises.
# Prompts can have four intended ways to respond
prompt_types = ["short answer",
"long answer",
"multiple choice - choose one",
"multiple choice - choose multiple"]
# `val` is a dictionary with key "exercises" and value being a dictionary
for name, val in seed_experts.items():
split_name = name.split()
# e.g., Brené Brown's `email_local_part` == BrenéBrown
email_local_part = "".join(split_name)
email = email_local_part + "@test.com"
password = "test"
first_name = split_name[0]
last_name = split_name[1]
pen_name = name
is_expert = True
is_consumer = True
# Hash password for security
hashpw = werkzeug.security.generate_password_hash(password)
# Create user who is an expert
user = crud.create_user(email=email,
password=hashpw,
first_name=first_name,
last_name=last_name,
is_expert=is_expert,
is_consumer=is_consumer,
pen_name=pen_name)
model.db.session.add(user)
# Create exercises, authored by this user
# `val["exercises"]` is a list of exercise titles
for exercise_title in val["exercises"]:
title = exercise_title
# `seed_exercise_details[exercise_title]` is a dictionary containing
# keys of "description", "frequency", "time_limit_per_sitting",
# and "prompts"
description = seed_exercise_details[exercise_title]["description"]
frequency = int(seed_exercise_details[exercise_title]["frequency"])
time_limit_per_sitting = int(seed_exercise_details[exercise_title]["time_limit_per_sitting"])
author = User.query.filter(User.pen_name == name).first()
exercise = crud.create_exercise(title=title,
description=description,
frequency=frequency,
time_limit_per_sitting=time_limit_per_sitting,
author=author)
model.db.session.add(exercise)
# Create prompts for every seed exercise
prompt_type = "long answer"
# `seed_exercise_details[exercise.title]` is a dictionary containing
# keys of "description", "frequency", "time_limit_per_sitting",
# and "prompts".
# `seed_exercise_details[exercise.title]["prompts"]` is a list of
# strings that are prompts.
for prompt_str in seed_exercise_details[exercise.title]["prompts"]:
prompt_content = prompt_str
prompt = crud.create_prompt(prompt_content=prompt_content,
prompt_type=prompt_type,
exercise=exercise)
model.db.session.add(prompt)
# Create 2 test responses for each prompt of each exercise
pacific_time = pytz.timezone(PACIFIC_TIMEZONE_CITY)
time_completed_exercise = datetime.now(pacific_time)
response_content = "Response"
# Randomly choose a respondent
user1 = choice(respondents)
response1 = crud.create_response(response_content=response_content,
prompt=prompt,
user=user1,
time_completed_exercise=time_completed_exercise)
# Allow for the same user to have multiple responses to the same
# prompt because a user can do an exercise on multiple occasions
user2 = choice(respondents)
response2 = crud.create_response(response_content=response_content,
prompt=prompt,
user=user2,
time_completed_exercise=time_completed_exercise)
model.db.session.add_all([response1, response2])
# In this way of seeding responses for each prompt, it is possible
# for a user to respond to a subset of and not all prompts within
# an exercise. A user randomly selected for one prompt may not
# get selected again for the next prompt of the same exercise,
# which aligns with how the app allows users to make
# submissions in which not all prompts have a response and which
# aligns with reality of how users want to use the app, based on
# user research.
# Committing a transaction takes a relatively long time so better to commit
# once outside loop
model.db.session.commit()