-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeclock.py
179 lines (156 loc) · 5 KB
/
timeclock.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
import os
from flask_migrate import Migrate
from app import create_app, db
from app.models import (
User,
Permission,
Event,
Pay,
Password,
ChangeLog,
Vacation,
Role,
Tag,
HealthScreenResults,
HealthScreenUsers,
)
if os.getenv("FLASK_ENV") == "development":
from faker import Faker
from app.utils import divisions, tags
app = create_app(os.getenv("FLASK_CONFIG") or "default")
migrate = Migrate(app, db)
@app.shell_context_processor
def make_shell_context():
return dict(
app=app,
db=db,
User=User,
Permission=Permission,
Event=Event,
Pay=Pay,
Password=Password,
ChangeLog=ChangeLog,
Vacation=Vacation,
Role=Role,
Tag=Tag,
HealthScreenResults=HealthScreenResults,
HealthScreenUsers=HealthScreenUsers,
)
@app.cli.command()
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover("tests")
unittest.TextTestRunner(verbosity=2).run(tests)
@app.cli.command("setup_db")
def setup_db():
"""Setup the database."""
db.create_all()
Role.insert_roles()
Tag.insert_tags()
@app.cli.command("reset_db")
def reset_db():
"""Reset the database."""
db.drop_all()
db.create_all()
Role.insert_roles()
Tag.insert_tags()
@app.cli.command("setup_roles")
def setup_roles():
"""Insert roles in the proper order."""
Role.query.delete()
user = Role(name="User", permissions=0x01, id=1)
moderator = Role(name="Moderator", permissions=0x80, id=2)
administrator = Role(name="Administrator", permissions=0xFF, id=3)
db.session.add(user)
db.session.add(moderator)
db.session.add(administrator)
db.session.commit()
@app.cli.command("create_dev_users")
def create_dev_users():
"""Create users for development."""
# Administrator
faker = Faker()
first_name = faker.first_name()
last_name = faker.last_name()
tag_id = faker.random_int(min=1, max=len(tags) - 1)
division = faker.random_elements(elements=divisions, length=1)[0][0]
administrator = User(
email="{first_initial}{last_name}@{email_domain}".format(
first_initial=first_name[0].lower(),
last_name=last_name.lower(),
email_domain=app.config["EMAIL_DOMAIN"],
),
first_name=first_name,
last_name=last_name,
password="Change4me",
division=division,
role=Role.query.filter_by(name="Administrator").first(),
tag_id=tag_id,
is_supervisor=True,
)
db.session.add(administrator)
administrator.password_list.update(administrator.password_hash)
# Supervisor
first_name = faker.first_name()
last_name = faker.last_name()
tag_id = faker.random_int(min=1, max=len(tags) - 1)
division = faker.random_elements(elements=divisions, length=1)[0][0]
supervisor = User(
email="{first_initial}{last_name}@{email_domain}".format(
first_initial=first_name[0].lower(),
last_name=last_name.lower(),
email_domain=app.config["EMAIL_DOMAIN"],
),
first_name=first_name,
last_name=last_name,
password="Change4me",
division=division,
role=Role.query.filter_by(name="User").first(),
tag_id=tag_id,
is_supervisor=True,
)
db.session.add(supervisor)
supervisor.password_list.update(supervisor.password_hash)
# Users
for i in range(10):
first_name = faker.first_name()
last_name = faker.last_name()
tag_id = faker.random_int(min=1, max=len(tags) - 1)
division = faker.random_elements(elements=divisions, length=1)[0][0]
user = User(
email="{first_initial}{last_name}@{email_domain}".format(
first_initial=first_name[0].lower(),
last_name=last_name.lower(),
email_domain=app.config["EMAIL_DOMAIN"],
),
first_name=first_name,
last_name=last_name,
password="Change4me",
division=division,
role=Role.query.filter_by(name="User").first(),
tag_id=tag_id,
is_supervisor=False,
)
db.session.add(user)
user.password_list.update(user.password_hash)
db.session.commit()
@app.cli.command("create_health_screen_users")
def create_dev_users():
"""Create health screen users for development."""
faker = Faker()
for i in range(10):
first_name = faker.first_name()
last_name = faker.last_name()
division = faker.random_elements(elements=divisions, length=1)[0][0]
user = HealthScreenUsers(
name=first_name + " " + last_name,
email="{first_initial}{last_name}@{email_domain}".format(
first_initial=first_name[0].lower(),
last_name=last_name.lower(),
email_domain=app.config["EMAIL_DOMAIN"],
),
division=division,
)
db.session.add(user)
db.session.commit()