]> Softwares of Agnibho - librevax.git/blob - librevax.py
Vaccine Availability
[librevax.git] / librevax.py
1 # LibreVax
2 # Copyright (C) 2024 Dr. Agnibho Mondal
3 # This file is part of LibreVax.
4 # LibreVax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5 # LibreVax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6 # You should have received a copy of the GNU General Public License along with LibreVax. If not, see <https://www.gnu.org/licenses/>.
7
8 from flask import Flask, render_template, request, session, redirect, g
9 from urllib.parse import urlencode
10 from datetime import datetime
11 import json, sqlite3
12 import advice, auth, inventory, multicenter, patient, personnel, transaction, vaccination
13
14 app=Flask(__name__)
15 app.config.from_file("config/config.json", load=json.load)
16
17 @app.before_request
18 def preload():
19 g.user=session.get("user", None)
20 g.mid=session.get("mid", None)
21 g.center=session.get("center", None)
22 g.enable_delete=app.config.get("ENABLE_DELETE", False)
23
24 @app.route("/")
25 def index():
26 try:
27 if((ret:=problem())!="go"):
28 return ret
29 cursor=get_db().cursor()
30 (ok, mc)=multicenter.read(cursor, session["mid"])
31 if(not ok):
32 raise exception(mc)
33 (ok, pat)=patient.list(cursor)
34 if(not ok):
35 raise exception(pat)
36 return render_template("index.html", center=mc, patient=pat)
37 except Exception as e:
38 raise(e)
39 return render_template("error.html", data=e)
40
41 @app.get("/login")
42 def login_get(error=False):
43 try:
44 cursor=get_db().cursor()
45 (ok, data)=multicenter.list(cursor)
46 if(not ok):
47 raise exception(data)
48 if(len(data)<=0):
49 data.append("")
50 return render_template("login.html", data=data, error=error)
51 except Exception as e:
52 return render_template("error.html", data=e)
53
54 @app.post("/login")
55 def login_post():
56 cursor=get_db().cursor()
57 if(auth.login(cursor, request.form["user"], request.form["password"], request.form["center"])):
58 return redirect("/")
59 else:
60 return login_get(True)
61
62 @app.route("/logout")
63 def logout():
64 auth.logout()
65 return redirect("/login")
66
67 @app.get("/admin")
68 def admin(msg=None):
69 cursor=get_db().cursor()
70 (ok, mc)=multicenter.list(cursor)
71 if(not ok):
72 raise Exception(mc)
73 users=auth.list(cursor)
74 return render_template("admin.html", users=users, center=mc, msg=msg)
75
76 @app.post("/newuser")
77 def newuser():
78 try:
79 cursor=get_db().cursor()
80 if(auth.new(cursor, request.form["user"], request.form["pwd"])):
81 return admin(msg="New user added")
82 else:
83 return admin(msg="Failed to add")
84 except Exception as e:
85 raise(e)
86 return render_template("error.html", data=e)
87
88 @app.post("/changepass")
89 def changepass():
90 try:
91 cursor=get_db().cursor()
92 if("user" in request.form.keys()):
93 if(auth.change(cursor, request.form["user"], request.form["new"])):
94 return admin(msg="Password changed")
95 else:
96 return admin(msg="Incorrect Password")
97 elif(request.form["new"]==request.form["check"]):
98 user=request.form.get("user", session["user"])
99 if(auth.change(cursor, user, request.form["new"], request.form["old"])):
100 return admin(msg="Password changed")
101 else:
102 return admin(msg="Incorrect Password")
103 else:
104 return admin(msg="Failed to change password")
105 except Exception as e:
106 raise(e)
107 return render_template("error.html", data=e)
108
109 @app.post("/switchcenter")
110 def switchcenter():
111 try:
112 cursor=get_db().cursor()
113 if(auth.switch(cursor, request.form)):
114 return admin(msg="Switched center to "+g.center)
115 else:
116 raise Exception("Failed to switch")
117 except Exception as e:
118 raise(e)
119 return render_template("error.html", data=e)
120
121 @app.get("/center/<action>")
122 @app.get("/center/<action>/<mid>")
123 def center_view(action, mid=None):
124 if((ret:=problem())!="go"):
125 return ret
126 try:
127 cursor=get_db().cursor()
128 if(action=="list"):
129 (ok, data)=multicenter.list(cursor)
130 if(not ok):
131 raise exception(data)
132 return render_template("multicenter-view.html", data=data, action=action)
133 elif(action=="view"):
134 if(mid is not None):
135 (ok, data)=multicenter.read(cursor, mid)
136 if(not ok):
137 raise Exception(data)
138 (ok, invt)=inventory.list(cursor, mid)
139 if(not ok):
140 raise Exception(invt)
141 (ok, pers)=personnel.list(cursor, mid)
142 if(not ok):
143 raise Exception(pers)
144 return render_template("multicenter-view.html", data=data, inventory=invt, personnel=pers, action=action)
145 else:
146 raise Exception("Invalid parameter")
147 elif(action=="edit"):
148 if(mid is not None):
149 (ok, data)=multicenter.read(cursor, mid)
150 if(not ok):
151 raise Exception(data)
152 return render_template("multicenter-edit.html", data=data, action=action)
153 else:
154 raise Exception("Invalid parameter")
155 elif(action=="new"):
156 return render_template("multicenter-edit.html", data=[], action=action)
157 except Exception as e:
158 raise(e)
159 return render_template("error.html", data=e)
160
161 @app.get("/personnel/<action>")
162 @app.get("/personnel/<action>/<sid>")
163 def personnel_view(action, sid=None):
164 if((ret:=problem())!="go"):
165 return ret
166 try:
167 cursor=get_db().cursor()
168 if(action=="list"):
169 (ok, data)=personnel.list(cursor, g.mid)
170 if(not ok):
171 raise exception(data)
172 return render_template("personnel-view.html", data=data, action=action)
173 elif(action=="view"):
174 if(sid is not None):
175 (ok, data)=personnel.read(cursor, sid)
176 if(not ok):
177 raise Exception(data)
178 return render_template("personnel-view.html", data=data, action=action)
179 else:
180 raise Exception("Invalid parameter")
181 elif(action=="edit"):
182 if(sid is not None):
183 (ok, data)=personnel.read(cursor, sid)
184 if(not ok):
185 raise Exception(data)
186 (ok, center)=multicenter.list(cursor)
187 if(not ok):
188 raise Exception(center)
189 return render_template("personnel-edit.html", data=data, center=center, action=action)
190 else:
191 raise Exception("Invalid parameter")
192 elif(action=="new"):
193 (ok, center)=multicenter.list(cursor)
194 if(not ok):
195 raise Exception(center)
196 return render_template("personnel-edit.html", data=[], center=center, action=action)
197 except Exception as e:
198 raise(e)
199 return render_template("error.html", data=e)
200
201 @app.post("/personnel/new")
202 @app.post("/personnel/edit/<sid>")
203 def personnel_edit(sid=None):
204 if((ret:=problem())!="go"):
205 return ret
206 try:
207 cursor=get_db().cursor()
208 if(sid is None):
209 active="active" in request.form.keys()
210 (ok, data)=personnel.create(cursor, request.form, active)
211 if(not ok):
212 raise Exception(data)
213 sid=data
214 else:
215 active="active" in request.form.keys()
216 (ok, data)=personnel.update(cursor, sid, request.form, active)
217 if(not ok):
218 raise Exception(data)
219 return redirect("/personnel/view/"+str(sid))
220 except Exception as e:
221 return render_template("error.html", data=e)
222
223 @app.post("/center/new")
224 @app.post("/center/edit/<mid>")
225 def center_edit(mid=None):
226 if((ret:=problem())!="go"):
227 return ret
228 try:
229 cursor=get_db().cursor()
230 if(mid is None):
231 (ok, data)=multicenter.create(cursor, request.form)
232 if(not ok):
233 raise Exception(data)
234 mid=data
235 else:
236 (ok, data)=multicenter.update(cursor, mid, request.form)
237 if(not ok):
238 raise Exception(data)
239 return redirect("/center/view/"+str(mid))
240 except Exception as e:
241 return render_template("error.html", data=e)
242
243 @app.get("/inventory/<action>")
244 @app.get("/inventory/<action>/<id>")
245 def inventory_view(action, id=None):
246 if((ret:=problem())!="go"):
247 return ret
248 try:
249 cursor=get_db().cursor()
250 if(action=="list"):
251 if(id is not None):
252 (ok, data)=inventory.list(cursor, id, all=True)
253 if(not ok):
254 raise exception(data)
255 (ok, center)=multicenter.read(cursor, id)
256 if(not ok):
257 raise exception(center)
258 return render_template("inventory-view.html", data=data, center=center, action=action)
259 else:
260 raise Exception("Invalid parameter")
261 elif(action=="view"):
262 if(id is not None):
263 (ok, data)=inventory.read(cursor, id)
264 if(not ok):
265 raise Exception(data)
266 return render_template("inventory-view.html", data=data, action=action)
267 else:
268 raise Exception("Invalid parameter")
269 elif(action=="edit"):
270 if(id is not None):
271 (ok, data)=inventory.read(cursor, id)
272 if(not ok):
273 raise Exception(data)
274 return render_template("inventory-edit.html", data=data, mid=data["mid"], action=action)
275 else:
276 raise Exception("Invalid parameter")
277 elif(action=="new"):
278 if(id is not None):
279 (ok, center)=multicenter.list(cursor)
280 if(not ok):
281 raise Exception(center)
282 return render_template("inventory-edit.html", data=[], mid=id, action=action, center=center)
283 else:
284 raise Exception("Invalid parameter")
285 except Exception as e:
286 raise(e)
287 return render_template("error.html", data=e)
288
289 @app.post("/inventory/new/<mid>")
290 @app.post("/inventory/edit/<iid>")
291 def inventory_edit(mid=None, iid=None):
292 if((ret:=problem())!="go"):
293 return ret
294 try:
295 cursor=get_db().cursor()
296 if(iid is None):
297 available="available" in request.form.keys()
298 (ok, data)=inventory.create(cursor, request.form, available)
299 if(not ok):
300 raise Exception(data)
301 iid=data
302 else:
303 available="available" in request.form.keys()
304 (ok, data)=inventory.update(cursor, iid, request.form, available)
305 if(not ok):
306 raise Exception(data)
307 return redirect("/inventory/view/"+str(iid))
308 except Exception as e:
309 raise(e)
310 return render_template("error.html", data=e)
311
312 @app.get("/patient/<action>")
313 @app.get("/patient/<action>/<pid>")
314 def patient_view(action, pid=None):
315 if((ret:=problem())!="go"):
316 return ret
317 try:
318 cursor=get_db().cursor()
319 if(action=="list"):
320 try:
321 page=int(request.args["page"])
322 except Exception:
323 page=1
324 if(request.args.get("all", False)):
325 (ok, data)=patient.list(cursor, request.args, g.mid)
326 else:
327 (ok, data)=patient.list(cursor, request.args)
328 if(ok):
329 param=dict(request.args)
330 param.pop("page", None)
331 return render_template("patient-list.html", num=app.config["LIST_LENGTH"], data=data, page=page, args=request.args, param=urlencode(param))
332 else:
333 raise Exception(data)
334 elif(action=="view"):
335 if(pid is not None):
336 (ok, data)=patient.read(cursor, pid)
337 if(not ok):
338 raise Exception(data)
339 (ok, vac)=vaccination.list_by_patient(cursor, pid)
340 if(not ok):
341 raise Exception(vac)
342 (ok, adv)=advice.list(cursor, pid)
343 if(not ok):
344 raise Exception(adv)
345
346 return render_template("patient-view.html", data=data, vaccination=vac, advice=adv, action=action)
347 else:
348 raise Exception("Invalid parameter")
349 elif(action=="edit"):
350 if(pid is not None):
351 (ok, data)=patient.read(cursor, pid)
352 if(not ok):
353 raise Exception(data)
354 (ok, center)=multicenter.list(cursor)
355 if(not ok):
356 raise exception(center)
357 return render_template("patient-edit.html", data=data, center=center, action=action)
358 else:
359 raise Exception("Invalid parameter")
360 elif(action=="new"):
361 (ok, last)=patient.last(cursor)
362 if(not ok):
363 raise Exception(last)
364 data={"lastCid": last["cid"]}
365 (ok, center)=multicenter.list(cursor)
366 if(not ok):
367 raise exception(center)
368 return render_template("patient-edit.html", data=data, center=center, action=action)
369 except Exception as e:
370 raise (e)
371 return render_template("error.html", data=e)
372
373 @app.post("/patient/new")
374 @app.post("/patient/edit/<pid>")
375 def patient_edit(pid=None):
376 if((ret:=problem())!="go"):
377 return ret
378 try:
379 cursor=get_db().cursor()
380 if(pid is None):
381 (ok, data)=patient.create(cursor, request.form)
382 if(not ok):
383 if(isinstance(data, sqlite3.IntegrityError)):
384 data=request.form
385 (ok, center)=multicenter.list(cursor)
386 if(not ok):
387 raise exception(center)
388 (ok, last)=patient.last(cursor)
389 if(not ok):
390 raise Exception(last)
391 data.lastCid=last["cid"]
392 return render_template("patient-edit.html", data=data, center=center, action="uniq")
393 else:
394 raise Exception(data)
395 pid=data
396 else:
397 (ok, data)=patient.update(cursor, pid, request.form)
398 if(not ok):
399 raise Exception(data)
400 return redirect("/patient/view/"+str(pid))
401 except Exception as e:
402 return render_template("error.html", data=e)
403
404 @app.get("/note/<pid>")
405 def note_get(pid):
406 if((ret:=problem())!="go"):
407 return ret
408 try:
409 cursor=get_db().cursor()
410 (ok, data)=patient.read(cursor, pid)
411 if(ok):
412 return render_template("note.html", data=data)
413 else:
414 return render_template("error.html", data=data)
415 except Exception as e:
416 raise(e)
417 return render_template("error.html", data=e)
418
419 @app.post("/note/<pid>")
420 def note_post(pid):
421 if((ret:=problem())!="go"):
422 return ret
423 try:
424 cursor=get_db().cursor()
425 (ok, data)=patient.note(cursor, pid, request.form)
426 if(not ok):
427 return render_template("error.html", data=data)
428 return redirect("/patient/view/"+str(pid))
429 except Exception as e:
430 raise(e)
431 return render_template("error.html", data=e)
432
433 @app.get("/vaccination/<action>")
434 @app.get("/vaccination/<action>/<id>")
435 def vaccination_view(action, id=None):
436 if((ret:=problem())!="go"):
437 return ret
438 try:
439 cursor=get_db().cursor()
440 if(action=="list"):
441 if(id is not None):
442 (ok, data)=vaccination.list_by_patient(cursor, id)
443 if(not ok):
444 raise exception(data)
445 (ok, pat)=patient.read(cursor, data["pid"])
446 if(not ok):
447 raise exception(patient)
448 (ok, inv)=inventory.list(cursor, data["iid"])
449 if(not ok):
450 raise exception(inventory)
451 return render_template("vaccination-view.html", data=data, patient=pat, inventory=inv, action=action)
452 else:
453 raise Exception("Invalid parameter")
454 elif(action=="view"):
455 if(id is not None):
456 (ok, data)=vaccination.read(cursor, id)
457 if(not ok):
458 raise Exception(data)
459 (ok, inv)=inventory.read(cursor, data["iid"])
460 if(not ok):
461 raise Exception(inv)
462 (ok, pat)=patient.read(cursor, data["pid"])
463 if(not ok):
464 raise Exception(pat)
465 (ok, consultant)=personnel.read(cursor, data["consultant"])
466 if(not ok):
467 raise Exception(consultant)
468 (ok, vaccinator)=personnel.read(cursor, data["vaccinator"])
469 if(not ok):
470 raise Exception(vaccinator)
471 return render_template("vaccination-view.html", data=data, inventory=inv, patient=pat, consultant=consultant, vaccinator=vaccinator, action=action)
472 else:
473 raise Exception("Invalid parameter")
474 elif(action=="edit"):
475 if(id is not None):
476 (ok, data)=vaccination.read(cursor, id)
477 if(not ok):
478 raise Exception(data)
479 (ok, inv)=inventory.list(cursor, g.mid)
480 if(not ok):
481 raise Exception(inv)
482 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
483 if(not ok):
484 raise Exception(consultant)
485 (ok, vaccinator)=personnel.list(cursor, g.mid, "vaccinator")
486 if(not ok):
487 raise Exception(vaccinator)
488 (ok, pat)=patient.read(cursor, data["pid"])
489 if(not ok):
490 raise Exception(pat)
491 return render_template("vaccination-edit.html", data=data, inventory=inv, consultant=consultant, vaccinator=vaccinator, patient=pat, action=action)
492 else:
493 raise Exception("Invalid parameter")
494 elif(action=="new"):
495 if(id is not None):
496 (ok, inv)=inventory.list(cursor, g.mid)
497 if(not ok):
498 raise Exception(inv)
499 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
500 if(not ok):
501 raise Exception(consultant)
502 (ok, vaccinator)=personnel.list(cursor, g.mid, "vaccinator")
503 if(not ok):
504 raise Exception(vaccinator)
505 (ok, pat)=patient.read(cursor, id)
506 if(not ok):
507 raise Exception(pat)
508 data={"pid": id, "date":datetime.now().strftime("%Y-%m-%dT%H:%M:%S")}
509 if(len(inv)<=0):
510 raise Exception("Please add a vaccine to inventory before vaccination.")
511 return render_template("vaccination-edit.html", data=data, inventory=inv, consultant=consultant, vaccinator=vaccinator, patient=pat, action=action)
512 else:
513 raise Exception("Invalid parameter")
514 except Exception as e:
515 return render_template("error.html", data=e)
516
517 @app.post("/vaccination/new/<pid>")
518 @app.post("/vaccination/edit/<vid>")
519 def vaccination_edit(vid=None, pid=None):
520 if((ret:=problem())!="go"):
521 return ret
522 try:
523 cursor=get_db().cursor()
524 if(vid is None):
525 given="given" in request.form.keys()
526 (ok, data)=vaccination.create(cursor, request.form, given)
527 if(not ok):
528 raise Exception(data)
529 vid=data
530 else:
531 given="given" in request.form.keys()
532 (ok, data)=vaccination.update(cursor, vid, request.form, given)
533 if(not ok):
534 raise Exception(data)
535 return redirect("/vaccination/view/"+str(vid))
536 except Exception as e:
537 raise(e)
538 return render_template("error.html", data=e)
539
540
541 @app.get("/advice/<action>")
542 @app.get("/advice/<action>/<id>")
543 def advice_view(action, id=None):
544 if((ret:=problem())!="go"):
545 return ret
546 try:
547 cursor=get_db().cursor()
548 if(action=="list"):
549 if(id is not None):
550 (ok, data)=advice.list(cursor, id)
551 if(not ok):
552 raise exception(data)
553 (ok, pat)=patient.read(cursor, id)
554 if(not ok):
555 raise exception(patient)
556 return render_template("advice-view.html", data=data, patient=pat, action=action)
557 else:
558 raise Exception("Invalid parameter")
559 elif(action=="view"):
560 if(id is not None):
561 (ok, data)=advice.read(cursor, id)
562 if(not ok):
563 raise Exception(data)
564 (ok, pat)=patient.read(cursor, data["pid"])
565 if(not ok):
566 raise Exception(pat)
567 (ok, consultant)=personnel.read(cursor, data["consultant"])
568 if(not ok):
569 raise Exception(consultant)
570 return render_template("advice-view.html", data=data, patient=pat, consultant=consultant, action=action)
571 else:
572 raise Exception("Invalid parameter")
573 elif(action=="edit"):
574 if(id is not None):
575 (ok, data)=advice.read(cursor, g.mid)
576 if(not ok):
577 raise Exception(data)
578 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
579 if(not ok):
580 raise Exception(consultant)
581 return render_template("advice-edit.html", data=data, consultant=consultant, action=action)
582 else:
583 raise Exception("Invalid parameter")
584 elif(action=="new"):
585 if(id is not None):
586 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
587 if(not ok):
588 raise Exception(consultant)
589 data={"pid": id, "date":datetime.now().strftime("%Y-%m-%dT%H:%M:%S")}
590 return render_template("advice-edit.html", data=data, consultant=consultant, action=action)
591 else:
592 raise Exception("Invalid parameter")
593 except Exception as e:
594 raise(e)
595 return render_template("error.html", data=e)
596
597 @app.post("/advice/new/<pid>")
598 @app.post("/advice/edit/<aid>")
599 def advice_edit(aid=None, pid=None):
600 if((ret:=problem())!="go"):
601 return ret
602 try:
603 cursor=get_db().cursor()
604 if(aid is None):
605 (ok, data)=advice.create(cursor, request.form, pid)
606 if(not ok):
607 raise Exception(data)
608 aid=data
609 else:
610 (ok, data)=advice.update(cursor, aid, request.form)
611 if(not ok):
612 raise Exception(data)
613 return redirect("/advice/view/"+str(aid))
614 except Exception as e:
615 raise(e)
616 return render_template("error.html", data=e)
617
618 @app.get("/delete/<cat>/<id>")
619 def delete(cat, id):
620 if((ret:=problem())!="go"):
621 return ret
622 try:
623 if(not g.enable_delete):
624 raise Exception("Deleting record is not allowed.")
625 cursor=get_db().cursor()
626 if(cat=="advice"):
627 (ok, data)=advice.delete(cursor, id)
628 if(not ok):
629 return render_template("error.html", data=data)
630 elif(cat=="inventory"):
631 (ok, data)=inventory.delete(cursor, id)
632 if(not ok):
633 return render_template("error.html", data=data)
634 elif(cat=="center"):
635 (ok, data)=multicenter.delete(cursor, id)
636 if(not ok):
637 return render_template("error.html", data=data)
638 elif(cat=="patient"):
639 (ok, data)=patient.delete(cursor, id)
640 if(not ok):
641 return render_template("error.html", data=data)
642 elif(cat=="personnel"):
643 (ok, data)=personnel.delete(cursor, id)
644 if(not ok):
645 return render_template("error.html", data=data)
646 elif(cat=="transaction"):
647 (ok, data)=transaction.delete(cursor, id)
648 if(not ok):
649 return render_template("error.html", data=data)
650 elif(cat=="vaccination"):
651 (ok, data)=vaccination.delete(cursor, id)
652 if(not ok):
653 return render_template("error.html", data=data)
654 return redirect("/")
655 except Exception as e:
656 raise(e)
657 return render_template("error.html", data=e)
658
659 @app.route("/card/<pid>")
660 def card(pid):
661 if((ret:=problem())!="go"):
662 return ret
663 try:
664 cursor=get_db().cursor()
665 (ok, data)=patient.read(cursor, pid)
666 if(not ok):
667 raise Exception(data)
668 (ok, vac)=vaccination.list_by_patient(cursor, pid)
669 if(not ok):
670 raise Exception(vac)
671 (ok, adv)=advice.list(cursor, pid)
672 if(not ok):
673 raise Exception(adv)
674 (ok, center)=multicenter.read(cursor, g.mid)
675 if(not ok):
676 raise Exception(center)
677 return render_template("card.html", data=data, vaccination=vac, advice=adv, center=center)
678 except Exception as e:
679 raise(e)
680 return render_template("error.html", data=e)
681
682 @app.route("/report/<mid>")
683 def report(mid):
684 if((ret:=problem())!="go"):
685 return ret
686 try:
687 date=request.args.get("date", datetime.now().strftime("%Y-%m-%d"))
688 cursor=get_db().cursor()
689 (ok, data)=vaccination.list_by_date(cursor, date)
690 if(not ok):
691 raise Exception(data)
692 (ok, center)=multicenter.read(cursor, g.mid)
693 if(not ok):
694 raise Exception(center)
695 return render_template("report.html", data=data, center=center, date=date)
696 except Exception as e:
697 raise(e)
698 return render_template("error.html", data=e)
699
700
701 def get_db():
702 db=getattr(g, "_database", None)
703 if db is None:
704 db=g._database=sqlite3.connect("data/database.db", isolation_level=None)
705 db.row_factory=sqlite3.Row
706 return db
707
708 @app.teardown_appcontext
709 def close_connection(exception):
710 db=getattr(g, "_database", None)
711 if db is not None:
712 db.close()
713
714 def problem(access=""):
715 if(auth.access()==auth.auth.ALL):
716 return "go"
717 else:
718 return redirect("/login")
719
720 @app.template_filter("format_date")
721 def format_date(date):
722 try:
723 if("T" in date):
724 dt=datetime.strptime(date, "%Y-%m-%dT%H:%M:%S")
725 return dt.strftime("%b %d, %Y, %I:%M %p")
726 else:
727 dt=datetime.strptime(date, "%Y-%m-%d")
728 return dt.strftime("%b %d, %Y")
729 except Exception as e:
730 return date
731
732 @app.template_filter("calculate_age")
733 def calculate_age(dob):
734 try:
735 today=datetime.today()
736 try:
737 born=datetime.strptime(dob, "%Y-%m-%d")
738 except ValueError:
739 born=datetime.strptime(dob, "%Y")
740 return today.year-born.year-((today.month, today.day)<(born.month, born.day))
741 except Exception as e:
742 return ""
743
744 def init_db():
745 with app.app_context():
746 db=get_db()
747 with app.open_resource("etc/schema.sql", mode="r") as f:
748 db.cursor().executescript(f.read())
749 db.commit()