This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
manage.py
executable file
·160 lines (129 loc) · 4.62 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
#!/usr/bin/env python3
import datetime
import functools
import random
import sys
import alembic
import names
from alembic.config import Config
from flask_debugtoolbar import DebugToolbarExtension
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from oh_queue import app, socketio
from oh_queue.models import Group, GroupAttendance, GroupAttendanceStatus, GroupStatus, db, Assignment, ConfigEntry, \
Location, Ticket, \
TicketStatus, User, \
Appointment, \
AppointmentSignup, AppointmentStatus
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
alembic_cfg = Config('migrations/alembic.ini')
def not_in_production(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
if app.config.get('ENV') == 'prod':
print('this commend should not be run in production. Aborting')
sys.exit(1)
return f(*args, **kwargs)
return wrapper
@manager.command
@not_in_production
def seed_data():
print('Seeding...')
assignments = [Assignment(name=name, course="ok", visible=True) for name in ['Hog', 'Maps', 'Ants', 'Scheme']]
locations = [Location(name=name, course="ok", visible=True, online=True, link="") for name in
['109 Morgan', '241 Cory', '247 Cory']]
questions = list(range(1, 16)) + ['Other', 'EC', 'Checkoff']
descriptions = ['', 'I\'m in the hallway', 'SyntaxError on Line 5']
students = []
for i in range(50):
real_name = names.get_full_name()
first_name, last_name = real_name.lower().split(' ')
email = '{0}{1}@{2}'.format(
random.choice([first_name, first_name[0]]),
random.choice([last_name, last_name[0]]),
random.choice(['berkeley.edu', 'gmail.com']),
)
student = User.query.filter_by(email=email).one_or_none()
if not student:
student = User(name=real_name, email=email, course="ok")
students.append(student)
db.session.add(student)
db.session.commit()
delta = datetime.timedelta(minutes=random.randrange(0, 30))
ticket = Ticket(
user=student,
status=TicketStatus.pending,
created=datetime.datetime.utcnow() - delta,
assignment=random.choice(assignments),
location=random.choice(locations),
question=random.choice(questions),
description=random.choice(descriptions),
course="ok"
)
db.session.add(ticket)
appointments = [Appointment(
start_time=datetime.datetime.now() + datetime.timedelta(hours=random.randrange(-8, 50)),
duration=datetime.timedelta(minutes=random.randrange(30, 120, 30)),
location=random.choice(locations),
capacity=5,
status=AppointmentStatus.pending,
course="ok",
helper=random.choice(students)
) for _ in range(70)]
for assignment in assignments:
db.session.add(assignment)
for location in locations:
db.session.add(location)
for appointment in appointments:
db.session.add(appointment)
db.session.commit()
signups = [AppointmentSignup(
appointment=random.choice(appointments),
user=random.choice(students),
assignment=random.choice(assignments),
question=random.choice(questions),
description=random.choice(descriptions),
course="ok",
) for _ in range(120)]
for signup in signups:
db.session.add(signup)
db.session.commit()
groups = [
Group(
group_status=GroupStatus.active,
question=random.choice(questions),
assignment=random.choice(assignments),
location=random.choice(locations),
attendees=[GroupAttendance(
user=student,
group_attendance_status=GroupAttendanceStatus.present,
course="ok"
) for student in random.sample(students, 5)],
call_url="",
doc_url="",
course="ok",
) for _ in range(120)]
for group in groups:
db.session.add(group)
db.session.commit()
@manager.command
@not_in_production
def resetdb():
print('Dropping tables...')
db.drop_all(app=app)
initdb()
@manager.command
def initdb():
print('Creating tables...')
db.create_all(app=app)
print('Stamping DB revision...')
alembic.command.stamp(alembic_cfg, "head")
@manager.command
@not_in_production
def server():
DebugToolbarExtension(app)
socketio.run(app, host=app.config.get('HOST'), port=app.config.get('PORT'))
if __name__ == '__main__':
manager.run()