]>
Softwares of Agnibho - librevax.git/blob - librevax.py
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/>.
8 from flask
import Flask
, render_template
, request
, session
, redirect
, send_file
, g
9 from urllib
. parse
import urlencode
10 from datetime
import datetime
12 import advice
, auth
, inventory
, multicenter
, patient
, personnel
, transaction
, vaccination
15 app
. config
. from_file ( "config/config.json" , load
= json
. load
)
19 g
. user
= session
. get ( "user" , None )
20 g
. auth
= session
. get ( "auth" , None )
21 g
. mid
= session
. get ( "mid" , None )
22 g
. center
= session
. get ( "center" , None )
23 g
. enable_delete
= app
. config
. get ( "ENABLE_DELETE" , False ) or g
. user
== "admin"
28 cursor
= get_db (). cursor ()
29 ( ok
, mc
)= multicenter
. read ( cursor
, session
[ "mid" ])
32 ( ok
, pat
)= patient
. list ( cursor
)
35 return render_template ( "index.html" , center
= mc
, patient
= pat
)
37 return redirect ( "/login" )
38 except Exception as e
:
40 return render_template ( "error.html" , data
= e
)
43 def login_get ( error
= False ):
45 cursor
= get_db (). cursor ()
46 ( ok
, data
)= multicenter
. list ( cursor
)
51 return render_template ( "login.html" , data
= data
, error
= error
)
52 except Exception as e
:
53 return render_template ( "error.html" , data
= e
)
57 cursor
= get_db (). cursor ()
58 if ( auth
. login ( cursor
, request
. form
[ "user" ], request
. form
[ "password" ], request
. form
[ "center" ])):
61 return login_get ( True )
66 return redirect ( "/login" )
70 if (( ret
:= problem ())!= "go" ):
72 cursor
= get_db (). cursor ()
73 ( ok
, mc
)= multicenter
. list ( cursor
)
76 users
= auth
. list ( cursor
)
77 return render_template ( "admin.html" , users
= users
, auth
= auth
. auth
, center
= mc
, msg
= msg
)
81 if (( ret
:= problem ())!= "go" ):
84 cursor
= get_db (). cursor ()
85 if ( auth
. new ( cursor
, request
. form
[ "user" ], request
. form
[ "pwd" ], request
. form
[ "auth" ])):
86 return admin ( msg
= "New user added" )
88 return admin ( msg
= "Failed to add" )
89 except Exception as e
:
91 return render_template ( "error.html" , data
= e
)
93 @app . post ( "/changepass" )
95 if (( ret
:= problem ())!= "go" ):
98 cursor
= get_db (). cursor ()
99 if ( "user" in request
. form
. keys ()):
100 if ( auth
. changePass ( cursor
, request
. form
[ "user" ], request
. form
[ "new" ])):
101 return admin ( msg
= "Password changed" )
103 return admin ( msg
= "Incorrect Password" )
104 elif ( request
. form
[ "new" ]== request
. form
[ "check" ]):
105 user
= request
. form
. get ( "user" , session
[ "user" ])
106 if ( auth
. changePass ( cursor
, user
, request
. form
[ "new" ], request
. form
[ "old" ])):
107 return admin ( msg
= "Password changed" )
109 return admin ( msg
= "Incorrect Password" )
111 return admin ( msg
= "Failed to change password" )
112 except Exception as e
:
114 return render_template ( "error.html" , data
= e
)
116 @app . post ( "/changeauth" )
118 if (( ret
:= problem ())!= "go" ):
121 cursor
= get_db (). cursor ()
122 if ( g
. auth
== auth
. auth
. ALL
and "user" in request
. form
. keys () and "auth" in request
. form
. keys ()):
123 if ( auth
. changeAuth ( cursor
, request
. form
[ "user" ], request
. form
[ "auth" ])):
124 return admin ( msg
= "Authorization changed" )
126 return admin ( msg
= "Failed to change authorization" )
128 return admin ( msg
= "Failed to change authorization" )
129 except Exception as e
:
131 return render_template ( "error.html" , data
= e
)
133 @app . post ( "/deluser" )
135 if (( ret
:= problem ())!= "go" ):
138 cursor
= get_db (). cursor ()
139 if ( "user" in request
. form
. keys ()):
140 if ( auth
. delete ( cursor
, request
. form
[ "user" ])):
141 return admin ( msg
= "User deleted" )
143 return admin ( msg
= "Failed to delete" )
145 return admin ( msg
= "Failed to delete" )
146 except Exception as e
:
148 return render_template ( "error.html" , data
= e
)
150 @app . post ( "/switchcenter" )
153 cursor
= get_db (). cursor ()
154 if ( auth
. switch ( cursor
, request
. form
)):
155 return admin ( msg
= "Switched center to " + g
. center
)
157 raise Exception ( "Failed to switch" )
158 except Exception as e
:
160 return render_template ( "error.html" , data
= e
)
162 @app . get ( "/center/<action>" )
163 @app . get ( "/center/<action>/<mid>" )
164 def center_view ( action
, mid
= None ):
165 if (( ret
:= problem ())!= "go" ):
168 cursor
= get_db (). cursor ()
170 ( ok
, data
)= multicenter
. list ( cursor
)
172 raise exception ( data
)
173 return render_template ( "multicenter-view.html" , data
= data
, action
= action
)
174 elif ( action
== "view" ):
176 ( ok
, data
)= multicenter
. read ( cursor
, mid
)
178 raise Exception ( data
)
179 ( ok
, invt
)= inventory
. list ( cursor
, mid
)
181 raise Exception ( invt
)
182 ( ok
, pers
)= personnel
. list ( cursor
, mid
)
184 raise Exception ( pers
)
185 return render_template ( "multicenter-view.html" , data
= data
, inventory
= invt
, personnel
= pers
, action
= action
)
187 raise Exception ( "Invalid parameter" )
188 elif ( action
== "edit" ):
190 ( ok
, data
)= multicenter
. read ( cursor
, mid
)
192 raise Exception ( data
)
193 return render_template ( "multicenter-edit.html" , data
= data
, action
= action
)
195 raise Exception ( "Invalid parameter" )
197 return render_template ( "multicenter-edit.html" , data
=[], action
= action
)
198 except Exception as e
:
200 return render_template ( "error.html" , data
= e
)
202 @app . get ( "/personnel/<action>" )
203 @app . get ( "/personnel/<action>/<sid>" )
204 def personnel_view ( action
, sid
= None ):
205 if (( ret
:= problem ())!= "go" ):
208 cursor
= get_db (). cursor ()
210 ( ok
, data
)= personnel
. list ( cursor
, g
. mid
)
212 raise exception ( data
)
213 return render_template ( "personnel-view.html" , data
= data
, action
= action
)
214 elif ( action
== "view" ):
216 ( ok
, data
)= personnel
. read ( cursor
, sid
)
218 raise Exception ( data
)
219 return render_template ( "personnel-view.html" , data
= data
, action
= action
)
221 raise Exception ( "Invalid parameter" )
222 elif ( action
== "edit" ):
224 ( ok
, data
)= personnel
. read ( cursor
, sid
)
226 raise Exception ( data
)
227 ( ok
, center
)= multicenter
. list ( cursor
)
229 raise Exception ( center
)
230 return render_template ( "personnel-edit.html" , data
= data
, center
= center
, action
= action
)
232 raise Exception ( "Invalid parameter" )
234 ( ok
, center
)= multicenter
. list ( cursor
)
236 raise Exception ( center
)
237 return render_template ( "personnel-edit.html" , data
=[], center
= center
, action
= action
)
238 except Exception as e
:
240 return render_template ( "error.html" , data
= e
)
242 @app . post ( "/personnel/new" )
243 @app . post ( "/personnel/edit/<sid>" )
244 def personnel_edit ( sid
= None ):
245 if (( ret
:= problem ())!= "go" ):
248 cursor
= get_db (). cursor ()
250 active
= "active" in request
. form
. keys ()
251 ( ok
, data
)= personnel
. create ( cursor
, request
. form
, active
)
253 raise Exception ( data
)
256 active
= "active" in request
. form
. keys ()
257 ( ok
, data
)= personnel
. update ( cursor
, sid
, request
. form
, active
)
259 raise Exception ( data
)
260 return redirect ( "/personnel/view/" + str ( sid
))
261 except Exception as e
:
262 return render_template ( "error.html" , data
= e
)
264 @app . post ( "/center/new" )
265 @app . post ( "/center/edit/<mid>" )
266 def center_edit ( mid
= None ):
267 if (( ret
:= problem ())!= "go" ):
270 cursor
= get_db (). cursor ()
272 ( ok
, data
)= multicenter
. create ( cursor
, request
. form
)
274 raise Exception ( data
)
277 ( ok
, data
)= multicenter
. update ( cursor
, mid
, request
. form
)
279 raise Exception ( data
)
280 return redirect ( "/center/view/" + str ( mid
))
281 except Exception as e
:
282 return render_template ( "error.html" , data
= e
)
284 @app . get ( "/inventory/<action>" )
285 @app . get ( "/inventory/<action>/<id>" )
286 def inventory_view ( action
, id = None ):
287 if (( ret
:= problem ())!= "go" ):
290 cursor
= get_db (). cursor ()
293 ( ok
, data
)= inventory
. list ( cursor
, id , all
= True )
295 raise exception ( data
)
296 ( ok
, center
)= multicenter
. read ( cursor
, id )
298 raise exception ( center
)
299 return render_template ( "inventory-view.html" , data
= data
, center
= center
, action
= action
)
301 raise Exception ( "Invalid parameter" )
302 elif ( action
== "view" ):
304 ( ok
, data
)= inventory
. read ( cursor
, id )
306 raise Exception ( data
)
307 return render_template ( "inventory-view.html" , data
= data
, action
= action
)
309 raise Exception ( "Invalid parameter" )
310 elif ( action
== "edit" ):
312 ( ok
, data
)= inventory
. read ( cursor
, id )
314 raise Exception ( data
)
315 return render_template ( "inventory-edit.html" , data
= data
, mid
= data
[ "mid" ], action
= action
)
317 raise Exception ( "Invalid parameter" )
320 ( ok
, center
)= multicenter
. list ( cursor
)
322 raise Exception ( center
)
323 return render_template ( "inventory-edit.html" , data
=[], mid
= id , action
= action
, center
= center
)
325 raise Exception ( "Invalid parameter" )
326 except Exception as e
:
328 return render_template ( "error.html" , data
= e
)
330 @app . post ( "/inventory/new/<mid>" )
331 @app . post ( "/inventory/edit/<iid>" )
332 def inventory_edit ( mid
= None , iid
= None ):
333 if (( ret
:= problem ())!= "go" ):
336 cursor
= get_db (). cursor ()
338 available
= "available" in request
. form
. keys ()
339 ( ok
, data
)= inventory
. create ( cursor
, request
. form
, available
)
341 raise Exception ( data
)
344 available
= "available" in request
. form
. keys ()
345 ( ok
, data
)= inventory
. update ( cursor
, iid
, request
. form
, available
)
347 raise Exception ( data
)
348 return redirect ( "/inventory/view/" + str ( iid
))
349 except Exception as e
:
351 return render_template ( "error.html" , data
= e
)
353 @app . get ( "/transaction/<action>" )
354 @app . get ( "/transaction/<action>/<id>" )
355 def transaction_view ( action
, id = None ):
356 if (( ret
:= problem ())!= "go" ):
359 cursor
= get_db (). cursor ()
362 ( ok
, data
)= transaction
. list ( cursor
, id )
364 raise Exception ( data
)
365 ( ok
, item
)= inventory
. read ( cursor
, id )
367 raise Exception ( item
)
368 ( ok
, administered
)= vaccination
. list_by_inventory ( cursor
, id )
370 raise Exception ( administered
)
372 for used
in administered
:
373 usedDosage
= usedDosage
+ int ( float ( used
[ "dosage" ]))
376 if ( entry
[ "action" ]== "IN" ):
377 stockVial
= stockVial
+ int ( float ( entry
[ "target" ]))
378 elif ( entry
[ "action" ]== "OUT" ):
379 stockVial
= stockVial
- int ( float ( entry
[ "target" ]))
380 return render_template ( "transaction-view.html" , data
= data
, item
= item
, administered
= administered
, usage
= usedDosage
, stock
= stockVial
, action
= action
)
382 raise Exception ( "Invalid parameter" )
383 elif ( action
== "view" ):
385 ( ok
, data
)= transaction
. read ( cursor
, id )
387 raise Exception ( data
)
388 ( ok
, item
)= inventory
. read ( cursor
, data
[ "iid" ])
390 raise Exception ( item
)
391 return render_template ( "transaction-view.html" , data
= data
, action
= action
, item
= item
)
393 raise Exception ( "Invalid parameter" )
394 elif ( action
== "edit" ):
396 ( ok
, data
)= transaction
. read ( cursor
, id )
398 raise Exception ( data
)
399 ( ok
, item
)= inventory
. read ( cursor
, data
[ "iid" ])
401 raise Exception ( item
)
402 return render_template ( "transaction-edit.html" , data
= data
, iid
= data
[ "iid" ], action
= action
, item
= item
)
404 raise Exception ( "Invalid parameter" )
407 ( ok
, item
)= inventory
. read ( cursor
, id )
409 raise Exception ( item
)
410 data
={ "date" : datetime
. now (). strftime ( "%Y-%m- %d T%H:%M:%S" )}
411 return render_template ( "transaction-edit.html" , data
= data
, iid
= id , action
= action
, item
= item
)
413 raise Exception ( "Invalid parameter" )
414 except Exception as e
:
416 return render_template ( "error.html" , data
= e
)
418 @app . post ( "/transaction/new/<iid>" )
419 @app . post ( "/transaction/edit/<tid>" )
420 def transaction_edit ( iid
= None , tid
= None ):
421 if (( ret
:= problem ())!= "go" ):
424 cursor
= get_db (). cursor ()
426 ( ok
, data
)= transaction
. create ( cursor
, request
. form
)
428 raise Exception ( data
)
431 ( ok
, data
)= transaction
. update ( cursor
, tid
, request
. form
)
433 raise Exception ( data
)
434 return redirect ( "/transaction/view/" + str ( tid
))
435 except Exception as e
:
437 return render_template ( "error.html" , data
= e
)
439 @app . get ( "/patient/<action>" )
440 @app . get ( "/patient/<action>/<pid>" )
441 def patient_view ( action
, pid
= None ):
442 if (( ret
:= problem ())!= "go" ):
445 cursor
= get_db (). cursor ()
448 page
= int ( request
. args
[ "page" ])
451 if ( request
. args
. get ( "all" , False )):
452 ( ok
, data
)= patient
. list ( cursor
, request
. args
, g
. mid
)
454 ( ok
, data
)= patient
. list ( cursor
, request
. args
)
456 param
= dict ( request
. args
)
457 param
. pop ( "page" , None )
458 return render_template ( "patient-list.html" , num
= app
. config
[ "LIST_LENGTH" ], data
= data
, page
= page
, args
= request
. args
, param
= urlencode ( param
))
460 raise Exception ( data
)
461 elif ( action
== "view" ):
463 ( ok
, data
)= patient
. read ( cursor
, pid
)
465 raise Exception ( data
)
466 ( ok
, vac
)= vaccination
. list_by_patient ( cursor
, pid
)
469 ( ok
, adv
)= advice
. list ( cursor
, pid
)
473 return render_template ( "patient-view.html" , data
= data
, vaccination
= vac
, advice
= adv
, action
= action
)
475 raise Exception ( "Invalid parameter" )
476 elif ( action
== "edit" ):
478 ( ok
, data
)= patient
. read ( cursor
, pid
)
480 raise Exception ( data
)
481 ( ok
, center
)= multicenter
. list ( cursor
)
483 raise exception ( center
)
484 return render_template ( "patient-edit.html" , data
= data
, center
= center
, action
= action
)
486 raise Exception ( "Invalid parameter" )
488 ( ok
, last
)= patient
. last ( cursor
)
490 raise Exception ( last
)
491 data
={ "lastCid" : last
[ "cid" ]}
492 ( ok
, center
)= multicenter
. list ( cursor
)
494 raise exception ( center
)
495 return render_template ( "patient-edit.html" , data
= data
, center
= center
, action
= action
)
496 except Exception as e
:
498 return render_template ( "error.html" , data
= e
)
500 @app . post ( "/patient/new" )
501 @app . post ( "/patient/edit/<pid>" )
502 def patient_edit ( pid
= None ):
503 if (( ret
:= problem ())!= "go" ):
506 cursor
= get_db (). cursor ()
508 ( ok
, data
)= patient
. create ( cursor
, request
. form
)
510 if ( isinstance ( data
, sqlite3
. IntegrityError
)):
512 ( ok
, center
)= multicenter
. list ( cursor
)
514 raise exception ( center
)
515 ( ok
, last
)= patient
. last ( cursor
)
517 raise Exception ( last
)
518 data
. lastCid
= last
[ "cid" ]
519 return render_template ( "patient-edit.html" , data
= data
, center
= center
, action
= "uniq" )
521 raise Exception ( data
)
524 ( ok
, data
)= patient
. update ( cursor
, pid
, request
. form
)
526 raise Exception ( data
)
527 return redirect ( "/patient/view/" + str ( pid
))
528 except Exception as e
:
529 return render_template ( "error.html" , data
= e
)
531 @app . get ( "/note/<pid>" )
533 if (( ret
:= problem ())!= "go" ):
536 cursor
= get_db (). cursor ()
537 ( ok
, data
)= patient
. read ( cursor
, pid
)
539 return render_template ( "note.html" , data
= data
)
541 return render_template ( "error.html" , data
= data
)
542 except Exception as e
:
544 return render_template ( "error.html" , data
= e
)
546 @app . post ( "/note/<pid>" )
548 if (( ret
:= problem ())!= "go" ):
551 cursor
= get_db (). cursor ()
552 ( ok
, data
)= patient
. note ( cursor
, pid
, request
. form
)
554 return render_template ( "error.html" , data
= data
)
555 return redirect ( "/patient/view/" + str ( pid
))
556 except Exception as e
:
558 return render_template ( "error.html" , data
= e
)
560 @app . get ( "/vaccination/<action>" )
561 @app . get ( "/vaccination/<action>/<id>" )
562 def vaccination_view ( action
, id = None ):
563 if (( ret
:= problem ())!= "go" ):
566 cursor
= get_db (). cursor ()
569 ( ok
, data
)= vaccination
. list_by_patient ( cursor
, id )
571 raise exception ( data
)
572 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
574 raise exception ( patient
)
575 ( ok
, inv
)= inventory
. list ( cursor
, data
[ "iid" ])
577 raise exception ( inventory
)
578 return render_template ( "vaccination-view.html" , data
= data
, patient
= pat
, inventory
= inv
, action
= action
)
580 raise Exception ( "Invalid parameter" )
581 elif ( action
== "view" ):
583 ( ok
, data
)= vaccination
. read ( cursor
, id )
585 raise Exception ( data
)
586 ( ok
, inv
)= inventory
. read ( cursor
, data
[ "iid" ])
589 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
592 ( ok
, consultant
)= personnel
. read ( cursor
, data
[ "consultant" ])
594 raise Exception ( consultant
)
595 ( ok
, vaccinator
)= personnel
. read ( cursor
, data
[ "vaccinator" ])
597 raise Exception ( vaccinator
)
598 return render_template ( "vaccination-view.html" , data
= data
, inventory
= inv
, patient
= pat
, consultant
= consultant
, vaccinator
= vaccinator
, action
= action
)
600 raise Exception ( "Invalid parameter" )
601 elif ( action
== "edit" ):
603 ( ok
, data
)= vaccination
. read ( cursor
, id )
605 raise Exception ( data
)
606 ( ok
, inv
)= inventory
. list ( cursor
, g
. mid
)
609 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
611 raise Exception ( consultant
)
612 ( ok
, vaccinator
)= personnel
. list ( cursor
, g
. mid
, "vaccinator" )
614 raise Exception ( vaccinator
)
615 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
618 return render_template ( "vaccination-edit.html" , data
= data
, inventory
= inv
, consultant
= consultant
, vaccinator
= vaccinator
, patient
= pat
, action
= action
)
620 raise Exception ( "Invalid parameter" )
623 ( ok
, inv
)= inventory
. list ( cursor
, g
. mid
)
626 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
628 raise Exception ( consultant
)
629 ( ok
, vaccinator
)= personnel
. list ( cursor
, g
. mid
, "vaccinator" )
631 raise Exception ( vaccinator
)
632 ( ok
, pat
)= patient
. read ( cursor
, id )
635 data
={ "pid" : id , "date" : datetime
. now (). strftime ( "%Y-%m- %d T%H:%M:%S" )}
637 raise Exception ( "Please add a vaccine to inventory before vaccination." )
638 return render_template ( "vaccination-edit.html" , data
= data
, inventory
= inv
, consultant
= consultant
, vaccinator
= vaccinator
, patient
= pat
, action
= action
)
640 raise Exception ( "Invalid parameter" )
641 except Exception as e
:
642 return render_template ( "error.html" , data
= e
)
644 @app . post ( "/vaccination/new/<pid>" )
645 @app . post ( "/vaccination/edit/<vid>" )
646 def vaccination_edit ( vid
= None , pid
= None ):
647 if (( ret
:= problem ())!= "go" ):
650 cursor
= get_db (). cursor ()
652 given
= "given" in request
. form
. keys ()
653 ( ok
, data
)= vaccination
. create ( cursor
, request
. form
, given
)
655 raise Exception ( data
)
658 given
= "given" in request
. form
. keys ()
659 ( ok
, data
)= vaccination
. update ( cursor
, vid
, request
. form
, given
)
661 raise Exception ( data
)
662 return redirect ( "/vaccination/view/" + str ( vid
))
663 except Exception as e
:
665 return render_template ( "error.html" , data
= e
)
669 if (( ret
:= problem ())!= "go" ):
672 cursor
= get_db (). cursor ()
673 ( ok
, data
)= vaccination
. mark ( cursor
, request
. form
[ "vid" ], True )
675 raise Exception ( data
)
676 return redirect ( "/vaccination/view/" + str ( request
. form
[ "vid" ]))
677 except Exception as e
:
679 return render_template ( "error.html" , data
= e
)
681 @app . get ( "/advice/<action>" )
682 @app . get ( "/advice/<action>/<id>" )
683 def advice_view ( action
, id = None ):
684 if (( ret
:= problem ())!= "go" ):
687 cursor
= get_db (). cursor ()
690 ( ok
, data
)= advice
. list ( cursor
, id )
692 raise exception ( data
)
693 ( ok
, pat
)= patient
. read ( cursor
, id )
695 raise exception ( patient
)
696 return render_template ( "advice-view.html" , data
= data
, patient
= pat
, action
= action
)
698 raise Exception ( "Invalid parameter" )
699 elif ( action
== "view" ):
701 ( ok
, data
)= advice
. read ( cursor
, id )
703 raise Exception ( data
)
704 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
707 ( ok
, consultant
)= personnel
. read ( cursor
, data
[ "consultant" ])
709 raise Exception ( consultant
)
710 return render_template ( "advice-view.html" , data
= data
, patient
= pat
, consultant
= consultant
, action
= action
)
712 raise Exception ( "Invalid parameter" )
713 elif ( action
== "edit" ):
715 ( ok
, data
)= advice
. read ( cursor
, g
. mid
)
717 raise Exception ( data
)
718 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
720 raise Exception ( consultant
)
721 return render_template ( "advice-edit.html" , data
= data
, consultant
= consultant
, action
= action
)
723 raise Exception ( "Invalid parameter" )
726 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
728 raise Exception ( consultant
)
729 data
={ "pid" : id , "date" : datetime
. now (). strftime ( "%Y-%m- %d T%H:%M:%S" )}
730 return render_template ( "advice-edit.html" , data
= data
, consultant
= consultant
, action
= action
)
732 raise Exception ( "Invalid parameter" )
733 except Exception as e
:
735 return render_template ( "error.html" , data
= e
)
737 @app . post ( "/advice/new/<pid>" )
738 @app . post ( "/advice/edit/<aid>" )
739 def advice_edit ( aid
= None , pid
= None ):
740 if (( ret
:= problem ())!= "go" ):
743 cursor
= get_db (). cursor ()
745 ( ok
, data
)= advice
. create ( cursor
, request
. form
, pid
)
747 raise Exception ( data
)
750 ( ok
, data
)= advice
. update ( cursor
, aid
, request
. form
)
752 raise Exception ( data
)
753 return redirect ( "/advice/view/" + str ( aid
))
754 except Exception as e
:
756 return render_template ( "error.html" , data
= e
)
758 @app . get ( "/delete/<cat>/<id>" )
760 if (( ret
:= problem ())!= "go" ):
763 if ( not g
. enable_delete
):
764 raise Exception ( "Deleting record is not allowed." )
765 cursor
= get_db (). cursor ()
767 ( ok
, data
)= advice
. delete ( cursor
, id )
769 return render_template ( "error.html" , data
= data
)
770 elif ( cat
== "inventory" ):
771 ( ok
, data
)= inventory
. delete ( cursor
, id )
773 return render_template ( "error.html" , data
= data
)
775 ( ok
, data
)= multicenter
. delete ( cursor
, id )
777 return render_template ( "error.html" , data
= data
)
778 elif ( cat
== "patient" ):
779 ( ok
, data
)= patient
. delete ( cursor
, id )
781 return render_template ( "error.html" , data
= data
)
782 elif ( cat
== "personnel" ):
783 ( ok
, data
)= personnel
. delete ( cursor
, id )
785 return render_template ( "error.html" , data
= data
)
786 elif ( cat
== "transaction" ):
787 ( ok
, data
)= transaction
. delete ( cursor
, id )
789 return render_template ( "error.html" , data
= data
)
790 elif ( cat
== "vaccination" ):
791 ( ok
, data
)= vaccination
. delete ( cursor
, id )
793 return render_template ( "error.html" , data
= data
)
795 except Exception as e
:
797 return render_template ( "error.html" , data
= e
)
799 @app . route ( "/card/<pid>" )
801 if (( ret
:= problem ())!= "go" ):
804 cursor
= get_db (). cursor ()
805 ( ok
, data
)= patient
. read ( cursor
, pid
)
807 raise Exception ( data
)
808 ( ok
, vac
)= vaccination
. list_by_patient ( cursor
, pid
)
811 ( ok
, adv
)= advice
. list ( cursor
, pid
)
814 ( ok
, center
)= multicenter
. read ( cursor
, g
. mid
)
816 raise Exception ( center
)
817 return render_template ( "card.html" , data
= data
, vaccination
= vac
, advice
= adv
, center
= center
)
818 except Exception as e
:
820 return render_template ( "error.html" , data
= e
)
822 @app . route ( "/report/<mid>" )
824 if (( ret
:= problem ())!= "go" ):
827 date
= request
. args
. get ( "date" , datetime
. now (). strftime ( "%Y-%m- %d " ))
828 cursor
= get_db (). cursor ()
829 ( ok
, data
)= vaccination
. list_by_date ( cursor
, date
)
831 raise Exception ( data
)
832 ( ok
, center
)= multicenter
. read ( cursor
, g
. mid
)
834 raise Exception ( center
)
838 count
[ i
[ "vaccine" ]]= count
[ i
[ "vaccine" ]]+ i
[ "dosage" ]
840 count
[ i
[ "vaccine" ]]= i
[ "dosage" ]
841 return render_template ( "report.html" , data
= data
, count
= count
, center
= center
, date
= date
)
842 except Exception as e
:
844 return render_template ( "error.html" , data
= e
)
848 if (( ret
:= problem ())!= "go" ):
852 raise PermissionError
853 return send_file ( "data/database.db" , as_attachment
= True , download_name
= "backup-" + str ( datetime
. now ())+ ".db" )
854 except PermissionError
as e
:
855 return render_template ( "error.html" , data
= "Access Denied" )
856 except Exception as e
:
858 return render_template ( "error.html" , data
= e
)
861 db
= getattr ( g
, "_database" , None )
863 db
= g
._ database
= sqlite3
. connect ( "data/database.db" , isolation_level
= None )
864 db
. row_factory
= sqlite3
. Row
867 @app . teardown_appcontext
868 def close_connection ( exception
):
869 db
= getattr ( g
, "_database" , None )
873 def problem ( access
= "" ):
874 if ( auth
. access ()== auth
. auth
. ALL
):
877 return render_template ( "error.html" , data
= "Access denied" )
879 @app . template_filter ( "format_date" )
880 def format_date ( date
):
883 dt
= datetime
. strptime ( date
, "%Y-%m- %d T%H:%M:%S" )
884 return dt
. strftime ( "%b %d , %Y, %I:%M %p" )
886 dt
= datetime
. strptime ( date
, "%Y-%m- %d " )
887 return dt
. strftime ( "%b %d , %Y" )
888 except Exception as e
:
891 @app . template_filter ( "calculate_age" )
892 def calculate_age ( dob
):
894 today
= datetime
. today ()
896 born
= datetime
. strptime ( dob
, "%Y-%m- %d " )
898 born
= datetime
. strptime ( dob
, "%Y" )
899 return today
. year
- born
. year
-(( today
. month
, today
. day
)<( born
. month
, born
. day
))
900 except Exception as e
:
904 with app
. app_context ():
906 with app
. open_resource ( "etc/schema.sql" , mode
= "r" ) as f
:
907 db
. cursor (). executescript ( f
. read ())