This repository was archived by the owner on May 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdmin.py
More file actions
executable file
·239 lines (202 loc) · 8.9 KB
/
Admin.py
File metadata and controls
executable file
·239 lines (202 loc) · 8.9 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
#!/usr/bin/env python
"""Landing page for the CDR Administrative system.
"""
import datetime
import functools
import cdrcgi
class Page(cdrcgi.HTMLPage):
@functools.cached_property
def logged_in_count(self):
"""How many users are currently logged in on this CDR tier?"""
query = self.control.Query("session", "COUNT(DISTINCT usr)")
query.where("ended IS NULL")
query.where("name <> 'guest'")
return query.execute(self.control.cursor).fetchone()[0]
@functools.cached_property
def saved_today(self):
"""Count of unique documents saved today."""
today = str(datetime.date.today())
query = self.control.Query("audit_trail", "COUNT(DISTINCT document)")
query.where(f"dt >= '{today}'")
return f"{query.execute(self.control.cursor).fetchone()[0]:,}"
@functools.cached_property
def saved_this_week(self):
"""Count of unique documents saved in the past week."""
last_week = str(datetime.date.today() - datetime.timedelta(7))
query = self.control.Query("audit_trail", "COUNT(DISTINCT document)")
query.where(f"dt >= '{last_week}'")
return f"{query.execute(self.control.cursor).fetchone()[0]:,}"
@functools.cached_property
def locked_count(self):
"""How many documents are currently checked out?"""
query = self.control.Query("checkout", "COUNT(DISTINCT id)")
query.where("dt_in IS NULL")
return f"{query.execute(self.control.cursor).fetchone()[0]:,}"
@functools.cached_property
def active_count(self):
"""How many non-blocked documents are in the system?"""
query = self.control.Query("active_doc", "COUNT(*)")
return f"{query.execute(self.control.cursor).fetchone()[0]:,}"
@functools.cached_property
def last_pub_date(self):
"""When was the last successful weekly publishing job run?"""
query = self.control.Query("pub_proc", "MAX(started)")
query.where("pub_subset = 'Push_Documents_To_Cancer.Gov_Export'")
query.where("status = 'Success'")
date = query.execute(self.control.cursor).fetchone()[0]
return date.strftime("%Y-%m-%d")
@functools.cached_property
def summary_count(self):
"""How many Summary documents are published?"""
return self.doctype_count("Summary")
@functools.cached_property
def drug_summary_count(self):
"""How many Summary documents are published?"""
return self.doctype_count("DrugInformationSummary")
@functools.cached_property
def glossary_term_count(self):
"""How many Summary documents are published?"""
return self.doctype_count("GlossaryTermName")
@functools.cached_property
def media_count(self):
"""How many Summary documents are published?"""
return self.doctype_count("Media")
def doctype_count(self, name):
"""How many documents of a given document type are published?
"""
query = self.control.Query("document d", "COUNT(*)")
query.join("pub_proc_cg c", "c.id = d.id")
query.join("doc_type t", "t.id = d.doc_type")
query.where(query.Condition("t.name", name))
return f"{query.execute(self.control.cursor).fetchone()[0]:,}"
@functools.cached_property
def main(self):
"""Dispense with the sidebar menu."""
card_classes = "usa-card desktop:grid-col-6 usa-card-header-first"
checked_out_label = "Documents checked out for editing"
drug_summary_count = self.drug_summary_count
activity_counts = (
self.B.LI(f"Logged-in users: {self.logged_in_count}"),
self.B.LI(f"Documents saved today: {self.saved_today}"),
self.B.LI(f"Documents saved this week: {self.saved_this_week}"),
self.B.LI(f"{checked_out_label}: {self.locked_count}"),
self.B.LI(f"Active CDR documents: {self.active_count}"),
)
publishing_counts = (
self.B.LI(f"Last full publishing job: {self.last_pub_date}"),
self.B.LI(f"Cancer Information Summaries: {self.summary_count}"),
self.B.LI(f"Drug Information Summaries: {drug_summary_count}"),
self.B.LI(f"Glossary Terms: {self.glossary_term_count}"),
self.B.LI(f"Media: {self.media_count}"),
)
return self.B.E(
"main",
self.B.DIV(
self.B.UL(
self.B.LI(
self.B.DIV(
self.B.DIV(
self.B.H2(
"Current Activity",
self.B.CLASS("usa-card__heading")
),
self.B.CLASS("usa-card__header")
),
self.B.DIV(
self.B.DIV(
self.B.IMG(
src="/images/office-cropped.jpg",
alt="office"
),
self.B.CLASS("usa-card__img")
),
self.B.CLASS("usa-card__media")
),
self.B.DIV(
self.B.UL(*activity_counts),
self.B.CLASS("usa-card__body")
),
self.B.CLASS("usa-card__container")
),
self.B.CLASS(card_classes)
),
self.B.LI(
self.B.DIV(
self.B.DIV(
self.B.H2(
"Publishing",
self.B.CLASS("usa-card__heading")
),
self.B.CLASS("usa-card__header")
),
self.B.DIV(
self.B.DIV(
self.B.IMG(
src="/images/printing-cropped.jpg",
alt="printing"
),
self.B.CLASS("usa-card__img")
),
self.B.CLASS("usa-card__media")
),
self.B.DIV(
self.B.UL(*publishing_counts),
self.B.CLASS("usa-card__body")
),
self.B.CLASS("usa-card__container")
),
self.B.CLASS(card_classes)
),
self.B.CLASS("usa-card-group")
),
self.B.CLASS("grid-container")
),
self.B.CLASS("usa-section")
)
class Control(cdrcgi.Controller):
"""Logic for dynamic construction of the top-level CDR admin menu."""
SUBTITLE = "Main Menu"
BOARD_MANAGERS = "Board Manager Menu Users"
CIAT_OCCM = "CIAT/OCCM Staff Menu Users"
DEV_SA = "Developer/SysAdmin Menu Users"
MENUS = (
(BOARD_MANAGERS, "BoardManagers.py", "OCC Board Managers"),
(CIAT_OCCM, "CiatCipsStaff.py", "CIAT/OCC Staff"),
(DEV_SA, "DevSA.py", "Developers/System Administrators"),
)
def populate_form(self, page):
"""Add the menu links available for this user.
If the user only has one menu option, go there directly.
"""
if len(page.user_menus) < 2:
labels = [menu["label"] for menu in page.menus]
menu = page.user_menus[0]
label = menu["label"]
positions = [labels.index(label)] if label in labels else []
script = page.find_menu_link(menu, positions)
opts = dict(show_news=True)
if self.logged_out:
opts["logged_out"] = True
self.navigate_to(script, self.session, **opts)
@functools.cached_property
def form_page(self):
"""Custom layout for the landing page."""
return Page(self.title, control=self, session=self.session, buttons=[])
@property
def buttons(self):
"""This page needs no buttons."""
return []
@functools.cached_property
def show_news(self):
"""Show news items when the user hits the home page."""
return True
@property
def user(self):
"""Access to which groups the current user belongs to."""
if not hasattr(self, "_user"):
opts = dict(name=self.session.user_name)
self._user = self.session.User(self.session, **opts)
return self._user
if __name__ == "__main__":
"""Don't run the script if loaded as a module."""
Control().run()