arthexis 0.1.13__py3-none-any.whl → 0.1.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of arthexis might be problematic. Click here for more details.
- {arthexis-0.1.13.dist-info → arthexis-0.1.14.dist-info}/METADATA +222 -221
- arthexis-0.1.14.dist-info/RECORD +109 -0
- {arthexis-0.1.13.dist-info → arthexis-0.1.14.dist-info}/licenses/LICENSE +674 -674
- config/__init__.py +5 -5
- config/active_app.py +15 -15
- config/asgi.py +43 -43
- config/auth_app.py +7 -7
- config/celery.py +32 -32
- config/context_processors.py +67 -69
- config/horologia_app.py +7 -7
- config/loadenv.py +11 -11
- config/logging.py +59 -48
- config/middleware.py +25 -25
- config/offline.py +49 -49
- config/settings.py +691 -682
- config/settings_helpers.py +109 -109
- config/urls.py +171 -166
- config/wsgi.py +17 -17
- core/admin.py +3771 -2809
- core/admin_history.py +50 -50
- core/admindocs.py +151 -151
- core/apps.py +356 -272
- core/auto_upgrade.py +57 -57
- core/backends.py +265 -236
- core/changelog.py +342 -0
- core/entity.py +133 -133
- core/environment.py +61 -61
- core/fields.py +168 -168
- core/form_fields.py +75 -75
- core/github_helper.py +188 -25
- core/github_issues.py +178 -172
- core/github_repos.py +72 -0
- core/lcd_screen.py +78 -78
- core/liveupdate.py +25 -25
- core/log_paths.py +100 -100
- core/mailer.py +85 -85
- core/middleware.py +91 -91
- core/models.py +3609 -2795
- core/notifications.py +105 -105
- core/public_wifi.py +267 -227
- core/reference_utils.py +108 -108
- core/release.py +721 -368
- core/rfid_import_export.py +113 -0
- core/sigil_builder.py +149 -149
- core/sigil_context.py +20 -20
- core/sigil_resolver.py +315 -315
- core/system.py +752 -493
- core/tasks.py +408 -394
- core/temp_passwords.py +181 -181
- core/test_system_info.py +186 -139
- core/tests.py +2095 -1521
- core/tests_liveupdate.py +17 -17
- core/urls.py +11 -11
- core/user_data.py +641 -633
- core/views.py +2175 -1417
- core/widgets.py +213 -94
- core/workgroup_urls.py +17 -17
- core/workgroup_views.py +94 -94
- nodes/admin.py +1720 -1161
- nodes/apps.py +87 -85
- nodes/backends.py +160 -160
- nodes/dns.py +203 -203
- nodes/feature_checks.py +133 -133
- nodes/lcd.py +165 -165
- nodes/models.py +1737 -1597
- nodes/reports.py +411 -411
- nodes/rfid_sync.py +195 -0
- nodes/signals.py +18 -0
- nodes/tasks.py +46 -46
- nodes/tests.py +3810 -3116
- nodes/urls.py +15 -14
- nodes/utils.py +121 -105
- nodes/views.py +683 -619
- ocpp/admin.py +948 -948
- ocpp/apps.py +25 -25
- ocpp/consumers.py +1565 -1459
- ocpp/evcs.py +844 -844
- ocpp/evcs_discovery.py +158 -158
- ocpp/models.py +917 -917
- ocpp/reference_utils.py +42 -42
- ocpp/routing.py +11 -11
- ocpp/simulator.py +745 -745
- ocpp/status_display.py +26 -26
- ocpp/store.py +601 -541
- ocpp/tasks.py +31 -31
- ocpp/test_export_import.py +130 -130
- ocpp/test_rfid.py +913 -702
- ocpp/tests.py +4445 -4094
- ocpp/transactions_io.py +189 -189
- ocpp/urls.py +50 -50
- ocpp/views.py +1479 -1251
- pages/admin.py +708 -539
- pages/apps.py +10 -10
- pages/checks.py +40 -40
- pages/context_processors.py +127 -119
- pages/defaults.py +13 -13
- pages/forms.py +198 -198
- pages/middleware.py +205 -153
- pages/models.py +607 -426
- pages/tests.py +2612 -2200
- pages/urls.py +25 -25
- pages/utils.py +12 -12
- pages/views.py +1165 -1128
- arthexis-0.1.13.dist-info/RECORD +0 -105
- nodes/actions.py +0 -70
- {arthexis-0.1.13.dist-info → arthexis-0.1.14.dist-info}/WHEEL +0 -0
- {arthexis-0.1.13.dist-info → arthexis-0.1.14.dist-info}/top_level.txt +0 -0
pages/views.py
CHANGED
|
@@ -1,1128 +1,1165 @@
|
|
|
1
|
-
import base64
|
|
2
|
-
import logging
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from types import SimpleNamespace
|
|
5
|
-
import datetime
|
|
6
|
-
import calendar
|
|
7
|
-
import io
|
|
8
|
-
import shutil
|
|
9
|
-
import re
|
|
10
|
-
from html import escape
|
|
11
|
-
from urllib.parse import urlparse
|
|
12
|
-
|
|
13
|
-
from django.conf import settings
|
|
14
|
-
from django.contrib import admin
|
|
15
|
-
from django.contrib import messages
|
|
16
|
-
from django.contrib.admin.views.decorators import staff_member_required
|
|
17
|
-
from django.contrib.auth import get_user_model, login
|
|
18
|
-
from django.contrib.auth.tokens import default_token_generator
|
|
19
|
-
from django.contrib.auth.views import LoginView
|
|
20
|
-
from django import forms
|
|
21
|
-
from django.apps import apps as django_apps
|
|
22
|
-
from utils.sites import get_site
|
|
23
|
-
from django.http import Http404, HttpResponse, JsonResponse
|
|
24
|
-
from django.shortcuts import get_object_or_404, redirect, render
|
|
25
|
-
from nodes.models import Node
|
|
26
|
-
from django.template.response import TemplateResponse
|
|
27
|
-
from django.test import RequestFactory
|
|
28
|
-
from django.urls import NoReverseMatch, reverse
|
|
29
|
-
from django.utils import timezone
|
|
30
|
-
from django.utils.encoding import force_bytes, force_str
|
|
31
|
-
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
|
|
32
|
-
from core import mailer, public_wifi
|
|
33
|
-
from core.backends import TOTP_DEVICE_NAME
|
|
34
|
-
from django.utils.translation import gettext as _
|
|
35
|
-
from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie
|
|
36
|
-
from django.views.decorators.http import require_POST
|
|
37
|
-
from django.core.cache import cache
|
|
38
|
-
from django.views.decorators.cache import never_cache
|
|
39
|
-
from django.utils.cache import patch_vary_headers
|
|
40
|
-
from django.core.exceptions import PermissionDenied
|
|
41
|
-
from django.utils.text import slugify
|
|
42
|
-
from django.core.validators import EmailValidator
|
|
43
|
-
from django.db.models import Q
|
|
44
|
-
from core.models import
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
import
|
|
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
|
-
from .
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
from .
|
|
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
|
-
if
|
|
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
|
-
"
|
|
402
|
-
"
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
)
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
if action == "
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
)
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
)
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
)
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
help_text=_("
|
|
896
|
-
)
|
|
897
|
-
|
|
898
|
-
label=_("
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
help_text=_("
|
|
902
|
-
)
|
|
903
|
-
|
|
904
|
-
label=_("
|
|
905
|
-
required=False,
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
report.
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
return
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
def
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1
|
+
import base64
|
|
2
|
+
import logging
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from types import SimpleNamespace
|
|
5
|
+
import datetime
|
|
6
|
+
import calendar
|
|
7
|
+
import io
|
|
8
|
+
import shutil
|
|
9
|
+
import re
|
|
10
|
+
from html import escape
|
|
11
|
+
from urllib.parse import urlparse
|
|
12
|
+
|
|
13
|
+
from django.conf import settings
|
|
14
|
+
from django.contrib import admin
|
|
15
|
+
from django.contrib import messages
|
|
16
|
+
from django.contrib.admin.views.decorators import staff_member_required
|
|
17
|
+
from django.contrib.auth import get_user_model, login
|
|
18
|
+
from django.contrib.auth.tokens import default_token_generator
|
|
19
|
+
from django.contrib.auth.views import LoginView
|
|
20
|
+
from django import forms
|
|
21
|
+
from django.apps import apps as django_apps
|
|
22
|
+
from utils.sites import get_site
|
|
23
|
+
from django.http import Http404, HttpResponse, JsonResponse
|
|
24
|
+
from django.shortcuts import get_object_or_404, redirect, render
|
|
25
|
+
from nodes.models import Node
|
|
26
|
+
from django.template.response import TemplateResponse
|
|
27
|
+
from django.test import RequestFactory
|
|
28
|
+
from django.urls import NoReverseMatch, reverse
|
|
29
|
+
from django.utils import timezone
|
|
30
|
+
from django.utils.encoding import force_bytes, force_str
|
|
31
|
+
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
|
|
32
|
+
from core import mailer, public_wifi
|
|
33
|
+
from core.backends import TOTP_DEVICE_NAME
|
|
34
|
+
from django.utils.translation import gettext as _
|
|
35
|
+
from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie
|
|
36
|
+
from django.views.decorators.http import require_POST
|
|
37
|
+
from django.core.cache import cache
|
|
38
|
+
from django.views.decorators.cache import never_cache
|
|
39
|
+
from django.utils.cache import patch_vary_headers
|
|
40
|
+
from django.core.exceptions import PermissionDenied
|
|
41
|
+
from django.utils.text import slugify
|
|
42
|
+
from django.core.validators import EmailValidator
|
|
43
|
+
from django.db.models import Q
|
|
44
|
+
from core.models import (
|
|
45
|
+
InviteLead,
|
|
46
|
+
ClientReport,
|
|
47
|
+
ClientReportSchedule,
|
|
48
|
+
SecurityGroup,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
try: # pragma: no cover - optional dependency guard
|
|
52
|
+
from graphviz import Digraph
|
|
53
|
+
from graphviz.backend import CalledProcessError, ExecutableNotFound
|
|
54
|
+
except ImportError: # pragma: no cover - handled gracefully in views
|
|
55
|
+
Digraph = None
|
|
56
|
+
CalledProcessError = ExecutableNotFound = None
|
|
57
|
+
|
|
58
|
+
import markdown
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
MARKDOWN_EXTENSIONS = ["toc", "tables", "mdx_truly_sane_lists"]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _render_markdown_with_toc(text: str) -> tuple[str, str]:
|
|
65
|
+
"""Render ``text`` to HTML and return the HTML and stripped TOC."""
|
|
66
|
+
|
|
67
|
+
md = markdown.Markdown(extensions=MARKDOWN_EXTENSIONS)
|
|
68
|
+
html = md.convert(text)
|
|
69
|
+
toc_html = md.toc
|
|
70
|
+
toc_html = _strip_toc_wrapper(toc_html)
|
|
71
|
+
return html, toc_html
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _strip_toc_wrapper(toc_html: str) -> str:
|
|
75
|
+
"""Normalize ``markdown``'s TOC output by removing the wrapper ``div``."""
|
|
76
|
+
|
|
77
|
+
toc_html = toc_html.strip()
|
|
78
|
+
if toc_html.startswith('<div class="toc">'):
|
|
79
|
+
toc_html = toc_html[len('<div class="toc">') :]
|
|
80
|
+
if toc_html.endswith("</div>"):
|
|
81
|
+
toc_html = toc_html[: -len("</div>")]
|
|
82
|
+
return toc_html.strip()
|
|
83
|
+
from pages.utils import landing
|
|
84
|
+
from core.liveupdate import live_update
|
|
85
|
+
from django_otp import login as otp_login
|
|
86
|
+
from django_otp.plugins.otp_totp.models import TOTPDevice
|
|
87
|
+
import qrcode
|
|
88
|
+
from .forms import (
|
|
89
|
+
AuthenticatorEnrollmentForm,
|
|
90
|
+
AuthenticatorLoginForm,
|
|
91
|
+
UserStoryForm,
|
|
92
|
+
)
|
|
93
|
+
from .models import Module, RoleLanding, UserManual, UserStory
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
logger = logging.getLogger(__name__)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _get_registered_models(app_label: str):
|
|
100
|
+
"""Return admin-registered models for the given app label."""
|
|
101
|
+
|
|
102
|
+
registered = [
|
|
103
|
+
model for model in admin.site._registry if model._meta.app_label == app_label
|
|
104
|
+
]
|
|
105
|
+
return sorted(registered, key=lambda model: str(model._meta.verbose_name))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _filter_models_for_request(models, request):
|
|
109
|
+
"""Filter ``models`` to only those viewable by ``request.user``."""
|
|
110
|
+
|
|
111
|
+
allowed = []
|
|
112
|
+
for model in models:
|
|
113
|
+
model_admin = admin.site._registry.get(model)
|
|
114
|
+
if model_admin is None:
|
|
115
|
+
continue
|
|
116
|
+
if not model_admin.has_module_permission(request) and not getattr(
|
|
117
|
+
request.user, "is_staff", False
|
|
118
|
+
):
|
|
119
|
+
continue
|
|
120
|
+
if not model_admin.has_view_permission(request, obj=None) and not getattr(
|
|
121
|
+
request.user, "is_staff", False
|
|
122
|
+
):
|
|
123
|
+
continue
|
|
124
|
+
allowed.append(model)
|
|
125
|
+
return allowed
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _admin_has_app_permission(request, app_label: str) -> bool:
|
|
129
|
+
"""Return whether the admin user can access the given app."""
|
|
130
|
+
|
|
131
|
+
has_app_permission = getattr(admin.site, "has_app_permission", None)
|
|
132
|
+
if callable(has_app_permission):
|
|
133
|
+
allowed = has_app_permission(request, app_label)
|
|
134
|
+
else:
|
|
135
|
+
allowed = bool(admin.site.get_app_list(request, app_label))
|
|
136
|
+
|
|
137
|
+
if not allowed and getattr(request.user, "is_staff", False):
|
|
138
|
+
return True
|
|
139
|
+
return allowed
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _resolve_related_model(field, default_app_label: str):
|
|
143
|
+
"""Resolve the Django model class referenced by ``field``."""
|
|
144
|
+
|
|
145
|
+
remote = getattr(getattr(field, "remote_field", None), "model", None)
|
|
146
|
+
if remote is None:
|
|
147
|
+
return None
|
|
148
|
+
if isinstance(remote, str):
|
|
149
|
+
if "." in remote:
|
|
150
|
+
app_label, model_name = remote.split(".", 1)
|
|
151
|
+
else:
|
|
152
|
+
app_label, model_name = default_app_label, remote
|
|
153
|
+
try:
|
|
154
|
+
remote = django_apps.get_model(app_label, model_name)
|
|
155
|
+
except LookupError:
|
|
156
|
+
return None
|
|
157
|
+
return remote
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _graph_field_type(field, default_app_label: str) -> str:
|
|
161
|
+
"""Format a field description for node labels."""
|
|
162
|
+
|
|
163
|
+
base = field.get_internal_type()
|
|
164
|
+
related = _resolve_related_model(field, default_app_label)
|
|
165
|
+
if related is not None:
|
|
166
|
+
base = f"{base} → {related._meta.object_name}"
|
|
167
|
+
return base
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _build_model_graph(models):
|
|
171
|
+
"""Generate a GraphViz ``Digraph`` for the provided ``models``."""
|
|
172
|
+
|
|
173
|
+
if Digraph is None:
|
|
174
|
+
raise RuntimeError("Graphviz is not installed")
|
|
175
|
+
|
|
176
|
+
graph = Digraph(
|
|
177
|
+
name="admin_app_models",
|
|
178
|
+
graph_attr={
|
|
179
|
+
"rankdir": "LR",
|
|
180
|
+
"splines": "ortho",
|
|
181
|
+
"nodesep": "0.8",
|
|
182
|
+
"ranksep": "1.0",
|
|
183
|
+
},
|
|
184
|
+
node_attr={
|
|
185
|
+
"shape": "plaintext",
|
|
186
|
+
"fontname": "Helvetica",
|
|
187
|
+
},
|
|
188
|
+
edge_attr={"fontname": "Helvetica"},
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
node_ids = {}
|
|
192
|
+
for model in models:
|
|
193
|
+
node_id = f"{model._meta.app_label}.{model._meta.model_name}"
|
|
194
|
+
node_ids[model] = node_id
|
|
195
|
+
|
|
196
|
+
rows = [
|
|
197
|
+
'<tr><td bgcolor="#1f2933" colspan="2"><font color="white"><b>'
|
|
198
|
+
f"{escape(model._meta.object_name)}"
|
|
199
|
+
"</b></font></td></tr>"
|
|
200
|
+
]
|
|
201
|
+
|
|
202
|
+
verbose_name = str(model._meta.verbose_name)
|
|
203
|
+
if verbose_name and verbose_name != model._meta.object_name:
|
|
204
|
+
rows.append(
|
|
205
|
+
'<tr><td colspan="2"><i>' f"{escape(verbose_name)}" "</i></td></tr>"
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
for field in model._meta.concrete_fields:
|
|
209
|
+
if field.auto_created and not field.concrete:
|
|
210
|
+
continue
|
|
211
|
+
name = escape(field.name)
|
|
212
|
+
if field.primary_key:
|
|
213
|
+
name = f"<u>{name}</u>"
|
|
214
|
+
type_label = escape(_graph_field_type(field, model._meta.app_label))
|
|
215
|
+
rows.append(
|
|
216
|
+
'<tr><td align="left">'
|
|
217
|
+
f"{name}"
|
|
218
|
+
'</td><td align="left">'
|
|
219
|
+
f"{type_label}"
|
|
220
|
+
"</td></tr>"
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
for field in model._meta.local_many_to_many:
|
|
224
|
+
name = escape(field.name)
|
|
225
|
+
type_label = _graph_field_type(field, model._meta.app_label)
|
|
226
|
+
rows.append(
|
|
227
|
+
'<tr><td align="left">'
|
|
228
|
+
f"{name}"
|
|
229
|
+
'</td><td align="left">'
|
|
230
|
+
f"{escape(type_label)}"
|
|
231
|
+
"</td></tr>"
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
label = '<\n <table BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">\n '
|
|
235
|
+
label += "\n ".join(rows)
|
|
236
|
+
label += "\n </table>\n>"
|
|
237
|
+
graph.node(name=node_id, label=label)
|
|
238
|
+
|
|
239
|
+
edges = set()
|
|
240
|
+
for model in models:
|
|
241
|
+
source_id = node_ids[model]
|
|
242
|
+
for field in model._meta.concrete_fields:
|
|
243
|
+
related = _resolve_related_model(field, model._meta.app_label)
|
|
244
|
+
if related not in node_ids:
|
|
245
|
+
continue
|
|
246
|
+
attrs = {"label": field.name}
|
|
247
|
+
if getattr(field, "one_to_one", False):
|
|
248
|
+
attrs.update({"arrowhead": "onormal", "arrowtail": "none"})
|
|
249
|
+
key = (source_id, node_ids[related], tuple(sorted(attrs.items())))
|
|
250
|
+
if key not in edges:
|
|
251
|
+
edges.add(key)
|
|
252
|
+
graph.edge(
|
|
253
|
+
tail_name=source_id,
|
|
254
|
+
head_name=node_ids[related],
|
|
255
|
+
**attrs,
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
for field in model._meta.local_many_to_many:
|
|
259
|
+
related = _resolve_related_model(field, model._meta.app_label)
|
|
260
|
+
if related not in node_ids:
|
|
261
|
+
continue
|
|
262
|
+
attrs = {
|
|
263
|
+
"label": f"{field.name} (M2M)",
|
|
264
|
+
"dir": "both",
|
|
265
|
+
"arrowhead": "normal",
|
|
266
|
+
"arrowtail": "normal",
|
|
267
|
+
}
|
|
268
|
+
key = (source_id, node_ids[related], tuple(sorted(attrs.items())))
|
|
269
|
+
if key not in edges:
|
|
270
|
+
edges.add(key)
|
|
271
|
+
graph.edge(
|
|
272
|
+
tail_name=source_id,
|
|
273
|
+
head_name=node_ids[related],
|
|
274
|
+
**attrs,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
return graph
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
@staff_member_required
|
|
281
|
+
def admin_model_graph(request, app_label: str):
|
|
282
|
+
"""Render a GraphViz-powered diagram for the admin app grouping."""
|
|
283
|
+
|
|
284
|
+
try:
|
|
285
|
+
app_config = django_apps.get_app_config(app_label)
|
|
286
|
+
except LookupError as exc: # pragma: no cover - invalid app label
|
|
287
|
+
raise Http404("Unknown application") from exc
|
|
288
|
+
|
|
289
|
+
models = _get_registered_models(app_label)
|
|
290
|
+
if not models:
|
|
291
|
+
raise Http404("No admin models registered for this application")
|
|
292
|
+
|
|
293
|
+
if not _admin_has_app_permission(request, app_label):
|
|
294
|
+
raise PermissionDenied
|
|
295
|
+
|
|
296
|
+
models = _filter_models_for_request(models, request)
|
|
297
|
+
if not models:
|
|
298
|
+
raise PermissionDenied
|
|
299
|
+
|
|
300
|
+
if Digraph is None: # pragma: no cover - dependency missing is unexpected
|
|
301
|
+
raise Http404("Graph visualization support is unavailable")
|
|
302
|
+
|
|
303
|
+
graph = _build_model_graph(models)
|
|
304
|
+
graph_source = graph.source
|
|
305
|
+
|
|
306
|
+
graph_svg = ""
|
|
307
|
+
graph_error = ""
|
|
308
|
+
graph_engine = getattr(graph, "engine", "dot")
|
|
309
|
+
engine_path = shutil.which(str(graph_engine))
|
|
310
|
+
download_format = request.GET.get("format")
|
|
311
|
+
|
|
312
|
+
if download_format == "pdf":
|
|
313
|
+
if engine_path is None:
|
|
314
|
+
messages.error(
|
|
315
|
+
request,
|
|
316
|
+
_(
|
|
317
|
+
"Graphviz executables are required to download the diagram as a PDF. Install Graphviz on the server and try again."
|
|
318
|
+
),
|
|
319
|
+
)
|
|
320
|
+
else:
|
|
321
|
+
try:
|
|
322
|
+
pdf_output = graph.pipe(format="pdf")
|
|
323
|
+
except (ExecutableNotFound, CalledProcessError) as exc:
|
|
324
|
+
logger.warning(
|
|
325
|
+
"Graphviz PDF rendering failed for admin model graph (engine=%s)",
|
|
326
|
+
graph_engine,
|
|
327
|
+
exc_info=exc,
|
|
328
|
+
)
|
|
329
|
+
messages.error(
|
|
330
|
+
request,
|
|
331
|
+
_(
|
|
332
|
+
"An error occurred while generating the PDF diagram. Check the server logs for details."
|
|
333
|
+
),
|
|
334
|
+
)
|
|
335
|
+
else:
|
|
336
|
+
filename = slugify(app_config.verbose_name) or app_label
|
|
337
|
+
response = HttpResponse(pdf_output, content_type="application/pdf")
|
|
338
|
+
response["Content-Disposition"] = (
|
|
339
|
+
f'attachment; filename="{filename}-model-graph.pdf"'
|
|
340
|
+
)
|
|
341
|
+
return response
|
|
342
|
+
|
|
343
|
+
params = request.GET.copy()
|
|
344
|
+
if "format" in params:
|
|
345
|
+
del params["format"]
|
|
346
|
+
query_string = params.urlencode()
|
|
347
|
+
redirect_url = request.path
|
|
348
|
+
if query_string:
|
|
349
|
+
redirect_url = f"{request.path}?{query_string}"
|
|
350
|
+
return redirect(redirect_url)
|
|
351
|
+
|
|
352
|
+
if engine_path is None:
|
|
353
|
+
graph_error = _(
|
|
354
|
+
"Graphviz executables are required to render this diagram. Install Graphviz on the server and try again."
|
|
355
|
+
)
|
|
356
|
+
else:
|
|
357
|
+
try:
|
|
358
|
+
svg_output = graph.pipe(format="svg", encoding="utf-8")
|
|
359
|
+
except (ExecutableNotFound, CalledProcessError) as exc:
|
|
360
|
+
logger.warning(
|
|
361
|
+
"Graphviz rendering failed for admin model graph (engine=%s)",
|
|
362
|
+
graph_engine,
|
|
363
|
+
exc_info=exc,
|
|
364
|
+
)
|
|
365
|
+
graph_error = _(
|
|
366
|
+
"An error occurred while rendering the diagram. Check the server logs for details."
|
|
367
|
+
)
|
|
368
|
+
else:
|
|
369
|
+
svg_start = svg_output.find("<svg")
|
|
370
|
+
if svg_start != -1:
|
|
371
|
+
svg_output = svg_output[svg_start:]
|
|
372
|
+
label = _("%(app)s model diagram") % {"app": app_config.verbose_name}
|
|
373
|
+
graph_svg = svg_output.replace(
|
|
374
|
+
"<svg", f'<svg role="img" aria-label="{escape(label)}"', 1
|
|
375
|
+
)
|
|
376
|
+
if not graph_svg:
|
|
377
|
+
graph_error = _("Graphviz did not return any diagram output.")
|
|
378
|
+
|
|
379
|
+
model_links = []
|
|
380
|
+
for model in models:
|
|
381
|
+
opts = model._meta
|
|
382
|
+
try:
|
|
383
|
+
url = reverse(f"admin:{opts.app_label}_{opts.model_name}_changelist")
|
|
384
|
+
except NoReverseMatch:
|
|
385
|
+
url = ""
|
|
386
|
+
model_links.append(
|
|
387
|
+
{
|
|
388
|
+
"label": str(opts.verbose_name_plural),
|
|
389
|
+
"url": url,
|
|
390
|
+
}
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
download_params = request.GET.copy()
|
|
394
|
+
download_params["format"] = "pdf"
|
|
395
|
+
download_url = f"{request.path}?{download_params.urlencode()}"
|
|
396
|
+
|
|
397
|
+
context = admin.site.each_context(request)
|
|
398
|
+
context.update(
|
|
399
|
+
{
|
|
400
|
+
"app_label": app_label,
|
|
401
|
+
"app_verbose_name": app_config.verbose_name,
|
|
402
|
+
"graph_source": graph_source,
|
|
403
|
+
"graph_svg": graph_svg,
|
|
404
|
+
"graph_error": graph_error,
|
|
405
|
+
"models": model_links,
|
|
406
|
+
"title": _("%(app)s model graph") % {"app": app_config.verbose_name},
|
|
407
|
+
"download_url": download_url,
|
|
408
|
+
}
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
return TemplateResponse(request, "admin/model_graph.html", context)
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
@landing("Home")
|
|
415
|
+
@never_cache
|
|
416
|
+
def index(request):
|
|
417
|
+
site = get_site(request)
|
|
418
|
+
if site:
|
|
419
|
+
try:
|
|
420
|
+
landing = site.badge.landing_override
|
|
421
|
+
except Exception:
|
|
422
|
+
landing = None
|
|
423
|
+
if landing:
|
|
424
|
+
return redirect(landing.path)
|
|
425
|
+
node = Node.get_local()
|
|
426
|
+
role = node.role if node else None
|
|
427
|
+
landing_filters = Q()
|
|
428
|
+
if role:
|
|
429
|
+
landing_filters |= Q(node_role=role)
|
|
430
|
+
user = getattr(request, "user", None)
|
|
431
|
+
if user and user.is_authenticated:
|
|
432
|
+
landing_filters |= Q(user=user)
|
|
433
|
+
user_group_ids = list(user.groups.values_list("pk", flat=True))
|
|
434
|
+
if user_group_ids:
|
|
435
|
+
security_group_ids = list(
|
|
436
|
+
SecurityGroup.objects.filter(pk__in=user_group_ids).values_list(
|
|
437
|
+
"pk", flat=True
|
|
438
|
+
)
|
|
439
|
+
)
|
|
440
|
+
if security_group_ids:
|
|
441
|
+
landing_filters |= Q(security_group_id__in=security_group_ids)
|
|
442
|
+
if landing_filters:
|
|
443
|
+
role_landing = (
|
|
444
|
+
RoleLanding.objects.filter(
|
|
445
|
+
landing_filters,
|
|
446
|
+
is_deleted=False,
|
|
447
|
+
landing__enabled=True,
|
|
448
|
+
landing__is_deleted=False,
|
|
449
|
+
)
|
|
450
|
+
.select_related("landing")
|
|
451
|
+
.order_by("-priority", "-pk")
|
|
452
|
+
.first()
|
|
453
|
+
)
|
|
454
|
+
if role_landing and role_landing.landing_id:
|
|
455
|
+
landing_obj = role_landing.landing
|
|
456
|
+
target_path = landing_obj.path
|
|
457
|
+
if target_path and target_path != request.path:
|
|
458
|
+
return redirect(target_path)
|
|
459
|
+
app = (
|
|
460
|
+
Module.objects.filter(node_role=role, is_default=True)
|
|
461
|
+
.select_related("application")
|
|
462
|
+
.first()
|
|
463
|
+
)
|
|
464
|
+
app_slug = app.path.strip("/") if app else ""
|
|
465
|
+
readme_base = (
|
|
466
|
+
Path(settings.BASE_DIR) / app_slug if app_slug else Path(settings.BASE_DIR)
|
|
467
|
+
)
|
|
468
|
+
lang = getattr(request, "LANGUAGE_CODE", "")
|
|
469
|
+
lang = lang.replace("_", "-").lower()
|
|
470
|
+
root_base = Path(settings.BASE_DIR)
|
|
471
|
+
candidates = []
|
|
472
|
+
if lang:
|
|
473
|
+
candidates.append(readme_base / f"README.{lang}.md")
|
|
474
|
+
short = lang.split("-")[0]
|
|
475
|
+
if short != lang:
|
|
476
|
+
candidates.append(readme_base / f"README.{short}.md")
|
|
477
|
+
candidates.append(readme_base / "README.md")
|
|
478
|
+
if readme_base != root_base:
|
|
479
|
+
if lang:
|
|
480
|
+
candidates.append(root_base / f"README.{lang}.md")
|
|
481
|
+
short = lang.split("-")[0]
|
|
482
|
+
if short != lang:
|
|
483
|
+
candidates.append(root_base / f"README.{short}.md")
|
|
484
|
+
candidates.append(root_base / "README.md")
|
|
485
|
+
readme_file = next((p for p in candidates if p.exists()), root_base / "README.md")
|
|
486
|
+
text = readme_file.read_text(encoding="utf-8")
|
|
487
|
+
html, toc_html = _render_markdown_with_toc(text)
|
|
488
|
+
title = "README" if readme_file.name.startswith("README") else readme_file.stem
|
|
489
|
+
context = {"content": html, "title": title, "toc": toc_html}
|
|
490
|
+
response = render(request, "pages/readme.html", context)
|
|
491
|
+
patch_vary_headers(response, ["Accept-Language", "Cookie"])
|
|
492
|
+
return response
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def sitemap(request):
|
|
496
|
+
site = get_site(request)
|
|
497
|
+
node = Node.get_local()
|
|
498
|
+
role = node.role if node else None
|
|
499
|
+
applications = Module.objects.filter(node_role=role)
|
|
500
|
+
base = request.build_absolute_uri("/").rstrip("/")
|
|
501
|
+
lines = [
|
|
502
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
503
|
+
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
|
504
|
+
]
|
|
505
|
+
seen = set()
|
|
506
|
+
for app in applications:
|
|
507
|
+
loc = f"{base}{app.path}"
|
|
508
|
+
if loc not in seen:
|
|
509
|
+
seen.add(loc)
|
|
510
|
+
lines.append(f" <url><loc>{loc}</loc></url>")
|
|
511
|
+
lines.append("</urlset>")
|
|
512
|
+
return HttpResponse("\n".join(lines), content_type="application/xml")
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def release_checklist(request):
|
|
516
|
+
file_path = Path(settings.BASE_DIR) / "releases" / "release-checklist.md"
|
|
517
|
+
if not file_path.exists():
|
|
518
|
+
raise Http404("Release checklist not found")
|
|
519
|
+
text = file_path.read_text(encoding="utf-8")
|
|
520
|
+
html, toc_html = _render_markdown_with_toc(text)
|
|
521
|
+
context = {"content": html, "title": "Release Checklist", "toc": toc_html}
|
|
522
|
+
response = render(request, "pages/readme.html", context)
|
|
523
|
+
patch_vary_headers(response, ["Accept-Language", "Cookie"])
|
|
524
|
+
return response
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
@csrf_exempt
|
|
528
|
+
def datasette_auth(request):
|
|
529
|
+
if request.user.is_authenticated:
|
|
530
|
+
return HttpResponse("OK")
|
|
531
|
+
return HttpResponse(status=401)
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
class CustomLoginView(LoginView):
|
|
535
|
+
"""Login view that redirects staff to the admin."""
|
|
536
|
+
|
|
537
|
+
template_name = "pages/login.html"
|
|
538
|
+
form_class = AuthenticatorLoginForm
|
|
539
|
+
|
|
540
|
+
def dispatch(self, request, *args, **kwargs):
|
|
541
|
+
if request.user.is_authenticated:
|
|
542
|
+
return redirect(self.get_success_url())
|
|
543
|
+
return super().dispatch(request, *args, **kwargs)
|
|
544
|
+
|
|
545
|
+
def get_context_data(self, **kwargs):
|
|
546
|
+
context = super(LoginView, self).get_context_data(**kwargs)
|
|
547
|
+
current_site = get_site(self.request)
|
|
548
|
+
redirect_target = self.request.GET.get(self.redirect_field_name)
|
|
549
|
+
restricted_notice = None
|
|
550
|
+
if redirect_target:
|
|
551
|
+
parsed_target = urlparse(redirect_target)
|
|
552
|
+
target_path = parsed_target.path or redirect_target
|
|
553
|
+
try:
|
|
554
|
+
simulator_path = reverse("cp-simulator")
|
|
555
|
+
except NoReverseMatch: # pragma: no cover - simulator may be uninstalled
|
|
556
|
+
simulator_path = None
|
|
557
|
+
if simulator_path and target_path.startswith(simulator_path):
|
|
558
|
+
restricted_notice = _(
|
|
559
|
+
"This page is reserved for members only. Please log in to continue."
|
|
560
|
+
)
|
|
561
|
+
context.update(
|
|
562
|
+
{
|
|
563
|
+
"site": current_site,
|
|
564
|
+
"site_name": getattr(current_site, "name", ""),
|
|
565
|
+
"next": self.get_success_url(),
|
|
566
|
+
"can_request_invite": mailer.can_send_email(),
|
|
567
|
+
"restricted_notice": restricted_notice,
|
|
568
|
+
}
|
|
569
|
+
)
|
|
570
|
+
return context
|
|
571
|
+
|
|
572
|
+
def get_success_url(self):
|
|
573
|
+
redirect_url = self.get_redirect_url()
|
|
574
|
+
if redirect_url:
|
|
575
|
+
return redirect_url
|
|
576
|
+
if self.request.user.is_staff:
|
|
577
|
+
return reverse("admin:index")
|
|
578
|
+
return "/"
|
|
579
|
+
|
|
580
|
+
def form_valid(self, form):
|
|
581
|
+
response = super().form_valid(form)
|
|
582
|
+
device = form.get_verified_device()
|
|
583
|
+
if device is not None:
|
|
584
|
+
otp_login(self.request, device)
|
|
585
|
+
return response
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
login_view = CustomLoginView.as_view()
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
@staff_member_required
|
|
592
|
+
def authenticator_setup(request):
|
|
593
|
+
"""Allow staff to enroll an authenticator app for TOTP logins."""
|
|
594
|
+
|
|
595
|
+
user = request.user
|
|
596
|
+
device_qs = TOTPDevice.objects.filter(user=user)
|
|
597
|
+
if TOTP_DEVICE_NAME:
|
|
598
|
+
device_qs = device_qs.filter(name=TOTP_DEVICE_NAME)
|
|
599
|
+
|
|
600
|
+
pending_device = device_qs.filter(confirmed=False).order_by("-id").first()
|
|
601
|
+
confirmed_device = device_qs.filter(confirmed=True).order_by("-id").first()
|
|
602
|
+
enrollment_form = AuthenticatorEnrollmentForm(device=pending_device)
|
|
603
|
+
|
|
604
|
+
if request.method == "POST":
|
|
605
|
+
action = request.POST.get("action")
|
|
606
|
+
if action == "generate":
|
|
607
|
+
device = pending_device or confirmed_device or TOTPDevice(user=user)
|
|
608
|
+
if TOTP_DEVICE_NAME:
|
|
609
|
+
device.name = TOTP_DEVICE_NAME
|
|
610
|
+
if device.pk is None:
|
|
611
|
+
device.save()
|
|
612
|
+
device.key = TOTPDevice._meta.get_field("key").get_default()
|
|
613
|
+
device.confirmed = False
|
|
614
|
+
device.drift = 0
|
|
615
|
+
device.last_t = -1
|
|
616
|
+
device.throttling_failure_count = 0
|
|
617
|
+
device.throttling_failure_timestamp = None
|
|
618
|
+
device.throttle_reset(commit=False)
|
|
619
|
+
device.save()
|
|
620
|
+
messages.success(
|
|
621
|
+
request,
|
|
622
|
+
_(
|
|
623
|
+
"Scan the QR code with your authenticator app, then "
|
|
624
|
+
"enter a code below to confirm enrollment."
|
|
625
|
+
),
|
|
626
|
+
)
|
|
627
|
+
return redirect("pages:authenticator-setup")
|
|
628
|
+
if action == "confirm" and pending_device is not None:
|
|
629
|
+
enrollment_form = AuthenticatorEnrollmentForm(
|
|
630
|
+
request.POST, device=pending_device
|
|
631
|
+
)
|
|
632
|
+
if enrollment_form.is_valid():
|
|
633
|
+
pending_device.confirmed = True
|
|
634
|
+
pending_device.save(update_fields=["confirmed"])
|
|
635
|
+
messages.success(
|
|
636
|
+
request,
|
|
637
|
+
_(
|
|
638
|
+
"Authenticator app confirmed. You can now log in "
|
|
639
|
+
"with codes from your device."
|
|
640
|
+
),
|
|
641
|
+
)
|
|
642
|
+
return redirect("pages:authenticator-setup")
|
|
643
|
+
if action == "remove":
|
|
644
|
+
if device_qs.exists():
|
|
645
|
+
device_qs.delete()
|
|
646
|
+
messages.success(
|
|
647
|
+
request,
|
|
648
|
+
_(
|
|
649
|
+
"Authenticator enrollment removed. Password logins "
|
|
650
|
+
"remain available."
|
|
651
|
+
),
|
|
652
|
+
)
|
|
653
|
+
return redirect("pages:authenticator-setup")
|
|
654
|
+
|
|
655
|
+
pending_device = device_qs.filter(confirmed=False).order_by("-id").first()
|
|
656
|
+
confirmed_device = device_qs.filter(confirmed=True).order_by("-id").first()
|
|
657
|
+
|
|
658
|
+
qr_data_uri = None
|
|
659
|
+
manual_key = None
|
|
660
|
+
if pending_device is not None:
|
|
661
|
+
config_url = pending_device.config_url
|
|
662
|
+
qr = qrcode.QRCode(box_size=10, border=4)
|
|
663
|
+
qr.add_data(config_url)
|
|
664
|
+
qr.make(fit=True)
|
|
665
|
+
image = qr.make_image(fill_color="black", back_color="white")
|
|
666
|
+
buffer = io.BytesIO()
|
|
667
|
+
image.save(buffer, format="PNG")
|
|
668
|
+
qr_data_uri = "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode(
|
|
669
|
+
"ascii"
|
|
670
|
+
)
|
|
671
|
+
secret = pending_device.key or ""
|
|
672
|
+
manual_key = " ".join(secret[i : i + 4] for i in range(0, len(secret), 4))
|
|
673
|
+
|
|
674
|
+
context = {
|
|
675
|
+
"pending_device": pending_device,
|
|
676
|
+
"confirmed_device": confirmed_device,
|
|
677
|
+
"qr_data_uri": qr_data_uri,
|
|
678
|
+
"manual_key": manual_key,
|
|
679
|
+
"enrollment_form": enrollment_form,
|
|
680
|
+
}
|
|
681
|
+
return TemplateResponse(request, "pages/authenticator_setup.html", context)
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
INVITATION_REQUEST_MIN_SUBMISSION_INTERVAL = datetime.timedelta(seconds=3)
|
|
685
|
+
INVITATION_REQUEST_THROTTLE_LIMIT = 3
|
|
686
|
+
INVITATION_REQUEST_THROTTLE_WINDOW = datetime.timedelta(hours=1)
|
|
687
|
+
INVITATION_REQUEST_HONEYPOT_MESSAGE = _(
|
|
688
|
+
"We could not process your request. Please try again."
|
|
689
|
+
)
|
|
690
|
+
INVITATION_REQUEST_TOO_FAST_MESSAGE = _(
|
|
691
|
+
"That was a little too fast. Please wait a moment and try again."
|
|
692
|
+
)
|
|
693
|
+
INVITATION_REQUEST_TIMESTAMP_ERROR = _(
|
|
694
|
+
"We could not verify your submission. Please reload the page and try again."
|
|
695
|
+
)
|
|
696
|
+
INVITATION_REQUEST_THROTTLE_MESSAGE = _(
|
|
697
|
+
"We've already received a few requests. Please try again later."
|
|
698
|
+
)
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
class InvitationRequestForm(forms.Form):
|
|
702
|
+
email = forms.EmailField()
|
|
703
|
+
comment = forms.CharField(
|
|
704
|
+
required=False, widget=forms.Textarea, label=_("Comment")
|
|
705
|
+
)
|
|
706
|
+
honeypot = forms.CharField(
|
|
707
|
+
required=False,
|
|
708
|
+
label=_("Leave blank"),
|
|
709
|
+
widget=forms.TextInput(attrs={"autocomplete": "off"}),
|
|
710
|
+
)
|
|
711
|
+
timestamp = forms.DateTimeField(required=False, widget=forms.HiddenInput())
|
|
712
|
+
|
|
713
|
+
min_submission_interval = INVITATION_REQUEST_MIN_SUBMISSION_INTERVAL
|
|
714
|
+
|
|
715
|
+
def __init__(self, *args, **kwargs):
|
|
716
|
+
super().__init__(*args, **kwargs)
|
|
717
|
+
if not self.is_bound:
|
|
718
|
+
self.fields["timestamp"].initial = timezone.now()
|
|
719
|
+
self.fields["honeypot"].widget.attrs.setdefault("aria-hidden", "true")
|
|
720
|
+
self.fields["honeypot"].widget.attrs.setdefault("tabindex", "-1")
|
|
721
|
+
|
|
722
|
+
def clean(self):
|
|
723
|
+
cleaned = super().clean()
|
|
724
|
+
|
|
725
|
+
honeypot_value = cleaned.get("honeypot", "")
|
|
726
|
+
if honeypot_value:
|
|
727
|
+
raise forms.ValidationError(INVITATION_REQUEST_HONEYPOT_MESSAGE)
|
|
728
|
+
|
|
729
|
+
timestamp = cleaned.get("timestamp")
|
|
730
|
+
if timestamp is None:
|
|
731
|
+
cleaned["timestamp"] = timezone.now()
|
|
732
|
+
return cleaned
|
|
733
|
+
|
|
734
|
+
now = timezone.now()
|
|
735
|
+
if timestamp > now or (now - timestamp) < self.min_submission_interval:
|
|
736
|
+
raise forms.ValidationError(INVITATION_REQUEST_TOO_FAST_MESSAGE)
|
|
737
|
+
|
|
738
|
+
return cleaned
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
@csrf_exempt
|
|
742
|
+
@ensure_csrf_cookie
|
|
743
|
+
def request_invite(request):
|
|
744
|
+
form = InvitationRequestForm(request.POST if request.method == "POST" else None)
|
|
745
|
+
sent = False
|
|
746
|
+
if request.method == "POST" and form.is_valid():
|
|
747
|
+
email = form.cleaned_data["email"]
|
|
748
|
+
comment = form.cleaned_data.get("comment", "")
|
|
749
|
+
ip_address = request.META.get("REMOTE_ADDR")
|
|
750
|
+
throttle_filters = Q(email__iexact=email)
|
|
751
|
+
if ip_address:
|
|
752
|
+
throttle_filters |= Q(ip_address=ip_address)
|
|
753
|
+
window_start = timezone.now() - INVITATION_REQUEST_THROTTLE_WINDOW
|
|
754
|
+
recent_requests = InviteLead.objects.filter(
|
|
755
|
+
throttle_filters, created_on__gte=window_start
|
|
756
|
+
)
|
|
757
|
+
if recent_requests.count() >= INVITATION_REQUEST_THROTTLE_LIMIT:
|
|
758
|
+
form.add_error(None, INVITATION_REQUEST_THROTTLE_MESSAGE)
|
|
759
|
+
else:
|
|
760
|
+
mac_address = public_wifi.resolve_mac_address(ip_address)
|
|
761
|
+
lead = InviteLead.objects.create(
|
|
762
|
+
email=email,
|
|
763
|
+
comment=comment,
|
|
764
|
+
user=request.user if request.user.is_authenticated else None,
|
|
765
|
+
path=request.path,
|
|
766
|
+
referer=request.META.get("HTTP_REFERER", ""),
|
|
767
|
+
user_agent=request.META.get("HTTP_USER_AGENT", ""),
|
|
768
|
+
ip_address=ip_address,
|
|
769
|
+
mac_address=mac_address or "",
|
|
770
|
+
)
|
|
771
|
+
logger.info("Invitation requested for %s", email)
|
|
772
|
+
User = get_user_model()
|
|
773
|
+
users = list(User.objects.filter(email__iexact=email))
|
|
774
|
+
if not users:
|
|
775
|
+
logger.warning("Invitation requested for unknown email %s", email)
|
|
776
|
+
for user in users:
|
|
777
|
+
uid = urlsafe_base64_encode(force_bytes(user.pk))
|
|
778
|
+
token = default_token_generator.make_token(user)
|
|
779
|
+
link = request.build_absolute_uri(
|
|
780
|
+
reverse("pages:invitation-login", args=[uid, token])
|
|
781
|
+
)
|
|
782
|
+
subject = _("Your invitation link")
|
|
783
|
+
body = _("Use the following link to access your account: %(link)s") % {
|
|
784
|
+
"link": link
|
|
785
|
+
}
|
|
786
|
+
try:
|
|
787
|
+
node_error = None
|
|
788
|
+
node = Node.get_local()
|
|
789
|
+
outbox = getattr(node, "email_outbox", None) if node else None
|
|
790
|
+
if node:
|
|
791
|
+
try:
|
|
792
|
+
result = node.send_mail(subject, body, [email])
|
|
793
|
+
lead.sent_via_outbox = outbox
|
|
794
|
+
except Exception as exc:
|
|
795
|
+
node_error = exc
|
|
796
|
+
lead.sent_via_outbox = None
|
|
797
|
+
logger.exception(
|
|
798
|
+
"Node send_mail failed, falling back to default backend"
|
|
799
|
+
)
|
|
800
|
+
result = mailer.send(
|
|
801
|
+
subject, body, [email], settings.DEFAULT_FROM_EMAIL
|
|
802
|
+
)
|
|
803
|
+
else:
|
|
804
|
+
result = mailer.send(
|
|
805
|
+
subject, body, [email], settings.DEFAULT_FROM_EMAIL
|
|
806
|
+
)
|
|
807
|
+
lead.sent_via_outbox = None
|
|
808
|
+
lead.sent_on = timezone.now()
|
|
809
|
+
if node_error:
|
|
810
|
+
lead.error = (
|
|
811
|
+
f"Node email send failed: {node_error}. "
|
|
812
|
+
"Invite was sent using default mail backend; ensure the "
|
|
813
|
+
"node's email service is running or check its configuration."
|
|
814
|
+
)
|
|
815
|
+
else:
|
|
816
|
+
lead.error = ""
|
|
817
|
+
logger.info(
|
|
818
|
+
"Invitation email sent to %s (user %s): %s", email, user.pk, result
|
|
819
|
+
)
|
|
820
|
+
except Exception as exc:
|
|
821
|
+
lead.error = f"{exc}. Ensure the email service is reachable and settings are correct."
|
|
822
|
+
lead.sent_via_outbox = None
|
|
823
|
+
logger.exception("Failed to send invitation email to %s", email)
|
|
824
|
+
if lead.sent_on or lead.error:
|
|
825
|
+
lead.save(update_fields=["sent_on", "error", "sent_via_outbox"])
|
|
826
|
+
sent = True
|
|
827
|
+
return render(request, "pages/request_invite.html", {"form": form, "sent": sent})
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
class InvitationPasswordForm(forms.Form):
|
|
831
|
+
new_password1 = forms.CharField(
|
|
832
|
+
widget=forms.PasswordInput, required=False, label=_("New password")
|
|
833
|
+
)
|
|
834
|
+
new_password2 = forms.CharField(
|
|
835
|
+
widget=forms.PasswordInput, required=False, label=_("Confirm password")
|
|
836
|
+
)
|
|
837
|
+
|
|
838
|
+
def clean(self):
|
|
839
|
+
cleaned = super().clean()
|
|
840
|
+
p1 = cleaned.get("new_password1")
|
|
841
|
+
p2 = cleaned.get("new_password2")
|
|
842
|
+
if p1 or p2:
|
|
843
|
+
if not p1 or not p2 or p1 != p2:
|
|
844
|
+
raise forms.ValidationError(_("Passwords do not match"))
|
|
845
|
+
return cleaned
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
def invitation_login(request, uidb64, token):
|
|
849
|
+
User = get_user_model()
|
|
850
|
+
try:
|
|
851
|
+
uid = force_str(urlsafe_base64_decode(uidb64))
|
|
852
|
+
user = User.objects.get(pk=uid)
|
|
853
|
+
except Exception:
|
|
854
|
+
user = None
|
|
855
|
+
if user is None or not default_token_generator.check_token(user, token):
|
|
856
|
+
return HttpResponse(_("Invalid invitation link"), status=400)
|
|
857
|
+
form = InvitationPasswordForm(request.POST if request.method == "POST" else None)
|
|
858
|
+
if request.method == "POST" and form.is_valid():
|
|
859
|
+
password = form.cleaned_data.get("new_password1")
|
|
860
|
+
if password:
|
|
861
|
+
user.set_password(password)
|
|
862
|
+
user.is_active = True
|
|
863
|
+
user.save()
|
|
864
|
+
node = Node.get_local()
|
|
865
|
+
if node and node.has_feature("ap-router"):
|
|
866
|
+
mac_address = public_wifi.resolve_mac_address(
|
|
867
|
+
request.META.get("REMOTE_ADDR")
|
|
868
|
+
)
|
|
869
|
+
if not mac_address:
|
|
870
|
+
mac_address = (
|
|
871
|
+
InviteLead.objects.filter(email__iexact=user.email)
|
|
872
|
+
.exclude(mac_address="")
|
|
873
|
+
.order_by("-created_on")
|
|
874
|
+
.values_list("mac_address", flat=True)
|
|
875
|
+
.first()
|
|
876
|
+
)
|
|
877
|
+
if mac_address:
|
|
878
|
+
public_wifi.grant_public_access(user, mac_address)
|
|
879
|
+
login(request, user, backend="core.backends.LocalhostAdminBackend")
|
|
880
|
+
return redirect(reverse("admin:index") if user.is_staff else "/")
|
|
881
|
+
return render(request, "pages/invitation_login.html", {"form": form})
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
class ClientReportForm(forms.Form):
|
|
885
|
+
PERIOD_CHOICES = [
|
|
886
|
+
("range", _("Date range")),
|
|
887
|
+
("week", _("Week")),
|
|
888
|
+
("month", _("Month")),
|
|
889
|
+
]
|
|
890
|
+
RECURRENCE_CHOICES = ClientReportSchedule.PERIODICITY_CHOICES
|
|
891
|
+
period = forms.ChoiceField(
|
|
892
|
+
choices=PERIOD_CHOICES,
|
|
893
|
+
widget=forms.RadioSelect,
|
|
894
|
+
initial="range",
|
|
895
|
+
help_text=_("Choose how the reporting window will be calculated."),
|
|
896
|
+
)
|
|
897
|
+
start = forms.DateField(
|
|
898
|
+
label=_("Start date"),
|
|
899
|
+
required=False,
|
|
900
|
+
widget=forms.DateInput(attrs={"type": "date"}),
|
|
901
|
+
help_text=_("First day included when using a custom date range."),
|
|
902
|
+
)
|
|
903
|
+
end = forms.DateField(
|
|
904
|
+
label=_("End date"),
|
|
905
|
+
required=False,
|
|
906
|
+
widget=forms.DateInput(attrs={"type": "date"}),
|
|
907
|
+
help_text=_("Last day included when using a custom date range."),
|
|
908
|
+
)
|
|
909
|
+
week = forms.CharField(
|
|
910
|
+
label=_("Week"),
|
|
911
|
+
required=False,
|
|
912
|
+
widget=forms.TextInput(attrs={"type": "week"}),
|
|
913
|
+
help_text=_("Generates the report for the ISO week that you select."),
|
|
914
|
+
)
|
|
915
|
+
month = forms.DateField(
|
|
916
|
+
label=_("Month"),
|
|
917
|
+
required=False,
|
|
918
|
+
widget=forms.DateInput(attrs={"type": "month"}),
|
|
919
|
+
help_text=_("Generates the report for the calendar month that you select."),
|
|
920
|
+
)
|
|
921
|
+
owner = forms.ModelChoiceField(
|
|
922
|
+
queryset=get_user_model().objects.all(),
|
|
923
|
+
required=False,
|
|
924
|
+
help_text=_(
|
|
925
|
+
"Sets who owns the report schedule and is listed as the requestor."
|
|
926
|
+
),
|
|
927
|
+
)
|
|
928
|
+
destinations = forms.CharField(
|
|
929
|
+
label=_("Email destinations"),
|
|
930
|
+
required=False,
|
|
931
|
+
widget=forms.Textarea(attrs={"rows": 2}),
|
|
932
|
+
help_text=_("Separate addresses with commas or new lines."),
|
|
933
|
+
)
|
|
934
|
+
recurrence = forms.ChoiceField(
|
|
935
|
+
label=_("Recurrency"),
|
|
936
|
+
choices=RECURRENCE_CHOICES,
|
|
937
|
+
initial=ClientReportSchedule.PERIODICITY_NONE,
|
|
938
|
+
help_text=_("Defines how often the report should be generated automatically."),
|
|
939
|
+
)
|
|
940
|
+
disable_emails = forms.BooleanField(
|
|
941
|
+
label=_("Disable email delivery"),
|
|
942
|
+
required=False,
|
|
943
|
+
help_text=_("Generate files without sending emails."),
|
|
944
|
+
)
|
|
945
|
+
|
|
946
|
+
def __init__(self, *args, request=None, **kwargs):
|
|
947
|
+
self.request = request
|
|
948
|
+
super().__init__(*args, **kwargs)
|
|
949
|
+
if request and getattr(request, "user", None) and request.user.is_authenticated:
|
|
950
|
+
self.fields["owner"].initial = request.user.pk
|
|
951
|
+
|
|
952
|
+
def clean(self):
|
|
953
|
+
cleaned = super().clean()
|
|
954
|
+
period = cleaned.get("period")
|
|
955
|
+
if period == "range":
|
|
956
|
+
if not cleaned.get("start") or not cleaned.get("end"):
|
|
957
|
+
raise forms.ValidationError(_("Please provide start and end dates."))
|
|
958
|
+
elif period == "week":
|
|
959
|
+
week_str = cleaned.get("week")
|
|
960
|
+
if not week_str:
|
|
961
|
+
raise forms.ValidationError(_("Please select a week."))
|
|
962
|
+
year, week_num = week_str.split("-W")
|
|
963
|
+
start = datetime.date.fromisocalendar(int(year), int(week_num), 1)
|
|
964
|
+
cleaned["start"] = start
|
|
965
|
+
cleaned["end"] = start + datetime.timedelta(days=6)
|
|
966
|
+
elif period == "month":
|
|
967
|
+
month_dt = cleaned.get("month")
|
|
968
|
+
if not month_dt:
|
|
969
|
+
raise forms.ValidationError(_("Please select a month."))
|
|
970
|
+
start = month_dt.replace(day=1)
|
|
971
|
+
last_day = calendar.monthrange(month_dt.year, month_dt.month)[1]
|
|
972
|
+
cleaned["start"] = start
|
|
973
|
+
cleaned["end"] = month_dt.replace(day=last_day)
|
|
974
|
+
return cleaned
|
|
975
|
+
|
|
976
|
+
def clean_destinations(self):
|
|
977
|
+
raw = self.cleaned_data.get("destinations", "")
|
|
978
|
+
if not raw:
|
|
979
|
+
return []
|
|
980
|
+
validator = EmailValidator()
|
|
981
|
+
seen: set[str] = set()
|
|
982
|
+
emails: list[str] = []
|
|
983
|
+
for part in re.split(r"[\s,]+", raw):
|
|
984
|
+
candidate = part.strip()
|
|
985
|
+
if not candidate:
|
|
986
|
+
continue
|
|
987
|
+
validator(candidate)
|
|
988
|
+
key = candidate.lower()
|
|
989
|
+
if key in seen:
|
|
990
|
+
continue
|
|
991
|
+
seen.add(key)
|
|
992
|
+
emails.append(candidate)
|
|
993
|
+
return emails
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
@live_update()
|
|
997
|
+
def client_report(request):
|
|
998
|
+
form = ClientReportForm(request.POST or None, request=request)
|
|
999
|
+
report = None
|
|
1000
|
+
schedule = None
|
|
1001
|
+
if request.method == "POST":
|
|
1002
|
+
if not request.user.is_authenticated:
|
|
1003
|
+
form.is_valid() # Run validation to surface field errors alongside auth error.
|
|
1004
|
+
form.add_error(
|
|
1005
|
+
None, _("You must log in to generate client reports."),
|
|
1006
|
+
)
|
|
1007
|
+
elif form.is_valid():
|
|
1008
|
+
throttle_seconds = getattr(settings, "CLIENT_REPORT_THROTTLE_SECONDS", 60)
|
|
1009
|
+
throttle_keys = []
|
|
1010
|
+
if request.user.is_authenticated:
|
|
1011
|
+
throttle_keys.append(f"client-report:user:{request.user.pk}")
|
|
1012
|
+
remote_addr = request.META.get("HTTP_X_FORWARDED_FOR")
|
|
1013
|
+
if remote_addr:
|
|
1014
|
+
remote_addr = remote_addr.split(",")[0].strip()
|
|
1015
|
+
remote_addr = remote_addr or request.META.get("REMOTE_ADDR")
|
|
1016
|
+
if remote_addr:
|
|
1017
|
+
throttle_keys.append(f"client-report:ip:{remote_addr}")
|
|
1018
|
+
|
|
1019
|
+
added_keys = []
|
|
1020
|
+
blocked = False
|
|
1021
|
+
for key in throttle_keys:
|
|
1022
|
+
if cache.add(key, timezone.now(), throttle_seconds):
|
|
1023
|
+
added_keys.append(key)
|
|
1024
|
+
else:
|
|
1025
|
+
blocked = True
|
|
1026
|
+
break
|
|
1027
|
+
|
|
1028
|
+
if blocked:
|
|
1029
|
+
for key in added_keys:
|
|
1030
|
+
cache.delete(key)
|
|
1031
|
+
form.add_error(
|
|
1032
|
+
None,
|
|
1033
|
+
_(
|
|
1034
|
+
"Client reports can only be generated periodically. Please wait before trying again."
|
|
1035
|
+
),
|
|
1036
|
+
)
|
|
1037
|
+
else:
|
|
1038
|
+
owner = form.cleaned_data.get("owner")
|
|
1039
|
+
if not owner and request.user.is_authenticated:
|
|
1040
|
+
owner = request.user
|
|
1041
|
+
report = ClientReport.generate(
|
|
1042
|
+
form.cleaned_data["start"],
|
|
1043
|
+
form.cleaned_data["end"],
|
|
1044
|
+
owner=owner,
|
|
1045
|
+
recipients=form.cleaned_data.get("destinations"),
|
|
1046
|
+
disable_emails=form.cleaned_data.get("disable_emails", False),
|
|
1047
|
+
)
|
|
1048
|
+
report.store_local_copy()
|
|
1049
|
+
recurrence = form.cleaned_data.get("recurrence")
|
|
1050
|
+
if recurrence and recurrence != ClientReportSchedule.PERIODICITY_NONE:
|
|
1051
|
+
schedule = ClientReportSchedule.objects.create(
|
|
1052
|
+
owner=owner,
|
|
1053
|
+
created_by=request.user if request.user.is_authenticated else None,
|
|
1054
|
+
periodicity=recurrence,
|
|
1055
|
+
email_recipients=form.cleaned_data.get("destinations", []),
|
|
1056
|
+
disable_emails=form.cleaned_data.get("disable_emails", False),
|
|
1057
|
+
)
|
|
1058
|
+
report.schedule = schedule
|
|
1059
|
+
report.save(update_fields=["schedule"])
|
|
1060
|
+
messages.success(
|
|
1061
|
+
request,
|
|
1062
|
+
_(
|
|
1063
|
+
"Client report schedule created; future reports will be generated automatically."
|
|
1064
|
+
),
|
|
1065
|
+
)
|
|
1066
|
+
try:
|
|
1067
|
+
login_url = reverse("pages:login")
|
|
1068
|
+
except NoReverseMatch:
|
|
1069
|
+
try:
|
|
1070
|
+
login_url = reverse("login")
|
|
1071
|
+
except NoReverseMatch:
|
|
1072
|
+
login_url = getattr(settings, "LOGIN_URL", None)
|
|
1073
|
+
|
|
1074
|
+
context = {
|
|
1075
|
+
"form": form,
|
|
1076
|
+
"report": report,
|
|
1077
|
+
"schedule": schedule,
|
|
1078
|
+
"login_url": login_url,
|
|
1079
|
+
}
|
|
1080
|
+
return render(request, "pages/client_report.html", context)
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
@require_POST
|
|
1084
|
+
def submit_user_story(request):
|
|
1085
|
+
data = request.POST.copy()
|
|
1086
|
+
if request.user.is_authenticated and not data.get("name"):
|
|
1087
|
+
data["name"] = request.user.get_username()[:40]
|
|
1088
|
+
if not data.get("path"):
|
|
1089
|
+
data["path"] = request.get_full_path()
|
|
1090
|
+
|
|
1091
|
+
form = UserStoryForm(data)
|
|
1092
|
+
if request.user.is_authenticated:
|
|
1093
|
+
form.instance.user = request.user
|
|
1094
|
+
|
|
1095
|
+
if form.is_valid():
|
|
1096
|
+
story = form.save(commit=False)
|
|
1097
|
+
if request.user.is_authenticated:
|
|
1098
|
+
story.user = request.user
|
|
1099
|
+
story.owner = request.user
|
|
1100
|
+
if not story.name:
|
|
1101
|
+
story.name = request.user.get_username()[:40]
|
|
1102
|
+
if not story.name:
|
|
1103
|
+
story.name = str(_("Anonymous"))[:40]
|
|
1104
|
+
story.path = (story.path or request.get_full_path())[:500]
|
|
1105
|
+
story.is_user_data = True
|
|
1106
|
+
story.save()
|
|
1107
|
+
return JsonResponse({"success": True})
|
|
1108
|
+
|
|
1109
|
+
return JsonResponse({"success": False, "errors": form.errors}, status=400)
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
def csrf_failure(request, reason=""):
|
|
1113
|
+
"""Custom CSRF failure view with a friendly message."""
|
|
1114
|
+
logger.warning("CSRF failure on %s: %s", request.path, reason)
|
|
1115
|
+
return render(request, "pages/csrf_failure.html", status=403)
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
def _admin_context(request):
|
|
1119
|
+
context = admin.site.each_context(request)
|
|
1120
|
+
if not context.get("has_permission"):
|
|
1121
|
+
rf = RequestFactory()
|
|
1122
|
+
mock_request = rf.get(request.path)
|
|
1123
|
+
mock_request.user = SimpleNamespace(
|
|
1124
|
+
is_active=True,
|
|
1125
|
+
is_staff=True,
|
|
1126
|
+
is_superuser=True,
|
|
1127
|
+
has_perm=lambda perm, obj=None: True,
|
|
1128
|
+
has_module_perms=lambda app_label: True,
|
|
1129
|
+
)
|
|
1130
|
+
context["available_apps"] = admin.site.get_app_list(mock_request)
|
|
1131
|
+
context["has_permission"] = True
|
|
1132
|
+
return context
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
def admin_manual_list(request):
|
|
1136
|
+
manuals = UserManual.objects.order_by("title")
|
|
1137
|
+
context = _admin_context(request)
|
|
1138
|
+
context["manuals"] = manuals
|
|
1139
|
+
return render(request, "admin_doc/manuals.html", context)
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
def admin_manual_detail(request, slug):
|
|
1143
|
+
manual = get_object_or_404(UserManual, slug=slug)
|
|
1144
|
+
context = _admin_context(request)
|
|
1145
|
+
context["manual"] = manual
|
|
1146
|
+
return render(request, "admin_doc/manual_detail.html", context)
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
def manual_pdf(request, slug):
|
|
1150
|
+
manual = get_object_or_404(UserManual, slug=slug)
|
|
1151
|
+
pdf_data = base64.b64decode(manual.content_pdf)
|
|
1152
|
+
response = HttpResponse(pdf_data, content_type="application/pdf")
|
|
1153
|
+
response["Content-Disposition"] = f'attachment; filename="{manual.slug}.pdf"'
|
|
1154
|
+
return response
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
@landing(_("Manuals"))
|
|
1158
|
+
def manual_list(request):
|
|
1159
|
+
manuals = UserManual.objects.order_by("title")
|
|
1160
|
+
return render(request, "pages/manual_list.html", {"manuals": manuals})
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
def manual_detail(request, slug):
|
|
1164
|
+
manual = get_object_or_404(UserManual, slug=slug)
|
|
1165
|
+
return render(request, "pages/manual_detail.html", {"manual": manual})
|