]> Softwares of Agnibho - librevax.git/blob - librevax.py
d5ea3b8db7e409162337c3cfc4200f47fec7a066
[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("/transaction/<action>")
313 @app.get("/transaction/<action>/<id>")
314 def transaction_view(action, id=None):
315 if((ret:=problem())!="go"):
316 return ret
317 try:
318 cursor=get_db().cursor()
319 if(action=="list"):
320 if(id is not None):
321 (ok, data)=transaction.list(cursor, id)
322 if(not ok):
323 raise Exception(data)
324 (ok, item)=inventory.read(cursor, id)
325 if(not ok):
326 raise Exception(item)
327 (ok, administered)=vaccination.list_by_inventory(cursor, id)
328 if(not ok):
329 raise Exception(administered)
330 usage=0
331 for used in administered:
332 usage=usage+int(float(used["dosage"]))
333 balance=0-usage
334 for entry in data:
335 if(entry["action"]=="IN"):
336 balance=balance+int(float(entry["target"]))
337 elif(entry["action"]=="OUT"):
338 balance=balance-int(float(entry["target"]))
339 return render_template("transaction-view.html", data=data, item=item, administered=administered, usage=usage, balance=balance, action=action)
340 else:
341 raise Exception("Invalid parameter")
342 elif(action=="view"):
343 if(id is not None):
344 (ok, data)=transaction.read(cursor, id)
345 if(not ok):
346 raise Exception(data)
347 (ok, item)=inventory.read(cursor, data["iid"])
348 if(not ok):
349 raise Exception(item)
350 return render_template("transaction-view.html", data=data, action=action, item=item)
351 else:
352 raise Exception("Invalid parameter")
353 elif(action=="edit"):
354 if(id is not None):
355 (ok, data)=transaction.read(cursor, id)
356 if(not ok):
357 raise Exception(data)
358 (ok, item)=inventory.read(cursor, data["iid"])
359 if(not ok):
360 raise Exception(item)
361 return render_template("transaction-edit.html", data=data, iid=data["iid"], action=action, item=item)
362 else:
363 raise Exception("Invalid parameter")
364 elif(action=="new"):
365 if(id is not None):
366 (ok, item)=inventory.read(cursor, id)
367 if(not ok):
368 raise Exception(item)
369 data={"date":datetime.now().strftime("%Y-%m-%dT%H:%M:%S")}
370 return render_template("transaction-edit.html", data=data, iid=id, action=action, item=item)
371 else:
372 raise Exception("Invalid parameter")
373 except Exception as e:
374 raise(e)
375 return render_template("error.html", data=e)
376
377 @app.post("/transaction/new/<iid>")
378 @app.post("/transaction/edit/<tid>")
379 def transaction_edit(iid=None, tid=None):
380 if((ret:=problem())!="go"):
381 return ret
382 try:
383 cursor=get_db().cursor()
384 if(tid is None):
385 (ok, data)=transaction.create(cursor, request.form)
386 if(not ok):
387 raise Exception(data)
388 tid=data
389 else:
390 (ok, data)=transaction.update(cursor, tid, request.form)
391 if(not ok):
392 raise Exception(data)
393 return redirect("/transaction/view/"+str(tid))
394 except Exception as e:
395 raise(e)
396 return render_template("error.html", data=e)
397
398 @app.get("/patient/<action>")
399 @app.get("/patient/<action>/<pid>")
400 def patient_view(action, pid=None):
401 if((ret:=problem())!="go"):
402 return ret
403 try:
404 cursor=get_db().cursor()
405 if(action=="list"):
406 try:
407 page=int(request.args["page"])
408 except Exception:
409 page=1
410 if(request.args.get("all", False)):
411 (ok, data)=patient.list(cursor, request.args, g.mid)
412 else:
413 (ok, data)=patient.list(cursor, request.args)
414 if(ok):
415 param=dict(request.args)
416 param.pop("page", None)
417 return render_template("patient-list.html", num=app.config["LIST_LENGTH"], data=data, page=page, args=request.args, param=urlencode(param))
418 else:
419 raise Exception(data)
420 elif(action=="view"):
421 if(pid is not None):
422 (ok, data)=patient.read(cursor, pid)
423 if(not ok):
424 raise Exception(data)
425 (ok, vac)=vaccination.list_by_patient(cursor, pid)
426 if(not ok):
427 raise Exception(vac)
428 (ok, adv)=advice.list(cursor, pid)
429 if(not ok):
430 raise Exception(adv)
431
432 return render_template("patient-view.html", data=data, vaccination=vac, advice=adv, action=action)
433 else:
434 raise Exception("Invalid parameter")
435 elif(action=="edit"):
436 if(pid is not None):
437 (ok, data)=patient.read(cursor, pid)
438 if(not ok):
439 raise Exception(data)
440 (ok, center)=multicenter.list(cursor)
441 if(not ok):
442 raise exception(center)
443 return render_template("patient-edit.html", data=data, center=center, action=action)
444 else:
445 raise Exception("Invalid parameter")
446 elif(action=="new"):
447 (ok, last)=patient.last(cursor)
448 if(not ok):
449 raise Exception(last)
450 data={"lastCid": last["cid"]}
451 (ok, center)=multicenter.list(cursor)
452 if(not ok):
453 raise exception(center)
454 return render_template("patient-edit.html", data=data, center=center, action=action)
455 except Exception as e:
456 raise (e)
457 return render_template("error.html", data=e)
458
459 @app.post("/patient/new")
460 @app.post("/patient/edit/<pid>")
461 def patient_edit(pid=None):
462 if((ret:=problem())!="go"):
463 return ret
464 try:
465 cursor=get_db().cursor()
466 if(pid is None):
467 (ok, data)=patient.create(cursor, request.form)
468 if(not ok):
469 if(isinstance(data, sqlite3.IntegrityError)):
470 data=request.form
471 (ok, center)=multicenter.list(cursor)
472 if(not ok):
473 raise exception(center)
474 (ok, last)=patient.last(cursor)
475 if(not ok):
476 raise Exception(last)
477 data.lastCid=last["cid"]
478 return render_template("patient-edit.html", data=data, center=center, action="uniq")
479 else:
480 raise Exception(data)
481 pid=data
482 else:
483 (ok, data)=patient.update(cursor, pid, request.form)
484 if(not ok):
485 raise Exception(data)
486 return redirect("/patient/view/"+str(pid))
487 except Exception as e:
488 return render_template("error.html", data=e)
489
490 @app.get("/note/<pid>")
491 def note_get(pid):
492 if((ret:=problem())!="go"):
493 return ret
494 try:
495 cursor=get_db().cursor()
496 (ok, data)=patient.read(cursor, pid)
497 if(ok):
498 return render_template("note.html", data=data)
499 else:
500 return render_template("error.html", data=data)
501 except Exception as e:
502 raise(e)
503 return render_template("error.html", data=e)
504
505 @app.post("/note/<pid>")
506 def note_post(pid):
507 if((ret:=problem())!="go"):
508 return ret
509 try:
510 cursor=get_db().cursor()
511 (ok, data)=patient.note(cursor, pid, request.form)
512 if(not ok):
513 return render_template("error.html", data=data)
514 return redirect("/patient/view/"+str(pid))
515 except Exception as e:
516 raise(e)
517 return render_template("error.html", data=e)
518
519 @app.get("/vaccination/<action>")
520 @app.get("/vaccination/<action>/<id>")
521 def vaccination_view(action, id=None):
522 if((ret:=problem())!="go"):
523 return ret
524 try:
525 cursor=get_db().cursor()
526 if(action=="list"):
527 if(id is not None):
528 (ok, data)=vaccination.list_by_patient(cursor, id)
529 if(not ok):
530 raise exception(data)
531 (ok, pat)=patient.read(cursor, data["pid"])
532 if(not ok):
533 raise exception(patient)
534 (ok, inv)=inventory.list(cursor, data["iid"])
535 if(not ok):
536 raise exception(inventory)
537 return render_template("vaccination-view.html", data=data, patient=pat, inventory=inv, action=action)
538 else:
539 raise Exception("Invalid parameter")
540 elif(action=="view"):
541 if(id is not None):
542 (ok, data)=vaccination.read(cursor, id)
543 if(not ok):
544 raise Exception(data)
545 (ok, inv)=inventory.read(cursor, data["iid"])
546 if(not ok):
547 raise Exception(inv)
548 (ok, pat)=patient.read(cursor, data["pid"])
549 if(not ok):
550 raise Exception(pat)
551 (ok, consultant)=personnel.read(cursor, data["consultant"])
552 if(not ok):
553 raise Exception(consultant)
554 (ok, vaccinator)=personnel.read(cursor, data["vaccinator"])
555 if(not ok):
556 raise Exception(vaccinator)
557 return render_template("vaccination-view.html", data=data, inventory=inv, patient=pat, consultant=consultant, vaccinator=vaccinator, action=action)
558 else:
559 raise Exception("Invalid parameter")
560 elif(action=="edit"):
561 if(id is not None):
562 (ok, data)=vaccination.read(cursor, id)
563 if(not ok):
564 raise Exception(data)
565 (ok, inv)=inventory.list(cursor, g.mid)
566 if(not ok):
567 raise Exception(inv)
568 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
569 if(not ok):
570 raise Exception(consultant)
571 (ok, vaccinator)=personnel.list(cursor, g.mid, "vaccinator")
572 if(not ok):
573 raise Exception(vaccinator)
574 (ok, pat)=patient.read(cursor, data["pid"])
575 if(not ok):
576 raise Exception(pat)
577 return render_template("vaccination-edit.html", data=data, inventory=inv, consultant=consultant, vaccinator=vaccinator, patient=pat, action=action)
578 else:
579 raise Exception("Invalid parameter")
580 elif(action=="new"):
581 if(id is not None):
582 (ok, inv)=inventory.list(cursor, g.mid)
583 if(not ok):
584 raise Exception(inv)
585 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
586 if(not ok):
587 raise Exception(consultant)
588 (ok, vaccinator)=personnel.list(cursor, g.mid, "vaccinator")
589 if(not ok):
590 raise Exception(vaccinator)
591 (ok, pat)=patient.read(cursor, id)
592 if(not ok):
593 raise Exception(pat)
594 data={"pid": id, "date":datetime.now().strftime("%Y-%m-%dT%H:%M:%S")}
595 if(len(inv)<=0):
596 raise Exception("Please add a vaccine to inventory before vaccination.")
597 return render_template("vaccination-edit.html", data=data, inventory=inv, consultant=consultant, vaccinator=vaccinator, patient=pat, action=action)
598 else:
599 raise Exception("Invalid parameter")
600 except Exception as e:
601 return render_template("error.html", data=e)
602
603 @app.post("/vaccination/new/<pid>")
604 @app.post("/vaccination/edit/<vid>")
605 def vaccination_edit(vid=None, pid=None):
606 if((ret:=problem())!="go"):
607 return ret
608 try:
609 cursor=get_db().cursor()
610 if(vid is None):
611 given="given" in request.form.keys()
612 (ok, data)=vaccination.create(cursor, request.form, given)
613 if(not ok):
614 raise Exception(data)
615 vid=data
616 else:
617 given="given" in request.form.keys()
618 (ok, data)=vaccination.update(cursor, vid, request.form, given)
619 if(not ok):
620 raise Exception(data)
621 return redirect("/vaccination/view/"+str(vid))
622 except Exception as e:
623 raise(e)
624 return render_template("error.html", data=e)
625
626
627 @app.get("/advice/<action>")
628 @app.get("/advice/<action>/<id>")
629 def advice_view(action, id=None):
630 if((ret:=problem())!="go"):
631 return ret
632 try:
633 cursor=get_db().cursor()
634 if(action=="list"):
635 if(id is not None):
636 (ok, data)=advice.list(cursor, id)
637 if(not ok):
638 raise exception(data)
639 (ok, pat)=patient.read(cursor, id)
640 if(not ok):
641 raise exception(patient)
642 return render_template("advice-view.html", data=data, patient=pat, action=action)
643 else:
644 raise Exception("Invalid parameter")
645 elif(action=="view"):
646 if(id is not None):
647 (ok, data)=advice.read(cursor, id)
648 if(not ok):
649 raise Exception(data)
650 (ok, pat)=patient.read(cursor, data["pid"])
651 if(not ok):
652 raise Exception(pat)
653 (ok, consultant)=personnel.read(cursor, data["consultant"])
654 if(not ok):
655 raise Exception(consultant)
656 return render_template("advice-view.html", data=data, patient=pat, consultant=consultant, action=action)
657 else:
658 raise Exception("Invalid parameter")
659 elif(action=="edit"):
660 if(id is not None):
661 (ok, data)=advice.read(cursor, g.mid)
662 if(not ok):
663 raise Exception(data)
664 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
665 if(not ok):
666 raise Exception(consultant)
667 return render_template("advice-edit.html", data=data, consultant=consultant, action=action)
668 else:
669 raise Exception("Invalid parameter")
670 elif(action=="new"):
671 if(id is not None):
672 (ok, consultant)=personnel.list(cursor, g.mid, "consultant")
673 if(not ok):
674 raise Exception(consultant)
675 data={"pid": id, "date":datetime.now().strftime("%Y-%m-%dT%H:%M:%S")}
676 return render_template("advice-edit.html", data=data, consultant=consultant, action=action)
677 else:
678 raise Exception("Invalid parameter")
679 except Exception as e:
680 raise(e)
681 return render_template("error.html", data=e)
682
683 @app.post("/advice/new/<pid>")
684 @app.post("/advice/edit/<aid>")
685 def advice_edit(aid=None, pid=None):
686 if((ret:=problem())!="go"):
687 return ret
688 try:
689 cursor=get_db().cursor()
690 if(aid is None):
691 (ok, data)=advice.create(cursor, request.form, pid)
692 if(not ok):
693 raise Exception(data)
694 aid=data
695 else:
696 (ok, data)=advice.update(cursor, aid, request.form)
697 if(not ok):
698 raise Exception(data)
699 return redirect("/advice/view/"+str(aid))
700 except Exception as e:
701 raise(e)
702 return render_template("error.html", data=e)
703
704 @app.get("/delete/<cat>/<id>")
705 def delete(cat, id):
706 if((ret:=problem())!="go"):
707 return ret
708 try:
709 if(not g.enable_delete):
710 raise Exception("Deleting record is not allowed.")
711 cursor=get_db().cursor()
712 if(cat=="advice"):
713 (ok, data)=advice.delete(cursor, id)
714 if(not ok):
715 return render_template("error.html", data=data)
716 elif(cat=="inventory"):
717 (ok, data)=inventory.delete(cursor, id)
718 if(not ok):
719 return render_template("error.html", data=data)
720 elif(cat=="center"):
721 (ok, data)=multicenter.delete(cursor, id)
722 if(not ok):
723 return render_template("error.html", data=data)
724 elif(cat=="patient"):
725 (ok, data)=patient.delete(cursor, id)
726 if(not ok):
727 return render_template("error.html", data=data)
728 elif(cat=="personnel"):
729 (ok, data)=personnel.delete(cursor, id)
730 if(not ok):
731 return render_template("error.html", data=data)
732 elif(cat=="transaction"):
733 (ok, data)=transaction.delete(cursor, id)
734 if(not ok):
735 return render_template("error.html", data=data)
736 elif(cat=="vaccination"):
737 (ok, data)=vaccination.delete(cursor, id)
738 if(not ok):
739 return render_template("error.html", data=data)
740 return redirect("/")
741 except Exception as e:
742 raise(e)
743 return render_template("error.html", data=e)
744
745 @app.route("/card/<pid>")
746 def card(pid):
747 if((ret:=problem())!="go"):
748 return ret
749 try:
750 cursor=get_db().cursor()
751 (ok, data)=patient.read(cursor, pid)
752 if(not ok):
753 raise Exception(data)
754 (ok, vac)=vaccination.list_by_patient(cursor, pid)
755 if(not ok):
756 raise Exception(vac)
757 (ok, adv)=advice.list(cursor, pid)
758 if(not ok):
759 raise Exception(adv)
760 (ok, center)=multicenter.read(cursor, g.mid)
761 if(not ok):
762 raise Exception(center)
763 return render_template("card.html", data=data, vaccination=vac, advice=adv, center=center)
764 except Exception as e:
765 raise(e)
766 return render_template("error.html", data=e)
767
768 @app.route("/report/<mid>")
769 def report(mid):
770 if((ret:=problem())!="go"):
771 return ret
772 try:
773 date=request.args.get("date", datetime.now().strftime("%Y-%m-%d"))
774 cursor=get_db().cursor()
775 (ok, data)=vaccination.list_by_date(cursor, date)
776 if(not ok):
777 raise Exception(data)
778 (ok, center)=multicenter.read(cursor, g.mid)
779 if(not ok):
780 raise Exception(center)
781 count={}
782 for i in data:
783 try:
784 count[i["vaccine"]]=count[i["vaccine"]]+i["dosage"]
785 except KeyError:
786 count[i["vaccine"]]=i["dosage"]
787 return render_template("report.html", data=data, count=count, center=center, date=date)
788 except Exception as e:
789 raise(e)
790 return render_template("error.html", data=e)
791
792
793 def get_db():
794 db=getattr(g, "_database", None)
795 if db is None:
796 db=g._database=sqlite3.connect("data/database.db", isolation_level=None)
797 db.row_factory=sqlite3.Row
798 return db
799
800 @app.teardown_appcontext
801 def close_connection(exception):
802 db=getattr(g, "_database", None)
803 if db is not None:
804 db.close()
805
806 def problem(access=""):
807 if(auth.access()==auth.auth.ALL):
808 return "go"
809 else:
810 return redirect("/login")
811
812 @app.template_filter("format_date")
813 def format_date(date):
814 try:
815 if("T" in date):
816 dt=datetime.strptime(date, "%Y-%m-%dT%H:%M:%S")
817 return dt.strftime("%b %d, %Y, %I:%M %p")
818 else:
819 dt=datetime.strptime(date, "%Y-%m-%d")
820 return dt.strftime("%b %d, %Y")
821 except Exception as e:
822 return date
823
824 @app.template_filter("calculate_age")
825 def calculate_age(dob):
826 try:
827 today=datetime.today()
828 try:
829 born=datetime.strptime(dob, "%Y-%m-%d")
830 except ValueError:
831 born=datetime.strptime(dob, "%Y")
832 return today.year-born.year-((today.month, today.day)<(born.month, born.day))
833 except Exception as e:
834 return ""
835
836 def init_db():
837 with app.app_context():
838 db=get_db()
839 with app.open_resource("etc/schema.sql", mode="r") as f:
840 db.cursor().executescript(f.read())
841 db.commit()