-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwebAdminStation.py
More file actions
242 lines (211 loc) · 9.61 KB
/
webAdminStation.py
File metadata and controls
242 lines (211 loc) · 9.61 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
import datetime
from teams import TeamMember
import cherrypy
import random
import sqlite3
import json
from accounts import Accounts, Role
from webBase import WebBase, Cookie
from cryptography.fernet import Fernet
from teams import TeamMemberType
class WebAdminStation(WebBase):
def checkPermissions(self, source="/admin"):
super().checkPermissions(Role.ADMIN, source)
# Admin
@cherrypy.expose
def index(self, error=""):
self.checkPermissions()
with self.dbConnect() as dbConnection:
forgotDates = []
for date in self.engine.reports.getForgottenDates(dbConnection):
forgotDates.append(date.isoformat())
teamList = self.engine.teams.getActiveTeamList(dbConnection)
lastBulkUpdateName = None
(lastBulkUpdateDate, barcode) = self.engine.logEvents.getLastEvent(dbConnection,
"Bulk Add")
if barcode:
(_, lastBulkUpdateName) = self.engine.members.getName(
dbConnection, barcode)
grace_period = self.engine.config.get(dbConnection, 'grace_period')
return self.template('admin.mako', forgotDates=forgotDates,
lastBulkUpdateDate=lastBulkUpdateDate,
lastBulkUpdateName=lastBulkUpdateName,
teamList=teamList, error=error,
grace_period=grace_period,
username=Cookie('username').get(''))
@cherrypy.expose
def emptyBuilding(self):
with self.dbConnect() as dbConnection:
self.engine.visits.emptyBuilding(dbConnection, "")
self.engine.accounts.removeKeyholder(dbConnection)
return "Building Empty"
@cherrypy.expose
def setGracePeriod(self, grace):
self.checkPermissions()
with self.dbConnect() as dbConnection:
self.engine.config.update(dbConnection, "grace_period", grace)
self.engine.logEvents.addEvent(
dbConnection, "Grace changed", self.getBarcode("/admin"))
return self.index()
@cherrypy.expose
def bulkAddMembers(self, csvfile):
self.checkPermissions()
with self.dbConnect() as dbConnection:
error = self.engine.members.bulkAdd(dbConnection, csvfile)
self.engine.logEvents.addEvent(
dbConnection, "Bulk Add", self.getBarcode("/admin"))
return self.index(error)
@cherrypy.expose
def fixData(self, date):
self.checkPermissions()
with self.dbConnect() as dbConnection:
data = self.engine.reports.getData(dbConnection, date)
return self.template('fixData.mako', date=date, data=data)
@cherrypy.expose
def oops(self):
super().checkPermissions(Role.KEYHOLDER, "/")
with self.dbConnect() as dbConnection:
self.engine.visits.oopsForgot(dbConnection)
return self.index('Oops is fixed. :-)')
@cherrypy.expose
def updatePresent(self, checked_out):
super().checkPermissions(Role.KEYHOLDER, "/")
with self.dbConnect() as dbConnection:
self.engine.visits.oopsForgot(dbConnection)
return self.index('Oops is fixed. :-)')
@cherrypy.expose
def fixed(self, output):
self.checkPermissions()
with self.dbConnect() as dbConnection:
self.engine.visits.fix(dbConnection, output)
return self.index()
@cherrypy.expose
def teams(self, error=""):
self.checkPermissions()
with self.dbConnect() as dbConnection:
activeTeams = self.engine.teams.getActiveTeamList(dbConnection)
inactiveTeams = self.engine.teams.getInactiveTeamList(dbConnection)
activeCoaches = self.engine.accounts.getMembersWithRole(
dbConnection, Role.COACH)
coaches = self.engine.teams.getCoachesList(
dbConnection, activeTeams)
todayDate = datetime.date.today().isoformat()
return self.template('adminTeams.mako', error=error,
todayDate=todayDate, username=Cookie(
'username').get(''),
activeTeams=activeTeams, inactiveTeams=inactiveTeams,
activeCoaches=activeCoaches, coaches=coaches)
@cherrypy.expose
def addTeam(self, programName, programNumber, teamName, startDate, coach1, coach2):
self.checkPermissions()
if not teamName:
teamName = "TBD:" + programName + programNumber
seasonStart = self.dateFromString(startDate)
with self.dbConnect() as connection:
error = self.engine.teams.createTeam(
connection, programName, programNumber, teamName, seasonStart)
if not error:
with self.dbConnect() as connection:
teamInfo = self.engine.teams.getTeamFromProgramInfo(
connection, programName, programNumber)
self.engine.teams.addMember(
connection, teamInfo.teamId, coach1, TeamMemberType.coach)
self.engine.teams.addMember(
connection, teamInfo.teamId, coach2, TeamMemberType.coach)
return self.teams(error)
@cherrypy.expose
def users(self, error=""):
self.checkPermissions()
with self.dbConnect() as dbConnection:
users = self.engine.accounts.getUsers(dbConnection)
nonUsers = self.engine.accounts.getNonAccounts(dbConnection)
return self.template('users.mako', error=error, username=Cookie('username').get(''), users=users, nonAccounts=nonUsers)
@cherrypy.expose
def addUser(self, user, barcode, keyholder=0, admin=0, certifier=0, coach=0, steward=0):
error = ""
self.checkPermissions()
if user == "":
error = "Username must not be blank"
return self.users(error)
with self.dbConnect() as dbConnection:
chars = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789'
tempPassword = ''.join(random.SystemRandom().choice(chars)
for _ in range(12))
role = Role()
role.setAdmin(admin)
role.setKeyholder(keyholder)
role.setShopCertifier(certifier)
role.setCoach(coach)
role.setShopSteward(steward)
try:
self.engine.accounts.addUser(
dbConnection, user, tempPassword, barcode, role)
email = self.engine.accounts.forgotPassword(dbConnection, user)
self.engine.logEvents.addEvent(dbConnection,
"Forgot password request", f"{email} for {user}")
except sqlite3.IntegrityError:
error = "Username already in use"
return self.users(error)
@cherrypy.expose
def deleteUser(self, barcode):
self.checkPermissions()
with self.dbConnect() as dbConnection:
self.engine.accounts.removeUser(dbConnection, barcode)
raise cherrypy.HTTPRedirect("/admin/users")
@cherrypy.expose
def deactivateTeam(self, teamId):
self.checkPermissions()
with self.dbConnect() as dbConnection:
self.engine.teams.deactivateTeam(dbConnection, teamId)
raise cherrypy.HTTPRedirect("/admin/teams")
@cherrypy.expose
def activateTeam(self, teamId):
self.checkPermissions()
with self.dbConnect() as dbConnection:
self.engine.teams.activateTeam(dbConnection, teamId)
raise cherrypy.HTTPRedirect("/admin/teams")
@cherrypy.expose
def deleteTeam(self, teamId):
self.checkPermissions()
with self.dbConnect() as dbConnection:
self.engine.teams.deleteTeam(dbConnection, teamId)
raise cherrypy.HTTPRedirect("/admin/teams")
@cherrypy.expose
def editTeam(self, programName, programNumber, startDate, teamId):
self.checkPermissions()
seasonStart = self.dateFromString(startDate)
with self.dbConnect() as dbConnection:
self.engine.teams.editTeam(
dbConnection, programName, programNumber, seasonStart, teamId)
raise cherrypy.HTTPRedirect("/admin/teams")
@cherrypy.expose
def changeAccess(self, barcode, admin=False, keyholder=False, certifier=False, coach=False, steward=False):
self.checkPermissions()
newRole = Role()
newRole.setAdmin(admin)
newRole.setKeyholder(keyholder)
newRole.setShopCertifier(certifier)
newRole.setCoach(coach)
newRole.setShopSteward(steward)
with self.dbConnect() as dbConnection:
self.engine.accounts.changeRole(dbConnection, barcode, newRole)
raise cherrypy.HTTPRedirect("/admin/users")
@cherrypy.expose
def getKeyholderJSON(self):
jsonData = ''
with self.dbConnect() as dbConnection:
keyholders = self.engine.accounts.getKeyholders(dbConnection)
for keyholder in keyholders:
keyholder['devices'] = []
devices = self.engine.devices.getList(
dbConnection, keyholder['barcode'])
for device in devices:
if device.mac:
keyholder['devices'].append(
{'name': device.name, 'mac': device.mac})
jsonData = json.dumps(keyholders)
# encrypt now
with open(self.engine.dataPath + 'checkmein.key', 'rb') as key_file:
key = key_file.read()
f = Fernet(key)
return f.encrypt(jsonData.encode('utf-8'))