-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·204 lines (187 loc) · 6.78 KB
/
manage.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
#!/usr/bin/env python
# Standard Library
import datetime
import getpass
import os
import random
# Third Party
import flask_migrate
import flask_script
import flask_security
import pyotp
import pytz
import sqlalchemy.sql
# Local
import main
MANAGER = flask_script.Manager(main.app)
MANAGER.add_command("db", flask_migrate.MigrateCommand)
@MANAGER.command
def dropdb():
"""Drop all database tables."""
print(f"Database: {main.models.db.engine.url}")
main.models.db.drop_all()
print("All tables dropped")
@MANAGER.command
def create_admin():
"""Create new admin user."""
main.models.db.create_all()
print('creating "admin" role')
admin_role = main.user_datastore.find_or_create_role("admin")
print("creating [email protected]")
now = datetime.datetime.utcnow()
password = flask_security.utils.encrypt_password(getpass.getpass())
admin = main.user_datastore.create_user(
email="[email protected]", password=password, active=True, confirmed_at=now
)
main.user_datastore.add_role_to_user(admin, admin_role)
main.models.db.session.commit()
@MANAGER.command
def create_confirmed_user():
main.models.db.create_all()
print("creating [email protected]")
password = flask_security.utils.encrypt_password(getpass.getpass())
main.user_datastore.create_user(
email="[email protected]",
password=password,
active=True,
confirmed_at=datetime.datetime.utcnow(),
secret=pyotp.random_base32(),
method_id=1,
)
main.models.db.session.commit()
@MANAGER.command
def create_defaults():
"""Create default data."""
main.models.db.create_all()
sections = {
"Words of Affirmation": [
"Compliment looks",
"Leave a love note",
"Be encouraging about a project",
"Compliment something they say",
],
"Acts of Service": [
"Cook dinner",
"Do some vacuuming",
"Dust",
"Do next day's chore(s)",
],
"Receiving Gifts": ["Give flowers", "Give candy", "Give a homemade card"],
"Quality Time": [
"Talk aout something God is doing",
"Go on a walk",
"Discuss the news",
"Game night",
],
"Physical Touch": [
"Back rub with oil",
"Back rub before bed",
"Slow dance hug",
"Passionate kiss goodbye",
"Multiple kisses on body",
"Kiss on the neck",
"Multiple kisses on face,",
],
}
print("Creating statuses")
for name in ("Proposed", "Rejected", "Approved"):
status = main.models.Status.query.filter_by(name=name).first()
if not status:
status = main.models.Status(name=name)
main.models.db.session.add(status)
print("Creating method")
for name in ("Five Love Languages",):
method = main.models.Method.query.filter_by(name=name).first()
if not method:
method = main.models.Method(name=name, status=status)
main.models.db.session.add(method)
print("Creating sections")
for name in sections:
section = main.models.Section.query.filter_by(name=name).first()
if not section:
section = main.models.Section(name=name)
if section not in method.sections:
method.sections.append(section)
for label in sections[name]:
assoc = main.models.SectionActions(status=status)
assoc.action = main.models.Action(label=label, status=status)
section.actions.append(assoc)
main.models.db.session.add(section)
main.models.db.session.commit()
@MANAGER.command
def create_dummy_data(max_id):
engine = main.db.engine
names = set(open("/usr/share/dict/words").read().lower().splitlines())
names = list(names)
utc_now = datetime.datetime.utcnow()
email_users = []
phone_users = []
query = main.db.create_scoped_session().query
min_id = query(sqlalchemy.sql.func.max(main.models.User.id)).first()[0]
schedules = []
actions = []
for user_id in range((min_id or 0) + 1, (min_id or 0) + int(max_id) + 1):
name = "{}{}".format(random.choice(names), user_id)
active = bool(random.randint(0, 5))
check_hour = random.randint(0, 23)
check_minute = random.randint(0, 59)
timezone = random.choice(pytz.common_timezones)
tz = pytz.timezone(timezone)
check_dt = datetime.datetime.now().replace(hour=check_hour, minute=check_minute)
utc_hour = tz.localize(check_dt).utctimetuple()[3]
days_of_week = []
weekdays = range(0, 7)
for _ in range(random.randint(2, 7)):
days_of_week.append(random.choice(weekdays))
weekdays.remove(days_of_week[-1])
for local_weekday in days_of_week:
days = (local_weekday - check_dt.weekday()) % 7
weekday_dt = check_dt + datetime.timedelta(days=days)
utc_weekday = tz.localize(weekday_dt).weekday()
schedules.append({"utc_weekday": utc_weekday, "user_id": user_id})
possible_actions = range(1, 23)
action_ids = []
for _ in range(3, random.randint(5, 10)):
action_ids.append(random.choice(possible_actions))
possible_actions.remove(action_ids[-1])
for action_id in action_ids:
actions.append({"user_id": user_id, "action_id": action_id})
user = {
"active": active,
"method_id": 1,
"id": user_id,
"check_hour": check_hour,
"check_minute": check_minute,
"timezone": timezone,
"utc_hour": utc_hour,
}
if random.randint(0, 1):
user["email"] = "{}@love-touches.org".format(name)
user["email_confirmed_at"] = utc_now
email_users.append(user)
else:
user["phone"] = "+1 {}-{}-{}".format(
random.randint(100, 999),
random.randint(100, 999),
random.randint(1000, 9999),
)
user["phone_confirmed_at"] = utc_now
phone_users.append(user)
if email_users:
engine.execute(main.models.User.__table__.insert(), email_users)
if phone_users:
engine.execute(main.models.User.__table__.insert(), phone_users)
engine.execute(main.models.Weekday.__table__.insert(), schedules)
engine.execute(main.models.users_actions.insert(), actions)
@MANAGER.shell
def _make_shell_context():
return {
"app": main.app,
"db": main.models.db,
"models": main.models,
"session": main.models.db.session,
}
PORT = int(os.getenv("PORT") or 5000)
SERVER = flask_script.Server(host="0.0.0.0", port=PORT)
MANAGER.add_command("runserver", SERVER)
MANAGER.run()