-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
912 lines (768 loc) · 27.6 KB
/
main.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
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
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
import json
import os
import random
import re
import string
import sys
import time
import traceback
from datetime import datetime, timezone
from io import BytesIO
import requests
import yaml
from flask import (
Flask,
Response,
abort,
redirect,
render_template,
request,
send_file,
send_from_directory,
session,
url_for,
)
from flask_login import (
LoginManager,
UserMixin,
current_user,
login_required,
login_user,
logout_user,
)
from flask_qrcode import QRcode
from git import Repo
base_path = os.path.dirname(os.path.realpath(__file__))
# GITHUB REPO INFO
repo = Repo.init(base_path)
active_branch = repo.active_branch
current_commit = repo.head.commit
current_commit_datetime = repo.head.commit.committed_datetime
current_commit_message = repo.head.commit.message
dirty_repo = repo.is_dirty()
# GITHUB REPO COMMITS
r = requests.get("https://api.github.com/repos/danricho/where_the/commits")
GHrepo = r.json()
GH_head_datetime = datetime.strptime(
GHrepo[0]["commit"]["author"]["date"], "%Y-%m-%dT%H:%M:%SZ"
).replace(tzinfo=timezone.utc)
# COMPARE LOCAL TO GITHUB
if str(GHrepo[0]["sha"]) == str(current_commit):
if dirty_repo:
local_GH = ("Same version as GitHub", True)
else:
local_GH = ("Same version as GitHub", False)
elif GH_head_datetime > current_commit_datetime:
if dirty_repo:
local_GH = ("Newer version on GitHub", True)
else:
local_GH = ("Newer version available on GitHub", False)
else:
if dirty_repo:
local_GH = ("Local version is newer than GitHub", True)
else:
local_GH = ("Local version is newer than GitHub", False)
git_revision = {
"hash": f"{current_commit}"[:7],
"timestamp": current_commit_datetime.strftime("%Y-%m-%d %H:%M:%S"),
"subject": current_commit_message,
"modified": dirty_repo,
"local_GH": local_GH,
}
############################################################
# LOADING CONFIG FROM YAML FILE #
############################################################
def load_config():
global config
with open(base_path + "/config.yml", "r") as f:
config = yaml.safe_load(f)
def save_config():
with open(base_path + "/config.yml", "w") as f:
yaml.dump(config, f, default_flow_style=False, allow_unicode=True)
def to_rgba(col_string):
def clamp(value, min_value, max_value):
return max(min_value, min(max_value, value))
def saturate(value):
return clamp(value, 0.0, 1.0)
def hue_to_rgb(h):
r = abs(h * 6.0 - 3.0) - 1.0
g = 2.0 - abs(h * 6.0 - 2.0)
b = 2.0 - abs(h * 6.0 - 4.0)
return saturate(r), saturate(g), saturate(b)
def hsl_to_rgb(h, s, l):
r, g, b = hue_to_rgb(h)
c = (1.0 - abs(2.0 * l - 1.0)) * s
r = (r - 0.5) * c + l
g = (g - 0.5) * c + l
b = (b - 0.5) * c + l
return r, g, b
col_string = col_string.replace(" ", "").strip().lower()
m = re.match(
r"^(#[0-9a-f]{3}|(#)(?:[0-9a-f]{2}){2,4}|(rgb|hsl)(a?)\(([^)]+)\))$", col_string
)
if m.group(1)[0] == "#":
s = m.group(1).lstrip("#")
if len(s) == 3:
r = int(s[0] + s[0], 16)
g = int(s[1] + s[1], 16)
b = int(s[2] + s[2], 16)
a = 1.0
elif len(s) == 6:
r = int(s[0:2], 16)
g = int(s[2:4], 16)
b = int(s[4:6], 16)
a = 1.0
elif len(s) == 8:
r = int(s[0:2], 16)
g = int(s[2:4], 16)
b = int(s[4:6], 16)
a = round(int(s[6:8], 16) / 255.0, 2)
return {"r": r, "g": g, "b": b, "a": a}
elif m.group(3) == "rgb":
if m.group(4) == "a":
r, g, b, a = m.group(5).split(",")
else:
a = 1.0
r, g, b = m.group(5).split(",")
if r[-1] == "%":
r = round(float(r.rstrip("%")) * 2.55)
else:
r = int(r)
if g[-1] == "%":
g = round(float(g.rstrip("%")) * 2.55)
else:
g = int(g)
if b[-1] == "%":
b = round(float(b.rstrip("%")) * 2.55)
else:
b = int(b)
a = float(a)
return {"r": r, "g": g, "b": b, "a": a}
elif m.group(3) == "hsl":
if m.group(4) == "a":
h, s, l, a = m.group(5).split(",")
else:
a = 1.0
h, s, l = m.group(5).split(",")
h = int(h) / 360.0
s = int(s.rstrip("%")) / 100.0
l = int(l.rstrip("%")) / 100.0
r, g, b = hsl_to_rgb(h, s, l)
a = float(a)
return {
"r": round(r * 255),
"g": round(g * 255),
"b": round(b * 255),
"a": round(a, 2),
}
print("NOTHING")
return m.group(3, 4, 5)
config = {}
load_config()
# THIS SECTION IS TO UPDATE PREVIOUS VERSIONS OF CONFIG.YML
config["PRIMARY-COLOR"] = config.get("PRIMARY-COLOR", "#C0A890")
config["PRINT_TEMPLATE"] = config.get(
"PRINT_TEMPLATE",
{
"ADD_DESCRIPTION_TO_LABEL": True,
"ADD_LOGO_TO_QR": True,
"DESCRIPTION_FONT_SIZE": "0.35cm",
"IDENTIFIER": "AVERY L7164",
"ID_FONT_SIZE": "0.8cm",
"LABEL": {
"HEIGHT": "7.2cm",
"WIDTH": "6.353cm",
"X_GUTTER": "0.25cm",
"X_PADDING": "0.25cm",
"Y_GUTTER": "0cm",
"Y_PADDING": "0.1cm",
},
"LABELS_PER_PAGE": 12,
"PAGE": {"WIDTH": "21cm", "X_MARGIN": "0.7214cm", "Y_MARGIN": "0.457cm"},
"QR_HEIGHT": "4.3cm",
},
)
if config.get("DISABLE-QR-LOGO", None) != None: # moving into PRINT_TEMPLATE
config["PRINT_TEMPLATE"]["ADD_LOGO_TO_QR"] = not config.get("DISABLE-QR-LOGO")
del config["DISABLE-QR-LOGO"]
if config.get("ADD-DESCRIPTION-TO-QR", None) != None: # moving into PRINT_TEMPLATE
config["PRINT_TEMPLATE"]["ADD-DESCRIPTION-TO-LABEL"] = config.get(
"ADD-DESCRIPTION-TO-QR"
)
del config["ADD-DESCRIPTION-TO-QR"]
if config["USERS"].get("default", None) == None:
config["USERS"]["default"] = {}
if config["PRINT_TEMPLATE"].get("COLORED_BACKGROUNDS", None) == None:
config["PRINT_TEMPLATE"]["COLORED_BACKGROUNDS"] = True
save_config()
############################################################
# INITIALISE FLASK APP #
############################################################
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = config["FLASK"]["TEMPLATES_AUTO_RELOAD"]
app.config["SECRET_KEY"] = config["FLASK"]["SECRET"]
QRcode(app)
login_manager = LoginManager()
login_manager.init_app(app)
app.config["LOGIN_DISABLED"] = config["AUTHENTICATION"] not in [
"FLASK-LOGIN",
"AUTHELIA",
]
############################################################
# LOAD INITIAL DATA #
############################################################
# TODO: ADD OPTION FOR TINYDB / YAML / JSON STORAGE
locs = {}
def load_json():
global locs
try:
with open(base_path + "/data.json", "r") as openfile:
locs = json.load(openfile)
except:
print("No data file... creating blank")
save_json()
daily_backup()
# backup will only occur if the site is being used...
def daily_backup():
max_backups = 14
backup_datetime_format = "%Y-%m-%d"
backups_directory = "data_bkp/"
today_bkp_filename = (
f"{datetime.now().strftime(backup_datetime_format)}_data.bkp.json"
)
if not os.path.exists(f"{base_path}/{backups_directory}{today_bkp_filename}"):
with open(
f"{base_path}/{backups_directory}{today_bkp_filename}", "w"
) as outfile:
outfile.write(json.dumps(locs, indent=4))
files = sorted(
[f for f in os.listdir(f"{base_path}/{backups_directory}") if "bkp" in f]
)
while len(files) > max_backups:
os.remove(f"{base_path}/{backups_directory}{files[0]}")
files = sorted(
[f for f in os.listdir(f"{base_path}/{backups_directory}") if "bkp" in f]
)
def save_json():
for loc in locs:
locs[loc]["items"] = [x.strip() for x in locs[loc]["items"]]
with open(base_path + "/data.json", "w") as outfile:
outfile.write(json.dumps(locs, indent=4))
load_json()
############################################################
# FUNCTION TO CREATE NEW LOCATION #
############################################################
def new_loc():
global locs
load_json()
def create_id():
loc_id = random.choice(string.ascii_letters)
loc_id += random.choice(string.ascii_letters)
loc_id += random.choice(string.ascii_letters)
loc_id += str(random.randint(0, 9))
loc_id += str(random.randint(0, 9))
loc_id += str(random.randint(0, 9))
return loc_id.upper()
loc_id = None
while loc_id == None or loc_id in locs:
loc_id = create_id()
loc_info = {
"id": loc_id,
"description": "",
"type": "",
"location": "",
"items": [],
"fullness": 0,
"last_change": time.time(),
}
locs[loc_id] = loc_info
print(f"Created location: {loc_id}")
save_json()
return loc_id
############################################################
# FUNCTION TO TEXT SEARCH ITEMS #
############################################################
def search_locs(term, mode="any"):
global locs
load_json()
results = {}
term = term.upper().strip()
if mode in ["any", "all"]:
term = re.sub(r"[^a-zA-Z0-9 ]+", " ", term, flags=re.IGNORECASE)
term = term.split()
for loc in list(locs.values()):
this_id = loc["id"]
if mode in ["any", "all"]:
word_pool = str(list(loc.values()))
word_pool = re.sub(r"[^a-zA-Z0-9 ]+", " ", word_pool, flags=re.IGNORECASE)
word_pool = " ".join(word_pool.split()).upper()
if mode == "any":
if any(x in word_pool for x in term):
if this_id not in results:
results[this_id] = dict(loc)
results[this_id]["items"] = []
matching_items = [
item for item in loc["items"] if any(x in item.upper() for x in term)
]
if len(matching_items):
if this_id not in results:
results[this_id] = dict(loc)
results[this_id]["items"] = []
results[this_id]["items"] = matching_items
elif mode == "all":
if all(x in word_pool for x in term):
if this_id not in results:
results[this_id] = dict(loc)
results[this_id]["items"] = []
matching_items = [
item for item in loc["items"] if any(x in item.upper() for x in term)
]
if len(matching_items):
if this_id not in results:
results[this_id] = dict(loc)
results[this_id]["items"] = []
results[this_id]["items"] = matching_items
elif mode == "re":
word_pool = str(list(loc.values())).upper()
if re.search(term, word_pool):
results[this_id] = dict(loc)
results[this_id]["items"] = []
for item in loc["items"]:
if re.search(term, item.upper()):
if this_id not in results:
results[this_id] = dict(loc)
results[this_id]["items"] = []
results[this_id]["items"].append(item)
return {"search": (mode, term), "results": list(results.values())}
############################################################
# EXCEPTION FOR FLASK REQUESTS WITH NOT MATCHING LOCATION #
############################################################
class locationIdNotFound(Exception):
"""Exception raised when ID not found in location data."""
def __init__(self):
self.code = 500
self.name = "Location Not Found"
self.description = "The location ID was not found in the location data."
super().__init__(self.description)
############################################################
# SET UP USER FOR FLASK-LOGIN #
############################################################
def props(x):
return dict((key, getattr(x, key)) for key in dir(x) if key not in dir(x.__class__))
class User(UserMixin):
def __init__(self, username, password, email=None):
self.username = username
self.password = password
self.email = email
self.id = username
users = []
for username in config["USERS"]:
users.append(
User(
username,
config["USERS"][username].get("password", "UNSET"),
config["USERS"][username].get("email", None),
)
)
sort_descriptions = [
"Location",
"Fullness",
"Description",
"Last Updated",
"Type",
"ID",
]
############################################################
# DECORATED FUNCTIONS FOR FLASK-LOGIN #
############################################################
# Loads a user from the active session if it exists.
# Without this you would have to keep logging in.
@login_manager.user_loader
def load_user(username):
global config
if config["AUTHENTICATION"] == "FLASK-LOGIN":
for user in users:
if user.username == username.lower():
user.name = user.username
user.type = "FLASK-LOGIN"
return user
if config["AUTHENTICATION"] == "AUTHELIA":
try:
username = request.headers.get("Remote-User").lower()
email = request.headers.get("Remote-Email").lower()
name = request.headers.get("Remote-Name").lower()
for user in users:
if user.username == username.lower():
user.name = user.username
user.type = "AUTHELIA"
return user
# AUTHELIA USER WASN'T IN CONFIG... ADD
load_config()
config["USERS"][username] = {"password": "AUTHELIA", "email": email}
save_config()
users.append(User(username, "AUTHELIA", email))
users[-1].name = name
users[-1].type = "AUTHELIA"
return users[-1]
except:
return None
return None # no user found!
# Send un-logged-in users to the login page
@login_manager.unauthorized_handler
def unauthorized():
return redirect(url_for("login"))
# Serve the login form or process the authorisation on login submission
# TODO: Redirect to original page after login
@app.route(config["SITE"]["PATH_PREFIX"] + "/login", methods=["GET", "POST"])
def login():
if config["AUTHENTICATION"] not in ["FLASK-LOGIN", "AUTHELIA"]:
return redirect(url_for("home"))
if config["AUTHENTICATION"] == "FLASK-LOGIN":
if request.method == "POST":
username = request.form.get("username", None)
password = request.form.get("password", None)
for user in users:
if (
username.lower() == user.username.lower()
and password == user.password
):
user.type = "FLASK-LOGIN"
login_user(user)
return redirect(url_for("home"))
return abort(401)
else:
return render_template("login.j2.html")
if config["AUTHENTICATION"] == "AUTHELIA":
try:
username = request.headers.get("Remote-User").lower()
email = request.headers.get("Remote-Email").lower()
name = request.headers.get("Remote-Name").lower()
for user in users:
if user.username == username.lower():
user.type = "AUTHELIA"
login_user(user)
return redirect(url_for("home"))
# AUTHELIA USER WASN'T IN CONFIG... ADD
load_config()
config["USERS"][username] = {"password": "AUTHELIA", "email": email}
save_config()
users.append(User(username, "AUTHELIA", email))
users[-1].name = name
users[-1].type = "AUTHELIA"
return redirect(url_for("home"))
except:
return abort(401)
return abort(401)
# URL to log out
@app.route(config["SITE"]["PATH_PREFIX"] + "/logout")
def logout():
logout_user()
return redirect(url_for("login"))
############################################################
# DECORATED FUNCTIONS FLASK ROUTING AND PAGE SERVING #
############################################################
# home page
@app.route(config["SITE"]["PATH_PREFIX"] + "/")
@login_required
def home():
return redirect(url_for("list_locs"))
# list locations
@app.route(config["SITE"]["PATH_PREFIX"] + "/list")
@login_required
def list_locs():
load_json()
# THIS IS FOR WHEN AUTH IS NO_AUTH.
if current_user.is_anonymous:
current_user.username = "DEFAULT"
# SORTING
if config["AUTHENTICATION"] == "NO-AUTH":
username = "DEFAULT"
sorting = 0 # default sort to location
load_config()
for user in config["USERS"]:
if current_user.username.lower() == user.lower():
sorting = config["USERS"][user].get("LOCATION_SORTING", 0)
# print(list(locs.values())[0])
if sort_descriptions[sorting] == "Location":
sorted_locs = list(
sorted(locs.values(), key=lambda i: (i["location"], i["description"]))
)
# print([(i['location'], i['description']) for i in sorted_locs])
elif sort_descriptions[sorting] == "ID":
sorted_locs = list(sorted(locs.values(), key=lambda i: (i["id"])))
# print([(i['id']) for i in sorted_locs])
elif sort_descriptions[sorting] == "Fullness":
sorted_locs = list(
sorted(locs.values(), key=lambda i: (-i["fullness"], i["description"]))
)
# print([(i['fullness'], i['description']) for i in sorted_locs])
elif sort_descriptions[sorting] == "Description":
sorted_locs = list(sorted(locs.values(), key=lambda i: (i["description"])))
# print([(i['description']) for i in sorted_locs])
elif sort_descriptions[sorting] == "Last Updated":
sorted_locs = list(
sorted(locs.values(), key=lambda i: (i["last_change"]), reverse=True)
)
# print([(i['last_change']) for i in sorted_locs])
elif sort_descriptions[sorting] == "Type":
sorted_locs = list(sorted(locs.values(), key=lambda i: (i["type"])))
# print([(i['last_change']) for i in sorted_locs])
# PAGINATION
items_per_page = 10
for user in config["USERS"]:
if current_user.username.lower() == user.lower():
items_per_page = config["USERS"][user].get(
"LOCATIONS_PER_PAGINATED_PAGE", 10
)
page = request.args.get("page", 1, type=int)
page = max(page, 1) # has to be minimum 1
pages = round(len(sorted_locs) / items_per_page + 0.499)
page = min(page, pages) # has to be at most the last page
from_page = int(page) * items_per_page - items_per_page
upto_page = int(page) * items_per_page
upto_page = min(upto_page, len(sorted_locs))
pagination_info = {
"pages": pages,
"current_page": page,
"first": from_page,
"last": upto_page,
"total": len(sorted_locs),
"per_page": items_per_page,
}
sorted_locs = sorted_locs[from_page:upto_page]
# Friendlier Timestamps
for loc in sorted_locs:
loc["last_change"] = datetime.fromtimestamp(loc["last_change"]).strftime(
"%-I:%M %p, %d %B %Y"
)
return render_template(
"list.j2.html",
locs=sorted_locs,
sorted_str=sort_descriptions[sorting],
SITE=config["SITE"],
pagination=pagination_info,
)
@app.route(config["SITE"]["PATH_PREFIX"] + "/search", methods=["GET", "POST"])
@login_required
def search():
if request.method == "GET": # FORM ONLY
return render_template("search.j2.html", form_data={}, results=None)
else: # SEARCH FUNCTION
submit_data = dict(request.form)
return render_template(
"search.j2.html",
form_data=submit_data,
results=search_locs(
submit_data["search-input"], mode=submit_data["search-mode-select"]
),
)
@app.route(
config["SITE"]["PATH_PREFIX"]
+ "/set-setting/<string:setting>/<string:value>/<string:next_endpoint>"
)
@login_required
def set_setting(setting, value, next_endpoint):
load_config()
global config
# THIS IS FOR WHEN AUTH IS NO_AUTH.
if current_user.is_anonymous:
current_user.username = "DEFAULT"
for user in config["USERS"]:
if current_user.username.lower() == user.lower():
if setting == "sort":
config["USERS"][user]["LOCATION_SORTING"] = sort_descriptions.index(
value
)
save_config()
if setting == "per_page":
config["USERS"][user]["LOCATIONS_PER_PAGINATED_PAGE"] = int(value)
save_config()
return redirect(url_for(next_endpoint))
@app.route(config["SITE"]["PATH_PREFIX"] + "/scan/<string:url_id>")
@login_required
def scanned(url_id):
load_json()
if url_id.upper() in locs:
return redirect(url_for("view", url_id=url_id))
else:
raise locationIdNotFound()
# view a location page
@app.route(config["SITE"]["PATH_PREFIX"] + "/view/<string:url_id>")
@login_required
def view(url_id):
load_json()
from_search = False
if request.referrer:
from_search = config["SITE"]["PATH_PREFIX"] + "/search" in request.referrer
if url_id.upper() in locs:
locs[url_id.upper()]["last_change"] = datetime.fromtimestamp(
locs[url_id.upper()]["last_change"]
).strftime("%-I:%M %p, %d %B %Y")
return render_template(
"view.j2.html", loc=locs[url_id.upper()], from_search=from_search
)
else:
raise locationIdNotFound()
# print qr code for location page
@app.route(config["SITE"]["PATH_PREFIX"] + "/print/<string:url_id>")
@login_required
def print_qr(url_id):
load_config()
load_json()
if url_id.upper() in locs:
return render_template(
"print.j2.html",
locs=[locs[url_id.upper()]],
SITE=config["SITE"],
batch=False,
template=config["PRINT_TEMPLATE"],
)
else:
raise locationIdNotFound()
# print qr code for location page
@app.route(config["SITE"]["PATH_PREFIX"] + "/batch-print/")
@login_required
def print_batch():
load_config()
load_json()
return render_template(
"print.j2.html",
locs=locs.values(),
SITE=config["SITE"],
batch=True,
template=config["PRINT_TEMPLATE"],
)
# edit location page (serve form and process submission)
@app.route(
config["SITE"]["PATH_PREFIX"] + "/edit/<string:url_id>", methods=["GET", "POST"]
)
@login_required
def edit(url_id):
global locs
load_json()
if request.method == "POST":
save_data = dict(request.form)
save_data["items-list"] = json.loads(save_data["items-list"])
save_data["fullness-input"] = int(save_data["fullness-input"])
save_data["sealed"] = {"on": True, "off": False}[save_data.get("sealed", "off")]
del save_data["add-item-input"]
# print(locs[url_id])
print(save_data)
for key in save_data:
locs[url_id][key.replace("-input", "").replace("-list", "")] = save_data[
key
]
locs[url_id]["last_change"] = time.time()
save_json()
return redirect(url_for("view", url_id=url_id))
else:
if url_id.upper() in locs:
return render_template("edit.j2.html", loc=locs[url_id.upper()])
else:
raise locationIdNotFound()
# create a new location page (redirects to edit once ID generated)
@app.route(config["SITE"]["PATH_PREFIX"] + "/create", methods=["GET", "POST"])
@login_required
def create():
return redirect(url_for("edit", url_id=new_loc()))
# delete location URL (redirect home if successful)
@app.route(config["SITE"]["PATH_PREFIX"] + "/delete/<string:url_id>")
@login_required
def delete(url_id):
global locs
load_json()
if url_id.upper() in locs:
del locs[url_id.upper()]
save_json()
return redirect(url_for("home"))
else:
raise locationIdNotFound()
@app.route(config["SITE"]["PATH_PREFIX"] + "/export/")
@login_required
def export_csv():
load_json()
dataCSV = "ID,Type,Description,Location,Destination,Sealed\n"
for item in locs.values():
dataCSV += f"{item['id']},{item['type']},{item['description']},{item['location']},{item.get('destination','')},{item.get('sealed','')}\n"
return send_file(
BytesIO(dataCSV.encode()), mimetype="text/csv", download_name="export.csv"
)
# serve static files
@app.route(config["SITE"]["PATH_PREFIX"] + "/static/<path:path>")
def send_static(path):
if config["SITE"]["PATH_PREFIX"] != "":
if path in ["icons/browserconfig.xml", "icons/site.webmanifest"]:
with open(base_path + f"/static/{path}", "r") as f:
if path in ["icons/browserconfig.xml"]:
return Response(
f.read().replace(
'"/static', f'"{config["SITE"]["PATH_PREFIX"]}/static'
),
mimetype="text/xml",
)
if path in ["icons/site.webmanifest"]:
return Response(
f.read().replace(
'"/static', f'"{config["SITE"]["PATH_PREFIX"]}/static'
),
mimetype="application/manifest+json",
)
return send_from_directory("static", path)
# error page generator - comment decortaor to see errors in terminal
@app.errorhandler(Exception)
def handle_error(e):
error_info = {}
try:
error_info["code"] = e.code
except:
pass
try:
error_info["name"] = e.name
except:
pass
try:
error_info["description"] = e.description
except:
pass
if error_info == {}:
etype, value, tb = sys.exc_info()
try:
error_info["code"] = etype.__name__
except:
pass
try:
error_info["name"] = value
except:
pass
try:
error_info["description"] = traceback.format_exc().replace("\n", "<br/>")
except:
pass
return render_template("error.j2.html", error=error_info)
# DATA PROVIDED TO ALL TEMPLATES
@app.context_processor
def inject_data():
return {
"git_revision": git_revision,
"PRIMARY_COLOR": config["PRIMARY-COLOR"],
"PRIMARY_COLOR_RGB": to_rgba(config["PRIMARY-COLOR"]),
"request_info": request.headers,
"user": props(current_user),
"path": request.path,
"SITE": config["SITE"],
"STATS": {
"LOCS": len(locs.values()),
"ITEMS": len(sum([loc["items"] for loc in locs.values()], [])),
},
"AUTHELIA_URL": config.get("AUTHELIA_URL", None),
}
############################################################
# START THE FLASK APP! #
############################################################
if __name__ == "__main__":
app.run(
host=config["FLASK"]["HOST"],
port=config["FLASK"]["PORT"],
use_reloader=config["FLASK"]["USE_RELOADER"],
debug=config["FLASK"]["DEBUG"],
)