]>
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
, g
9 from urllib
. parse
import urlencode
10 from datetime
import datetime
12 import advice
, auth
, inventory
, multicenter
, patient
, personnel
, transaction
, vaccination
14 from config
import cid
17 app
. config
. from_file ( "config/config.json" , load
= json
. load
)
21 g
. user
= session
. get ( "user" , None )
22 g
. mid
= session
. get ( "mid" , None )
23 g
. center
= session
. get ( "center" , None )
24 g
. enable_delete
= app
. config
. get ( "ENABLE_DELETE" , False )
29 if (( ret
:= problem ())!= "go" ):
31 cursor
= get_db (). cursor ()
32 ( ok
, mc
)= multicenter
. read ( cursor
, session
[ "mid" ])
35 ( ok
, pat
)= patient
. list ( cursor
)
38 return render_template ( "index.html" , center
= mc
, patient
= pat
)
39 except Exception as e
:
41 return render_template ( "error.html" , data
= e
)
44 def login_get ( error
= False ):
46 cursor
= get_db (). cursor ()
47 ( ok
, data
)= multicenter
. list ( cursor
)
52 return render_template ( "login.html" , data
= data
, error
= error
)
53 except Exception as e
:
54 return render_template ( "error.html" , data
= e
)
58 cursor
= get_db (). cursor ()
59 if ( auth
. login ( cursor
, request
. form
[ "user" ], request
. form
[ "password" ], request
. form
[ "center" ])):
62 return login_get ( True )
67 return redirect ( "/login" )
71 cursor
= get_db (). cursor ()
72 ( ok
, mc
)= multicenter
. list ( cursor
)
75 users
= auth
. list ( cursor
)
76 return render_template ( "admin.html" , users
= users
, center
= mc
, msg
= msg
)
81 cursor
= get_db (). cursor ()
82 if ( auth
. new ( cursor
, request
. form
[ "user" ], request
. form
[ "pwd" ])):
83 return admin ( msg
= "New user added" )
85 return admin ( msg
= "Failed to add" )
86 except Exception as e
:
88 return render_template ( "error.html" , data
= e
)
90 @app . post ( "/changepass" )
93 cursor
= get_db (). cursor ()
94 if ( "user" in request
. form
. keys ()):
95 if ( auth
. change ( cursor
, request
. form
[ "user" ], request
. form
[ "new" ])):
96 return admin ( msg
= "Password changed" )
98 return admin ( msg
= "Incorrect Password" )
99 elif ( request
. form
[ "new" ]== request
. form
[ "check" ]):
100 user
= request
. form
. get ( "user" , session
[ "user" ])
101 if ( auth
. change ( cursor
, user
, request
. form
[ "new" ], request
. form
[ "old" ])):
102 return admin ( msg
= "Password changed" )
104 return admin ( msg
= "Incorrect Password" )
106 return admin ( msg
= "Failed to change password" )
107 except Exception as e
:
109 return render_template ( "error.html" , data
= e
)
111 @app . post ( "/switchcenter" )
114 cursor
= get_db (). cursor ()
115 if ( auth
. switch ( cursor
, request
. form
)):
116 return admin ( msg
= "Switched center to " + g
. center
)
118 raise Exception ( "Failed to switch" )
119 except Exception as e
:
121 return render_template ( "error.html" , data
= e
)
123 @app . get ( "/center/<action>" )
124 @app . get ( "/center/<action>/<mid>" )
125 def center_view ( action
, mid
= None ):
126 if (( ret
:= problem ())!= "go" ):
129 cursor
= get_db (). cursor ()
131 ( ok
, data
)= multicenter
. list ( cursor
)
133 raise exception ( data
)
134 return render_template ( "multicenter-view.html" , data
= data
, action
= action
)
135 elif ( action
== "view" ):
137 ( ok
, data
)= multicenter
. read ( cursor
, mid
)
139 raise Exception ( data
)
140 ( ok
, invt
)= inventory
. list ( cursor
, mid
)
142 raise Exception ( invt
)
143 ( ok
, pers
)= personnel
. list ( cursor
, mid
)
145 raise Exception ( pers
)
146 return render_template ( "multicenter-view.html" , data
= data
, inventory
= invt
, personnel
= pers
, action
= action
)
148 raise Exception ( "Invalid parameter" )
149 elif ( action
== "edit" ):
151 ( ok
, data
)= multicenter
. read ( cursor
, mid
)
153 raise Exception ( data
)
154 return render_template ( "multicenter-edit.html" , data
= data
, action
= action
)
156 raise Exception ( "Invalid parameter" )
158 return render_template ( "multicenter-edit.html" , data
=[], action
= action
)
159 except Exception as e
:
161 return render_template ( "error.html" , data
= e
)
163 @app . get ( "/personnel/<action>" )
164 @app . get ( "/personnel/<action>/<sid>" )
165 def personnel_view ( action
, sid
= None ):
166 if (( ret
:= problem ())!= "go" ):
169 cursor
= get_db (). cursor ()
171 ( ok
, data
)= personnel
. list ( cursor
, g
. mid
)
173 raise exception ( data
)
174 return render_template ( "personnel-view.html" , data
= data
, action
= action
)
175 elif ( action
== "view" ):
177 ( ok
, data
)= personnel
. read ( cursor
, sid
)
179 raise Exception ( data
)
180 return render_template ( "personnel-view.html" , data
= data
, action
= action
)
182 raise Exception ( "Invalid parameter" )
183 elif ( action
== "edit" ):
185 ( ok
, data
)= personnel
. read ( cursor
, sid
)
187 raise Exception ( data
)
188 ( ok
, center
)= multicenter
. list ( cursor
)
190 raise Exception ( center
)
191 return render_template ( "personnel-edit.html" , data
= data
, center
= center
, action
= action
)
193 raise Exception ( "Invalid parameter" )
195 ( ok
, center
)= multicenter
. list ( cursor
)
197 raise Exception ( center
)
198 return render_template ( "personnel-edit.html" , data
=[], center
= center
, action
= action
)
199 except Exception as e
:
201 return render_template ( "error.html" , data
= e
)
203 @app . post ( "/personnel/new" )
204 @app . post ( "/personnel/edit/<sid>" )
205 def personnel_edit ( sid
= None ):
206 if (( ret
:= problem ())!= "go" ):
209 cursor
= get_db (). cursor ()
211 active
= "active" in request
. form
. keys ()
212 ( ok
, data
)= personnel
. create ( cursor
, request
. form
, active
)
214 raise Exception ( data
)
217 active
= "active" in request
. form
. keys ()
218 ( ok
, data
)= personnel
. update ( cursor
, sid
, request
. form
, active
)
220 raise Exception ( data
)
221 return redirect ( "/personnel/view/" + str ( sid
))
222 except Exception as e
:
223 return render_template ( "error.html" , data
= e
)
225 @app . post ( "/center/new" )
226 @app . post ( "/center/edit/<mid>" )
227 def center_edit ( mid
= None ):
228 if (( ret
:= problem ())!= "go" ):
231 cursor
= get_db (). cursor ()
233 ( ok
, data
)= multicenter
. create ( cursor
, request
. form
)
235 raise Exception ( data
)
238 ( ok
, data
)= multicenter
. update ( cursor
, mid
, request
. form
)
240 raise Exception ( data
)
241 return redirect ( "/center/view/" + str ( mid
))
242 except Exception as e
:
243 return render_template ( "error.html" , data
= e
)
245 @app . get ( "/inventory/<action>" )
246 @app . get ( "/inventory/<action>/<id>" )
247 def inventory_view ( action
, id = None ):
248 if (( ret
:= problem ())!= "go" ):
251 cursor
= get_db (). cursor ()
254 ( ok
, data
)= inventory
. list ( cursor
, id )
256 raise exception ( data
)
257 ( ok
, center
)= multicenter
. read ( cursor
, id )
259 raise exception ( center
)
260 return render_template ( "inventory-view.html" , data
= data
, center
= center
, action
= action
)
262 raise Exception ( "Invalid parameter" )
263 elif ( action
== "view" ):
265 ( ok
, data
)= inventory
. read ( cursor
, id )
267 raise Exception ( data
)
268 return render_template ( "inventory-view.html" , data
= data
, action
= action
)
270 raise Exception ( "Invalid parameter" )
271 elif ( action
== "edit" ):
273 ( ok
, data
)= inventory
. read ( cursor
, id )
275 raise Exception ( data
)
276 return render_template ( "inventory-edit.html" , data
= data
, mid
= data
[ "mid" ], action
= action
)
278 raise Exception ( "Invalid parameter" )
281 ( ok
, center
)= multicenter
. list ( cursor
)
283 raise Exception ( center
)
284 return render_template ( "inventory-edit.html" , data
=[], mid
= id , action
= action
, center
= center
)
286 raise Exception ( "Invalid parameter" )
287 except Exception as e
:
289 return render_template ( "error.html" , data
= e
)
291 @app . post ( "/inventory/new/<mid>" )
292 @app . post ( "/inventory/edit/<iid>" )
293 def inventory_edit ( mid
= None , iid
= None ):
294 if (( ret
:= problem ())!= "go" ):
297 cursor
= get_db (). cursor ()
299 ( ok
, data
)= inventory
. create ( cursor
, request
. form
)
301 raise Exception ( data
)
304 ( ok
, data
)= inventory
. update ( cursor
, iid
, request
. form
)
306 raise Exception ( data
)
307 return redirect ( "/inventory/view/" + str ( iid
))
308 except Exception as e
:
310 return render_template ( "error.html" , data
= e
)
312 @app . get ( "/patient/<action>" )
313 @app . get ( "/patient/<action>/<pid>" )
314 def patient_view ( action
, pid
= None ):
315 if (( ret
:= problem ())!= "go" ):
318 cursor
= get_db (). cursor ()
321 page
= int ( request
. args
[ "page" ])
324 if ( request
. args
. get ( "all" , False )):
325 ( ok
, data
)= patient
. list ( cursor
, request
. args
, g
. mid
)
327 ( ok
, data
)= patient
. list ( cursor
, request
. args
)
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
))
333 raise Exception ( data
)
334 elif ( action
== "view" ):
336 ( ok
, data
)= patient
. read ( cursor
, pid
)
338 raise Exception ( data
)
339 ( ok
, vac
)= vaccination
. list_by_patient ( cursor
, pid
)
342 ( ok
, adv
)= advice
. list ( cursor
, pid
)
346 return render_template ( "patient-view.html" , data
= data
, vaccination
= vac
, advice
= adv
, action
= action
)
348 raise Exception ( "Invalid parameter" )
349 elif ( action
== "edit" ):
351 ( ok
, data
)= patient
. read ( cursor
, pid
)
353 raise Exception ( data
)
354 ( ok
, center
)= multicenter
. list ( cursor
)
356 raise exception ( center
)
357 return render_template ( "patient-edit.html" , data
= data
, center
= center
, action
= action
)
359 raise Exception ( "Invalid parameter" )
361 ( ok
, last
)= patient
. list ( cursor
)
363 raise exception ( last
)
364 data
={ "cid" : cid
. new ( last
)}
365 ( ok
, center
)= multicenter
. list ( cursor
)
367 raise exception ( center
)
368 return render_template ( "patient-edit.html" , data
= data
, center
= center
, action
= action
)
369 except Exception as e
:
371 return render_template ( "error.html" , data
= e
)
373 @app . post ( "/patient/new" )
374 @app . post ( "/patient/edit/<pid>" )
375 def patient_edit ( pid
= None ):
376 if (( ret
:= problem ())!= "go" ):
379 cursor
= get_db (). cursor ()
381 ( ok
, data
)= patient
. create ( cursor
, request
. form
)
383 raise Exception ( data
)
386 ( ok
, data
)= patient
. update ( cursor
, pid
, request
. form
)
388 raise Exception ( data
)
389 return redirect ( "/patient/view/" + str ( pid
))
390 except Exception as e
:
391 return render_template ( "error.html" , data
= e
)
393 @app . get ( "/note/<pid>" )
395 if (( ret
:= problem ())!= "go" ):
398 cursor
= get_db (). cursor ()
399 ( ok
, data
)= patient
. read ( cursor
, pid
)
401 return render_template ( "note.html" , data
= data
)
403 return render_template ( "error.html" , data
= data
)
404 except Exception as e
:
406 return render_template ( "error.html" , data
= e
)
408 @app . post ( "/note/<pid>" )
410 if (( ret
:= problem ())!= "go" ):
413 cursor
= get_db (). cursor ()
414 ( ok
, data
)= patient
. note ( cursor
, pid
, request
. form
)
416 return render_template ( "error.html" , data
= data
)
417 return redirect ( "/patient/view/" + str ( pid
))
418 except Exception as e
:
420 return render_template ( "error.html" , data
= e
)
422 @app . get ( "/vaccination/<action>" )
423 @app . get ( "/vaccination/<action>/<id>" )
424 def vaccination_view ( action
, id = None ):
425 if (( ret
:= problem ())!= "go" ):
428 cursor
= get_db (). cursor ()
431 ( ok
, data
)= vaccination
. list_by_patient ( cursor
, id )
433 raise exception ( data
)
434 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
436 raise exception ( patient
)
437 ( ok
, inv
)= inventory
. list ( cursor
, data
[ "iid" ])
439 raise exception ( inventory
)
440 return render_template ( "vaccination-view.html" , data
= data
, patient
= pat
, inventory
= inv
, action
= action
)
442 raise Exception ( "Invalid parameter" )
443 elif ( action
== "view" ):
445 ( ok
, data
)= vaccination
. read ( cursor
, id )
447 raise Exception ( data
)
448 ( ok
, inv
)= inventory
. read ( cursor
, data
[ "iid" ])
450 raise Exception ( inventory
)
451 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
453 raise Exception ( patient
)
454 ( ok
, consultant
)= personnel
. read ( cursor
, data
[ "consultant" ])
456 raise Exception ( consultant
)
457 ( ok
, vaccinator
)= personnel
. read ( cursor
, data
[ "vaccinator" ])
459 raise Exception ( vaccinator
)
460 return render_template ( "vaccination-view.html" , data
= data
, inventory
= inv
, patient
= pat
, consultant
= consultant
, vaccinator
= vaccinator
, action
= action
)
462 raise Exception ( "Invalid parameter" )
463 elif ( action
== "edit" ):
465 ( ok
, data
)= vaccination
. read ( cursor
, g
. mid
)
467 raise Exception ( data
)
468 ( ok
, inv
)= inventory
. list ( cursor
, g
. mid
)
471 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
473 raise Exception ( consultant
)
474 ( ok
, vaccinator
)= personnel
. list ( cursor
, g
. mid
, "vaccinator" )
476 raise Exception ( vaccinator
)
477 return render_template ( "vaccination-edit.html" , data
= data
, inventory
= inv
, consultant
= consultant
, vaccinator
= vaccinator
, action
= action
)
479 raise Exception ( "Invalid parameter" )
482 ( ok
, inv
)= inventory
. list ( cursor
, g
. mid
)
485 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
487 raise Exception ( consultant
)
488 ( ok
, vaccinator
)= personnel
. list ( cursor
, g
. mid
, "vaccinator" )
490 raise Exception ( vaccinator
)
491 data
={ "pid" : id , "date" : datetime
. now (). strftime ( "%Y-%m- %d T%H:%M:%S" )}
493 raise Exception ( "Please add a vaccine to inventory before vaccination." )
494 return render_template ( "vaccination-edit.html" , data
= data
, inventory
= inv
, consultant
= consultant
, vaccinator
= vaccinator
, action
= action
)
496 raise Exception ( "Invalid parameter" )
497 except Exception as e
:
499 return render_template ( "error.html" , data
= e
)
501 @app . post ( "/vaccination/new/<pid>" )
502 @app . post ( "/vaccination/edit/<vid>" )
503 def vaccination_edit ( vid
= None , pid
= None ):
504 if (( ret
:= problem ())!= "go" ):
507 cursor
= get_db (). cursor ()
509 given
= "given" in request
. form
. keys ()
510 ( ok
, data
)= vaccination
. create ( cursor
, request
. form
, given
)
512 raise Exception ( data
)
515 given
= "given" in request
. form
. keys ()
516 ( ok
, data
)= vaccination
. update ( cursor
, vid
, request
. form
, given
)
518 raise Exception ( data
)
519 return redirect ( "/vaccination/view/" + str ( vid
))
520 except Exception as e
:
522 return render_template ( "error.html" , data
= e
)
525 @app . get ( "/advice/<action>" )
526 @app . get ( "/advice/<action>/<id>" )
527 def advice_view ( action
, id = None ):
528 if (( ret
:= problem ())!= "go" ):
531 cursor
= get_db (). cursor ()
534 ( ok
, data
)= advice
. list ( cursor
, id )
536 raise exception ( data
)
537 ( ok
, pat
)= patient
. read ( cursor
, id )
539 raise exception ( patient
)
540 return render_template ( "advice-view.html" , data
= data
, patient
= pat
, action
= action
)
542 raise Exception ( "Invalid parameter" )
543 elif ( action
== "view" ):
545 ( ok
, data
)= advice
. read ( cursor
, id )
547 raise Exception ( data
)
548 ( ok
, pat
)= patient
. read ( cursor
, data
[ "pid" ])
551 ( ok
, consultant
)= personnel
. read ( cursor
, data
[ "consultant" ])
553 raise Exception ( consultant
)
554 return render_template ( "advice-view.html" , data
= data
, patient
= pat
, consultant
= consultant
, action
= action
)
556 raise Exception ( "Invalid parameter" )
557 elif ( action
== "edit" ):
559 ( ok
, data
)= advice
. read ( cursor
, g
. mid
)
561 raise Exception ( data
)
562 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
564 raise Exception ( consultant
)
565 return render_template ( "advice-edit.html" , data
= data
, consultant
= consultant
, action
= action
)
567 raise Exception ( "Invalid parameter" )
570 ( ok
, consultant
)= personnel
. list ( cursor
, g
. mid
, "consultant" )
572 raise Exception ( consultant
)
573 data
={ "pid" : id , "date" : datetime
. now (). strftime ( "%Y-%m- %d T%H:%M:%S" )}
574 return render_template ( "advice-edit.html" , data
= data
, consultant
= consultant
, action
= action
)
576 raise Exception ( "Invalid parameter" )
577 except Exception as e
:
579 return render_template ( "error.html" , data
= e
)
581 @app . post ( "/advice/new/<pid>" )
582 @app . post ( "/advice/edit/<aid>" )
583 def advice_edit ( aid
= None , pid
= None ):
584 if (( ret
:= problem ())!= "go" ):
587 cursor
= get_db (). cursor ()
589 ( ok
, data
)= advice
. create ( cursor
, request
. form
, pid
)
591 raise Exception ( data
)
594 ( ok
, data
)= advice
. update ( cursor
, aid
, request
. form
)
596 raise Exception ( data
)
597 return redirect ( "/advice/view/" + str ( aid
))
598 except Exception as e
:
600 return render_template ( "error.html" , data
= e
)
602 @app . get ( "/delete/<cat>/<id>" )
604 if (( ret
:= problem ())!= "go" ):
607 if ( not g
. enable_delete
):
608 raise Exception ( "Deleting record is not allowed." )
609 cursor
= get_db (). cursor ()
611 ( ok
, data
)= advice
. delete ( cursor
, id )
613 return render_template ( "error.html" , data
= data
)
614 elif ( cat
== "inventory" ):
615 ( ok
, data
)= inventory
. delete ( cursor
, id )
617 return render_template ( "error.html" , data
= data
)
619 ( ok
, data
)= multicenter
. delete ( cursor
, id )
621 return render_template ( "error.html" , data
= data
)
622 elif ( cat
== "patient" ):
623 ( ok
, data
)= patient
. delete ( cursor
, id )
625 return render_template ( "error.html" , data
= data
)
626 elif ( cat
== "personnel" ):
627 ( ok
, data
)= personnel
. delete ( cursor
, id )
629 return render_template ( "error.html" , data
= data
)
630 elif ( cat
== "transaction" ):
631 ( ok
, data
)= transaction
. delete ( cursor
, id )
633 return render_template ( "error.html" , data
= data
)
634 elif ( cat
== "vaccination" ):
635 ( ok
, data
)= vaccination
. delete ( cursor
, id )
637 return render_template ( "error.html" , data
= data
)
639 except Exception as e
:
641 return render_template ( "error.html" , data
= e
)
643 @app . route ( "/card/<pid>" )
645 if (( ret
:= problem ())!= "go" ):
648 cursor
= get_db (). cursor ()
649 ( ok
, data
)= patient
. read ( cursor
, pid
)
651 raise Exception ( data
)
652 ( ok
, vac
)= vaccination
. list_by_patient ( cursor
, pid
)
655 ( ok
, adv
)= advice
. list ( cursor
, pid
)
658 ( ok
, center
)= multicenter
. read ( cursor
, g
. mid
)
660 raise Exception ( center
)
661 return render_template ( "card.html" , data
= data
, vaccination
= vac
, advice
= adv
, center
= center
)
662 except Exception as e
:
664 return render_template ( "error.html" , data
= e
)
666 @app . route ( "/report/<mid>" )
668 if (( ret
:= problem ())!= "go" ):
671 date
= request
. args
. get ( "date" , datetime
. now (). strftime ( "%Y-%m- %d " ))
672 cursor
= get_db (). cursor ()
673 ( ok
, data
)= vaccination
. list_by_date ( cursor
, date
)
675 raise Exception ( data
)
676 ( ok
, center
)= multicenter
. read ( cursor
, g
. mid
)
678 raise Exception ( center
)
679 return render_template ( "report.html" , data
= data
, center
= center
, date
= date
)
680 except Exception as e
:
682 return render_template ( "error.html" , data
= e
)
686 db
= getattr ( g
, "_database" , None )
688 db
= g
._ database
= sqlite3
. connect ( "data/database.db" , isolation_level
= None )
689 db
. row_factory
= sqlite3
. Row
692 @app . teardown_appcontext
693 def close_connection ( exception
):
694 db
= getattr ( g
, "_database" , None )
698 def problem ( access
= "" ):
699 if ( auth
. access ()== auth
. auth
. ALL
):
702 return redirect ( "/login" )
704 @app . template_filter ( "format_date" )
705 def format_date ( date
):
708 dt
= datetime
. strptime ( date
, "%Y-%m- %d T%H:%M:%S" )
709 return dt
. strftime ( "%b %d , %Y, %I:%M %p" )
711 dt
= datetime
. strptime ( date
, "%Y-%m- %d " )
712 return dt
. strftime ( "%b %d , %Y" )
717 with app
. app_context ():
719 with app
. open_resource ( "etc/schema.sql" , mode
= "r" ) as f
:
720 db
. cursor (). executescript ( f
. read ())