-
Notifications
You must be signed in to change notification settings - Fork 12
/
openrecords.py
401 lines (343 loc) · 13.2 KB
/
openrecords.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
`.` `.` ...` .... ...`
`;'', :##+ +##`#####, .######` +##.+####'
`'''', :#### +#########, .########` '#########,
;'';` `###' +###,``;###``###. `;##+ '###,``####
.'''` `### +##' ###,:##: ###`'##+ .###
.''' ###.+##, :##'###+++++###.'##, `###
.:::,,, ###.+##` ,##+###########.'##: `###
.### ###.+##. :##+###,.......`'##: `###
`###` `### +##; +##:;##, ...`'##: `###
+##+ ###+ +###. .###.`###. .### '##: `###
`#########` +#########' ;########. '##: `###
`#######` +##,#####' ,######. '##: `###
`,:,` +##, .:,` `.::. ```` ```
+##,
+##,
+##,
```
.........` ............` .,::,. ,,;:,` .......... `........ .,::,.
`'''''''''''. ''''''''''''. .''''''''` ,''''''''. ;'''''''''': .''''''''''. `'''''''',
`''''''''''''. ''''''''''''. ,''''''''''. ;''''''''''` ;'''''''''''' .''''''''''': `'''''''''',
`''':::::;'''' '''';;;;;;;;. .'''':.,:''''` :'''',.,:''''. ;'''::::,''''. .''';;;;''''', ;'''.``.''''`
`'''. ,'''` '''' ''''` `''': `'''' .'''' ;''' .''': .'''` ,''''` ''', `'''.
`'''. .'''. '''' ,'''. :''' :'''` ,'''. ;''' '''; .'''` ,''', ''', :,:.
`'''. .'''` '''' ;''' ```` '''; .''', ;''' ''', .'''` '''; '''';.
`'''. `'''' ''''''''''': '''; `''', ''': ;''' ,'''` .'''` ;''' .''''''':.
`'''''''''''' ''''''''''': ''': `'''. '''; ;'''''''''''. .'''` :''' .'''''''''.
`'''''''''''. '''''''''''; ''': `'''. '''; ;'''''''''': .'''` ;''' ,''''''''.
`''':::::''''. '''' '''' `...`''': ''': ;'''::::;'''' .'''` ;''' .,'''''
`'''. '''' '''' :'''` :''' '''' .'''. ;''' ,'''` .'''` ''':,::: ;'''`
`'''. ,''' '''; .''': '''' ,'''. ;'''` ;''' ''', .'''` :'''.:''' .'''`
`'''. .''' '''' '''', :'''. ''''. :''': ;''' ''', .'''. .;'''' .''': ;'''
`'''. `'''` '''''''''''', `''''';;''''' .''''';'''''' ;''' ''', .''''''''''''. ''''':::'''',
`'''. '''. ''''''''''''. .'''''''''' .''''''''''` ;''' ;''; .'''''''''''. '''''''''';
`'''. ''': '''''''''''', ,'''''', `:''''''. '''' ,''' .'''''''';. ,'''''''.
"""
import os
from dotenv import load_dotenv
basedir = os.path.abspath(os.path.dirname(__file__))
dotenv_path = os.path.join(basedir, '.env')
load_dotenv(dotenv_path)
from datetime import datetime
from urllib.parse import unquote, urljoin
import sys
import traceback
import click
from flask import url_for, render_template, request as flask_request
from flask.cli import main
from flask_login import login_user
from flask_migrate import Migrate, upgrade
from werkzeug.middleware.profiler import ProfilerMiddleware
from app import create_app, db
from app.constants import OPENRECORDS_DL_EMAIL
from app.jobs import _update_request_statuses
from app.lib.date_utils import process_due_date, local_to_utc
from app.lib.email_utils import send_email
from app.models import (
Agencies,
AgencyUsers,
CustomRequestForms,
Determinations,
Emails,
EnvelopeTemplates,
Envelopes,
Events,
LetterTemplates,
Letters,
Reasons,
Requests,
Responses,
Roles,
UserRequests,
Users,
)
from app.report.utils import generate_request_closing_user_report, generate_monthly_metrics_report
from app.request.utils import generate_guid
from app.response.utils import add_extension
from app.search.utils import recreate
from app.user.utils import make_user_admin
if os.getenv('FLASK_ENV') != 'production':
import pytest
app = create_app(os.getenv("FLASK_CONFIG") or "default")
migrate = Migrate(app, db)
COV = None
if os.environ.get("FLASK_COVERAGE"):
import coverage
COV = coverage.coverage(
branch=True, include="app/*", config_file=os.path.join(os.curdir, ".coveragerc")
)
COV.start()
@app.shell_context_processor
def make_shell_context():
return dict(
app=app,
db=db,
Users=Users,
Agencies=Agencies,
Determinations=Determinations,
Requests=Requests,
Responses=Responses,
Events=Events,
Reasons=Reasons,
Roles=Roles,
UserRequests=UserRequests,
AgencyUsers=AgencyUsers,
Emails=Emails,
Letters=Letters,
LetterTemplates=LetterTemplates,
Envelopes=Envelopes,
EnvelopeTemplates=EnvelopeTemplates,
CustomRequestForms=CustomRequestForms,
)
@app.cli.command()
@click.option("--first_name", prompt="First Name")
@click.option("--middle_initial", default="", prompt="Middle Initial")
@click.option("--last_name", prompt="Last Name")
@click.option("--email", prompt="Email Address")
@click.option("--agency_ein", prompt="Agency EIN (e.g. 0002)")
@click.option(
"--is_admin", default=False, prompt="Should user be made an agency administrator?"
)
@click.option(
"--is_active", default=False, prompt="Should user be activated immediately?"
)
def add_user(
first_name: str,
last_name: str,
email: str,
agency_ein: str,
middle_initial: str = None,
is_admin: bool = False,
is_active: bool = False,
):
"""
Add an agency user into the database.
"""
if not first_name:
raise click.UsageError("First name is required")
if not last_name:
raise click.UsageError("Last name is required")
if not email:
raise click.UsageError("Email Address is required")
if not agency_ein:
raise click.UsageError("Agency EIN is required")
user = Users(
guid=generate_guid(),
first_name=first_name,
middle_initial=middle_initial,
last_name=last_name,
email=email,
email_validated=False,
is_nyc_employee=True,
is_anonymous_requester=False,
)
db.session.add(user)
agency_user = AgencyUsers(
user_guid=user.guid,
agency_ein=agency_ein,
is_agency_active=is_active,
is_agency_admin=is_admin,
is_primary_agency=True,
)
db.session.add(agency_user)
db.session.commit()
if is_admin:
redis_key = "{current_user_guid}-{update_user_guid}-{agency_ein}-{timestamp}".format(
current_user_guid="openrecords_support",
update_user_guid=user.guid,
agency_ein=agency_ein,
timestamp=datetime.now(),
)
make_user_admin.apply_async(
args=(user.guid, "openrecords_support", agency_ein), task_id=redis_key
)
print(user)
@app.cli.command()
@click.option("--agency_ein", prompt="Agency EIN (e.g. 0002)")
@click.option("--date_from", prompt="Date From (e.g. 2000-01-01")
@click.option("--date_to", prompt="Date To (e.g. 2000-02-01)")
@click.option("--emails", prompt="Emails (e.g. [email protected],[email protected])")
def generate_closing_report(agency_ein: str, date_from: str, date_to: str, emails: str):
"""Generate request closing report.
"""
email_list = emails.split(',')
generate_request_closing_user_report(agency_ein, date_from, date_to, email_list)
@app.cli.command()
@click.option("--agency_ein", prompt="Agency EIN (e.g. 0002)")
@click.option("--date_from", prompt="Date From (e.g. 2000-01-01")
@click.option("--date_to", prompt="Date To (e.g. 2000-02-01)")
@click.option("--emails", prompt="Emails (e.g. [email protected],[email protected])")
def generate_monthly_report(agency_ein: str, date_from: str, date_to: str, emails: str):
"""Generate monthly metrics report.
CLI command to generate monthly metrics report.
Purposely leaving a full date range option instead of a monthly limit in order to provide more granularity for devs.
"""
email_list = emails.split(',')
generate_monthly_metrics_report(agency_ein, date_from, date_to, email_list)
@app.cli.command()
def es_recreate():
"""
Recreate elasticsearch index and request docs.
"""
recreate()
@app.cli.command()
@click.option("--agency_ein", prompt="Agency EIN (e.g. 0056)")
@click.option("--agency_name", prompt="Agency Name (e.g. New York City Police Department (NYPD))")
@click.option("--user_guid", prompt="User GUID")
@click.option("--extension_date", prompt="Extension Date (e.g. 01/01/2022)")
@click.option("--extension_reason", prompt="Extension Reason")
def extend_requests(agency_ein: str, agency_name: str, user_guid: str, extension_date: str, extension_reason: str):
# Create request context
ctx = app.test_request_context()
ctx.push()
app.preprocess_request()
# Select user to perform the extensions
user = Users.query.filter_by(guid=user_guid).first()
login_user(user)
# Extend overdue requests
overdue_requests = Requests.query.filter_by(agency_ein=agency_ein, status='Overdue').order_by(Requests.id).all()
for request in overdue_requests:
try:
date = datetime.strptime(extension_date, '%m/%d/%Y')
new_due_date = process_due_date(local_to_utc(date, 'America/New_York'))
email_template = render_template('email_templates/email_response_extension_cli.html',
default_content=True,
content=None,
request=request,
request_id=request.id,
agency_name=agency_name,
new_due_date=new_due_date.strftime("%A, %B %-d, %Y"),
reason=extension_reason,
page=urljoin(app.config['BASE_URL'], url_for('request.view', request_id=request.id)))
add_extension(request.id,
'-1',
extension_reason,
extension_date,
'America/New_York',
email_template,
'emails',
'')
print(request.id, 'extended')
except:
print(request.id, 'failed')
@app.cli.command()
def update_request_statuses():
try:
_update_request_statuses()
except Exception:
db.session.rollback()
send_email(
subject="Update Request Statuses Failure",
to=[OPENRECORDS_DL_EMAIL],
email_content=traceback.format_exc().replace("\n", "<br/>").replace(" ", " ")
)
@app.cli.command
def routes():
"""
Generate a list of HTTP routes for the application.
"""
output = []
for rule in app.url_map.iter_rules():
options = {}
for arg in rule.arguments:
if arg == "year":
options[arg] = "{}".format(datetime.now().year)
continue
options[arg] = "[{}]".format(arg)
methods = ",".join(rule.methods)
url = url_for(rule.endpoint, **options)
if str(datetime.now().year) in url:
url = url.replace(str(datetime.now().year), "[year]")
line = unquote("{:50} {:20} {}".format(rule.endpoint, methods, url))
output.append(line)
for line in sorted(output):
print(line)
@app.cli.command()
@click.option("--test-name", help="Specify tests (file, class, or specific test)")
@click.option(
"--coverage/--no-coverage", "use_coverage", default=False, help="Run tests under code coverage."
)
@click.option("--verbose", is_flag=True, default=False, help="Py.Test verbose mode")
def test(test_name: str = None, use_coverage: bool = False, verbose: bool = False):
"""Run the unit tests."""
if use_coverage and not os.environ.get("FLASK_COVERAGE"):
os.environ["FLASK_COVERAGE"] = "1"
os.execvp(sys.executable, [sys.executable] + sys.argv)
command = []
if verbose:
command.append("-v")
if test_name:
command.append("tests/{test_name}".format(test_name=test_name))
else:
command.append("tests/")
pytest.main(command)
if COV:
COV.stop()
COV.save()
print("Coverage Summary:")
COV.report()
COV.html_report()
COV.xml_report()
@app.cli.command()
@click.option(
"--length",
default=25,
help="Number of functions to include in the profiler report.",
)
@click.option(
"--profile-dir", default=None, help="Directory where profiler data files are saved."
)
def profile(length, profile_dir):
"""
Start the application under the code profiler.
"""
app.wsgi_app = ProfilerMiddleware(
app.wsgi_app, restrictions=[length], profile_dir=profile_dir
)
app.run()
@app.cli.command()
def deploy():
"""
Run deployment tasks.
"""
# migrate database to latest revision
upgrade()
# pre-populate
list(
map(
lambda x: x.populate(),
(
Roles,
Agencies,
Reasons,
Users,
LetterTemplates,
EnvelopeTemplates,
CustomRequestForms,
),
)
)
recreate()
if __name__ == "__main__":
main()