]>
Softwares of Agnibho - librevax.git/blob - librevax.py
ab1420fc75d8bf1c166d314e19bc948ed1f126e1
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
, 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
. mid
= session
. get ( "mid" , None )
21 g
. center
= session
. get ( "center" , None )
22 g
. enable_delete
= app
. config
. get ( "ENABLE_DELETE" , False )
27 if (( ret
:= problem ())!= "go" ):
29 cursor
= get_db (). cursor ()
30 ( ok
, mc
)= multicenter
. read ( cursor
, session
[ "mid" ])
33 ( ok
, pat
)= patient
. list ( cursor
)
36 return render_template ( "index.html" , center
= mc
, patient
= pat
)
37 except Exception as e
:
39 return render_template ( "error.html" , data
= e
)
42 def login_get ( error
= False ):
44 cursor
= get_db (). cursor ()
45 ( ok
, data
)= multicenter
. list ( cursor
)
50 return render_template ( "login.html" , data
= data
, error
= error
)
51 except Exception as e
:
52 return render_template ( "error.html" , data
= e
)
56 cursor
= get_db (). cursor ()
57 if ( auth
. login ( cursor
, request
. form
[ "user" ], request
. form
[ "password" ], request
. form
[ "center" ])):
60 return login_get ( True )
65 return redirect ( "/login" )
69 cursor
= get_db (). cursor ()
70 ( ok
, mc
)= multicenter
. list ( cursor
)
73 users
= auth
. list ( cursor
)
74 return render_template ( "admin.html" , users
= users
, center
= mc
, msg
= msg
)
79 cursor
= get_db (). cursor ()
80 if ( auth
. new ( cursor
, request
. form
[ "user" ], request
. form
[ "pwd" ])):
81 return admin ( msg
= "New user added" )
83 return admin ( msg
= "Failed to add" )
84 except Exception as e
:
86 return render_template ( "error.html" , data
= e
)
88 @app . post ( "/changepass" )
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" )
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" )
102 return admin ( msg
= "Incorrect Password" )
104 return admin ( msg
= "Failed to change password" )
105 except Exception as e
:
107 return render_template ( "error.html" , data
= e
)
109 @app . post ( "/switchcenter" )
112 cursor
= get_db (). cursor ()
113 if ( auth
. switch ( cursor
, request
. form
)):
114 return admin ( msg
= "Switched center to " + g
. center
)
116 raise Exception ( "Failed to switch" )
117 except Exception as e
:
119 return render_template ( "error.html" , data
= e
)
121 @app . get ( "/center/<action>" )
122 @app . get ( "/center/<action>/<mid>" )
123 def center_view ( action
, mid
= None ):
124 if (( ret
:= problem ())!= "go" ):
127 cursor
= get_db (). cursor ()
129 ( ok
, data
)= multicenter
. list ( cursor
)
131 raise exception ( data
)
132 return render_template ( "multicenter-view.html" , data
= data
, action
= action
)
133 elif ( action
== "view" ):
135 ( ok
, data
)= multicenter
. read ( cursor
, mid
)
137 raise Exception ( data
)
138 ( ok
, invt
)= inventory
. list ( cursor
, mid
)
140 raise Exception ( invt
)
141 ( ok
, pers
)= personnel
. list ( cursor
, mid
)
143 raise Exception ( pers
)
144 return render_template ( "multicenter-view.html" , data
= data
, inventory
= invt
, personnel
= pers
, action
= action
)
146 raise Exception ( "Invalid parameter" )
147 elif ( action
== "edit" ):
149 ( ok
, data
)= multicenter
. read ( cursor
, mid
)
151 raise Exception ( data
)
152 return render_template ( "multicenter-edit.html" , data
= data
, action
= action
)
154 raise Exception ( "Invalid parameter" )
156 return render_template ( "multicenter-edit.html" , data
=[], action
= action
)
157 except Exception as e
:
159 return render_template ( "error.html" , data
= e
)
161 @app . get ( "/personnel/<action>" )
162 @app . get ( "/personnel/<action>/<sid>" )
163 def personnel_view ( action
, sid
= None ):
164 if (( ret
:= problem ())!= "go" ):
167 cursor
= get_db (). cursor ()
169 ( ok
, data
)= personnel
. list ( cursor
, g
. mid
)
171 raise exception ( data
)
172 return render_template ( "personnel-view.html" , data
= data
, action
= action
)
173 elif ( action
== "view" ):
175 ( ok
, data
)= personnel
. read ( cursor
, sid
)
177 raise Exception ( data
)
178 return render_template ( "personnel-view.html" , data
= data
, action
= action
)
180 raise Exception ( "Invalid parameter" )
181 elif ( action
== "edit" ):
183 ( ok
, data
)= personnel
. read ( cursor
, sid
)
185 raise Exception ( data
)
186 ( ok
, center
)= multicenter
. list ( cursor
)
188 raise Exception ( center
)
189 return render_template ( "personnel-edit.html" , data
= data
, center
= center
, action
= action
)
191 raise Exception ( "Invalid parameter" )
193 ( ok
, center
)= multicenter
. list ( cursor
)
195 raise Exception ( center
)
196 return render_template ( "personnel-edit.html" , data
=[], center
= center
, action
= action
)
197 except Exception as e
:
199 return render_template ( "error.html" , data
= e
)
201 @app . post ( "/personnel/new" )
202 @app . post ( "/personnel/edit/<sid>" )
203 def personnel_edit ( sid
= None ):
204 if (( ret
:= problem ())!= "go" ):
207 cursor
= get_db (). cursor ()
209 active
= "active" in request
. form
. keys ()
210 ( ok
, data
)= personnel
. create ( cursor
, request
. form
, active
)
212 raise Exception ( data
)
215 active
= "active" in request
. form
. keys ()
216 ( ok
, data
)= personnel
. update ( cursor
, sid
, request
. form
, active
)
218 raise Exception ( data
)
219 return redirect ( "/personnel/view/" + str ( sid
))
220 except Exception as e
:
221 return render_template ( "error.html" , data
= e
)
223 @app . post ( "/center/new" )
224 @app . post ( "/center/edit/<mid>" )
225 def center_edit ( mid
= None ):
226 if (( ret
:= problem ())!= "go" ):
229 cursor
= get_db (). cursor ()
231 ( ok
, data
)= multicenter
. create ( cursor
, request
. form
)
233 raise Exception ( data
)
236 ( ok
, data
)= multicenter
. update ( cursor
, mid
, request
. form
)
238 raise Exception ( data
)
239 return redirect ( "/center/view/" + str ( mid
))
240 except Exception as e
:
241 return render_template ( "error.html" , data
= e
)
243 @app . get ( "/inventory/<action>" )
244 @app . get ( "/inventory/<action>/<id>" )
245 def inventory_view ( action
, id = None ):
246 if (( ret
:= problem ())!= "go" ):
249 cursor
= get_db (). cursor ()
252 ( ok
, data
)= inventory
. list ( cursor
, id )
254 raise exception ( data
)
255 ( ok
, center
)= multicenter
. read ( cursor
, id )
257 raise exception ( center
)
258 return render_template ( "inventory-view.html" , data
= data
, center
= center
, action
= action
)
260 raise Exception ( "Invalid parameter" )
261 elif ( action
== "view" ):
263 ( ok
, data
)= inventory
. read ( cursor
, id )
265 raise Exception ( data
)
266 return render_template ( "inventory-view.html" , data
= data
, action
= action
)
268 raise Exception ( "Invalid parameter" )
269 elif ( action
== "edit" ):
271 ( ok
, data
)= inventory
. read ( cursor
, id )
273 raise Exception ( data
)
274 return render_template ( "inventory-edit.html" , data
= data
, mid
= data
[ "mid" ], action
= action
)
276 raise Exception ( "Invalid parameter" )
279 ( ok
, center
)= multicenter
. list ( cursor
)
281 raise Exception ( center
)
282 return render_template ( "inventory-edit.html" , data
=[], mid
= id , action
= action
, center
= center
)
284 raise Exception ( "Invalid parameter" )
285 except Exception as e
:
287 return render_template ( "error.html" , data
= e
)
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" ):
295 cursor
= get_db (). cursor ()
297 ( ok
, data
)= inventory
. create ( cursor
, request
. form
)
299 raise Exception ( data
)
302 ( ok
, data
)= inventory
. update ( cursor
, iid
, request
. form
)
304 raise Exception ( data
)
305 return redirect ( "/inventory/view/" + str ( iid
))
306 except Exception as e
:
308 return render_template ( "error.html" , data
= e
)
310 @app . get ( "/patient/<action>" )
311 @app . get ( "/patient/<action>/<pid>" )
312 def patient_view ( action
, pid
= None ):
313 if (( ret
:= problem ())!= "go" ):
316 cursor
= get_db (). cursor ()
319 page
= int ( request
. args
[ "page" ])
322 if ( request
. args
. get ( "all" , False )):
323 ( ok
, data
)= patient
. list ( cursor
, request
. args
, g
. mid
)
325 ( ok
, data
)= patient
. list ( cursor
, request
. args
)
327 param
= dict ( request
. args
)
328 param
. pop ( "page" , None )
329 return render_template ( "patient-list.html" , num
= app
. config
[ "LIST_LENGTH" ], data
= data
, page
= page
, args
= request
. args
, param
= urlencode ( param
))
331 raise Exception ( data
)
332 elif ( action
== "view" ):
334 ( ok
, data
)= patient
. read ( cursor
, pid
)
336 raise Exception ( data
)
337 ( ok
, vac
)= vaccination
. list_by_patient ( cursor
, pid
)
340 ( ok
, adv
)= advice
. list ( cursor
, pid
)
344 return render_template ( "patient-view.html" , data
= data
, vaccination
= vac
, advice
= adv
, action
= action
)
346 raise Exception ( "Invalid parameter" )
347 elif ( action
== "edit" ):
349 ( ok
, data
)= patient
. read ( cursor
, pid
)
351 raise Exception ( data
)
352 ( ok
, center
)= multicenter
. list ( cursor
)
354 raise exception ( center
)
355 return render_template ( "patient-edit.html" , data
= data
, center
= center
, action
= action
)
357 raise Exception ( "Invalid parameter" )
359 ( ok
, last
)= patient
. last ( cursor
)
361 raise Exception ( last
)
362 data
={ "lastCid" : last
[ "cid" ]}
363 ( ok
, center
)= multicenter
. list ( cursor
)
365 raise exception ( center
)
366 return render_template ( "patient-edit.html" , data
= data
, center
= center
, action
= action
)
367 except Exception as e
:
369 return render_template ( "error.html" , data
= e
)
371 @app . post ( "/patient/new" )
372 @app . post ( "/patient/edit/<pid>" )
373 def patient_edit ( pid
= None ):
374 if (( ret
:= problem ())!= "go" ):
377 cursor
= get_db (). cursor ()
379 ( ok
, data
)= patient
. create ( cursor
, request
. form
)
381 raise Exception ( data
)
384 ( ok
, data
)= patient
. update ( cursor
, pid
, request
. form
)
386 raise Exception ( data
)
387 return redirect ( "/patient/view/" + str ( pid
))
388 except Exception as e
:
389 return render_template ( "error.html" , data
= e
)
391 @app . get ( "/note/<pid>" )
393 if (( ret
:= problem ())!= "go" ):
396 cursor
= get_db (). cursor ()
397 ( ok
, data
)= patient
. read ( cursor
, pid
)
399 return render_template ( "note.html" , data
= data
)
401 return render_template ( "error.html" , data
= data
)
402 except Exception as e
:
404 return render_template ( "error.html" , data
= e
)
406 @app . post ( "/note/<pid>" )
408 if (( ret
:= problem ())!= "go" ):
411 cursor
= get_db (). cursor ()
412 ( ok
, data
)= patient
. note ( cursor
, pid
, request
. form
)
414 return render_template ( "error.html" , data
= data
)
415 return redirect ( "/patient/view/" + str ( pid
))
416 except Exception as e
:
418 return render_template ( "error.html" , data
= e
)
420 @app . get ( "/vaccination/<action>" )
421 @app . get ( "/vaccination/<action>/<id>" )
422 def vaccination_view ( action
, id = None ):
423 if (( ret
:= problem ())!= "go" ):
426 cursor
= get_db (). cursor ()
429 ( ok
, data
)= vaccination
. list_by_patient ( cursor
, id )
431 raise exception ( data
)
432 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
434 raise exception ( patient
)
435 ( ok
, inv
)= inventory
. list ( cursor
, data
[ "iid" ])
437 raise exception ( inventory
)
438 return render_template ( "vaccination-view.html" , data
= data
, patient
= pat
, inventory
= inv
, action
= action
)
440 raise Exception ( "Invalid parameter" )
441 elif ( action
== "view" ):
443 ( ok
, data
)= vaccination
. read ( cursor
, id )
445 raise Exception ( data
)
446 ( ok
, inv
)= inventory
. read ( cursor
, data
[ "iid" ])
448 raise Exception ( inventory
)
449 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
451 raise Exception ( patient
)
452 ( ok
, consultant
)= personnel
. read ( cursor
, data
[ "consultant" ])
454 raise Exception ( consultant
)
455 ( ok
, vaccinator
)= personnel
. read ( cursor
, data
[ "vaccinator" ])
457 raise Exception ( vaccinator
)
458 return render_template ( "vaccination-view.html" , data
= data
, inventory
= inv
, patient
= pat
, consultant
= consultant
, vaccinator
= vaccinator
, action
= action
)
460 raise Exception ( "Invalid parameter" )
461 elif ( action
== "edit" ):
463 ( ok
, data
)= vaccination
. read ( cursor
, id )
465 raise Exception ( data
)
466 ( ok
, inv
)= inventory
. list ( cursor
, g
. mid
)
469 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
471 raise Exception ( consultant
)
472 ( ok
, vaccinator
)= personnel
. list ( cursor
, g
. mid
, "vaccinator" )
474 raise Exception ( vaccinator
)
475 return render_template ( "vaccination-edit.html" , data
= data
, inventory
= inv
, consultant
= consultant
, vaccinator
= vaccinator
, action
= action
)
477 raise Exception ( "Invalid parameter" )
480 ( ok
, inv
)= inventory
. list ( cursor
, g
. mid
)
483 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
485 raise Exception ( consultant
)
486 ( ok
, vaccinator
)= personnel
. list ( cursor
, g
. mid
, "vaccinator" )
488 raise Exception ( vaccinator
)
489 data
={ "pid" : id , "date" : datetime
. now (). strftime ( "%Y-%m- %d T%H:%M:%S" )}
491 raise Exception ( "Please add a vaccine to inventory before vaccination." )
492 return render_template ( "vaccination-edit.html" , data
= data
, inventory
= inv
, consultant
= consultant
, vaccinator
= vaccinator
, action
= action
)
494 raise Exception ( "Invalid parameter" )
495 except Exception as e
:
497 return render_template ( "error.html" , data
= e
)
499 @app . post ( "/vaccination/new/<pid>" )
500 @app . post ( "/vaccination/edit/<vid>" )
501 def vaccination_edit ( vid
= None , pid
= None ):
502 if (( ret
:= problem ())!= "go" ):
505 cursor
= get_db (). cursor ()
507 given
= "given" in request
. form
. keys ()
508 ( ok
, data
)= vaccination
. create ( cursor
, request
. form
, given
)
510 raise Exception ( data
)
513 given
= "given" in request
. form
. keys ()
514 ( ok
, data
)= vaccination
. update ( cursor
, vid
, request
. form
, given
)
516 raise Exception ( data
)
517 return redirect ( "/vaccination/view/" + str ( vid
))
518 except Exception as e
:
520 return render_template ( "error.html" , data
= e
)
523 @app . get ( "/advice/<action>" )
524 @app . get ( "/advice/<action>/<id>" )
525 def advice_view ( action
, id = None ):
526 if (( ret
:= problem ())!= "go" ):
529 cursor
= get_db (). cursor ()
532 ( ok
, data
)= advice
. list ( cursor
, id )
534 raise exception ( data
)
535 ( ok
, pat
)= patient
. read ( cursor
, id )
537 raise exception ( patient
)
538 return render_template ( "advice-view.html" , data
= data
, patient
= pat
, action
= action
)
540 raise Exception ( "Invalid parameter" )
541 elif ( action
== "view" ):
543 ( ok
, data
)= advice
. read ( cursor
, id )
545 raise Exception ( data
)
546 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
549 ( ok
, consultant
)= personnel
. read ( cursor
, data
[ "consultant" ])
551 raise Exception ( consultant
)
552 return render_template ( "advice-view.html" , data
= data
, patient
= pat
, consultant
= consultant
, action
= action
)
554 raise Exception ( "Invalid parameter" )
555 elif ( action
== "edit" ):
557 ( ok
, data
)= advice
. read ( cursor
, g
. mid
)
559 raise Exception ( data
)
560 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
562 raise Exception ( consultant
)
563 return render_template ( "advice-edit.html" , data
= data
, consultant
= consultant
, action
= action
)
565 raise Exception ( "Invalid parameter" )
568 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
570 raise Exception ( consultant
)
571 data
={ "pid" : id , "date" : datetime
. now (). strftime ( "%Y-%m- %d T%H:%M:%S" )}
572 return render_template ( "advice-edit.html" , data
= data
, consultant
= consultant
, action
= action
)
574 raise Exception ( "Invalid parameter" )
575 except Exception as e
:
577 return render_template ( "error.html" , data
= e
)
579 @app . post ( "/advice/new/<pid>" )
580 @app . post ( "/advice/edit/<aid>" )
581 def advice_edit ( aid
= None , pid
= None ):
582 if (( ret
:= problem ())!= "go" ):
585 cursor
= get_db (). cursor ()
587 ( ok
, data
)= advice
. create ( cursor
, request
. form
, pid
)
589 raise Exception ( data
)
592 ( ok
, data
)= advice
. update ( cursor
, aid
, request
. form
)
594 raise Exception ( data
)
595 return redirect ( "/advice/view/" + str ( aid
))
596 except Exception as e
:
598 return render_template ( "error.html" , data
= e
)
600 @app . get ( "/delete/<cat>/<id>" )
602 if (( ret
:= problem ())!= "go" ):
605 if ( not g
. enable_delete
):
606 raise Exception ( "Deleting record is not allowed." )
607 cursor
= get_db (). cursor ()
609 ( ok
, data
)= advice
. delete ( cursor
, id )
611 return render_template ( "error.html" , data
= data
)
612 elif ( cat
== "inventory" ):
613 ( ok
, data
)= inventory
. delete ( cursor
, id )
615 return render_template ( "error.html" , data
= data
)
617 ( ok
, data
)= multicenter
. delete ( cursor
, id )
619 return render_template ( "error.html" , data
= data
)
620 elif ( cat
== "patient" ):
621 ( ok
, data
)= patient
. delete ( cursor
, id )
623 return render_template ( "error.html" , data
= data
)
624 elif ( cat
== "personnel" ):
625 ( ok
, data
)= personnel
. delete ( cursor
, id )
627 return render_template ( "error.html" , data
= data
)
628 elif ( cat
== "transaction" ):
629 ( ok
, data
)= transaction
. delete ( cursor
, id )
631 return render_template ( "error.html" , data
= data
)
632 elif ( cat
== "vaccination" ):
633 ( ok
, data
)= vaccination
. delete ( cursor
, id )
635 return render_template ( "error.html" , data
= data
)
637 except Exception as e
:
639 return render_template ( "error.html" , data
= e
)
641 @app . route ( "/card/<pid>" )
643 if (( ret
:= problem ())!= "go" ):
646 cursor
= get_db (). cursor ()
647 ( ok
, data
)= patient
. read ( cursor
, pid
)
649 raise Exception ( data
)
650 ( ok
, vac
)= vaccination
. list_by_patient ( cursor
, pid
)
653 ( ok
, adv
)= advice
. list ( cursor
, pid
)
656 ( ok
, center
)= multicenter
. read ( cursor
, g
. mid
)
658 raise Exception ( center
)
659 return render_template ( "card.html" , data
= data
, vaccination
= vac
, advice
= adv
, center
= center
)
660 except Exception as e
:
662 return render_template ( "error.html" , data
= e
)
664 @app . route ( "/report/<mid>" )
666 if (( ret
:= problem ())!= "go" ):
669 date
= request
. args
. get ( "date" , datetime
. now (). strftime ( "%Y-%m- %d " ))
670 cursor
= get_db (). cursor ()
671 ( ok
, data
)= vaccination
. list_by_date ( cursor
, date
)
673 raise Exception ( data
)
674 ( ok
, center
)= multicenter
. read ( cursor
, g
. mid
)
676 raise Exception ( center
)
677 return render_template ( "report.html" , data
= data
, center
= center
, date
= date
)
678 except Exception as e
:
680 return render_template ( "error.html" , data
= e
)
684 db
= getattr ( g
, "_database" , None )
686 db
= g
._ database
= sqlite3
. connect ( "data/database.db" , isolation_level
= None )
687 db
. row_factory
= sqlite3
. Row
690 @app . teardown_appcontext
691 def close_connection ( exception
):
692 db
= getattr ( g
, "_database" , None )
696 def problem ( access
= "" ):
697 if ( auth
. access ()== auth
. auth
. ALL
):
700 return redirect ( "/login" )
702 @app . template_filter ( "format_date" )
703 def format_date ( date
):
706 dt
= datetime
. strptime ( date
, "%Y-%m- %d T%H:%M:%S" )
707 return dt
. strftime ( "%b %d , %Y, %I:%M %p" )
709 dt
= datetime
. strptime ( date
, "%Y-%m- %d " )
710 return dt
. strftime ( "%b %d , %Y" )
715 with app
. app_context ():
717 with app
. open_resource ( "etc/schema.sql" , mode
= "r" ) as f
:
718 db
. cursor (). executescript ( f
. read ())