eldoc/mods/receptions.py

165 lines
6.9 KiB
Python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, Gdk
from mods.db import db, Reception, Patient
from mods.files import new_reception_win_file, reception_row_ui_str, reception_list_settings_win_file, open_reception_win_file
from mods.settings import s_get_reception_list, s_set_reception_list
from mods.root import builder
from datetime import datetime, timedelta
def add_reception(patient_id, reception_dt):
with db.atomic():
patient = Patient.get_by_id(patient_id)
return Reception.create(patient=patient, time=reception_dt)
def get_reception_timelist(rec_date):
s = s_get_reception_list()
dstart = datetime(rec_date.year, rec_date.month, rec_date.day, s.day_start[0], s.day_start[1])
dend = datetime(rec_date.year, rec_date.month, rec_date.day, s.day_end[0], s.day_end[1])
work_day_minutes_total = (dend.hour - dstart.hour) * 60 + dend.minute - dstart.minute
shift_minutes_range = range(0, work_day_minutes_total, s.interval)
return [dstart + timedelta(minutes=x) for x in shift_minutes_range]
class ReceptionRow(Gtk.ListBoxRow):
@GObject.Property
def db_id(self):
return self._db_id
@db_id.setter
def db_id_setter(self, value):
self._db_id = value
@GObject.Property
def datetime(self):
return self._datetime
@datetime.setter
def datetime_setter(self, value):
self._datetime = value
@GObject.Property
def scheduled(self):
return self._scheduled
@scheduled.setter
def scheduled_setter(self, value):
self._scheduled = value
class NewReceptionPatientLabel(Gtk.Label):
@GObject.Property
def db_id(self):
return self._db_id
@db_id.setter
def db_id_setter(self, value):
self._db_id = value
def get_reception(reception_id):
with db.atomic():
return Reception.get_by_id(reception_id)
def build_reception_row(reception_datetime):
b = Gtk.Builder()
b.add_from_string(reception_row_ui_str)
win = b.get_object('win')
box = b.get_object('reception_box')
b.get_object('hour').set_text(reception_datetime.strftime('%H'))
b.get_object('minute').set_text(reception_datetime.strftime('%M'))
with db.atomic():
q = Reception.select().where(Reception.time == reception_datetime)
row = ReceptionRow()
if len(q):
reception = q.get()
row.props.scheduled = True
row.props.db_id = reception.id
reception_cont = b.get_object('reception_cont')
reception_cont.props.border_width = 2
reception_cont.override_background_color(Gtk.StateFlags(0), Gdk.RGBA(red=0.6, green=0.8, blue=0.6, alpha=1.0))
reception_patient = Gtk.Label()
reception_patient.set_markup(f'<span size="xx-large">{" ".join([reception.patient.last_name, reception.patient.first_name, reception.patient.middle_name])}</span>')
#reception_patient.set_text(' '.join([reception.patient.last_name, reception.patient.first_name, reception.patient.middle_name]))
reception_cont.add(reception_patient)
else:
row.props.scheduled = False
win.remove(win.get_children()[0])
row.props.datetime = reception_datetime
row.add(box)
return row
def create_open_reception_win():
b = Gtk.Builder()
reception_list = builder.get_object('reception_list')
reception_row = reception_list.get_selected_row()
reception = get_reception(reception_row.props.db_id)
class OpenReceptionWinHandler:
pass
b.add_from_file(open_reception_win_file)
b.connect_signals(OpenReceptionWinHandler())
#
patient_fio = b.get_object('patient_fio')
patient_fio.set_markup(f'<span size="x-large">{" ".join([reception.patient.last_name, reception.patient.first_name, reception.patient.middle_name])}</span>')
reception_dt = b.get_object('reception_datetime')
reception_dt.set_markup(f'<span size="x-large">{reception_row.props.datetime.strftime("%d.%m.%Y - %H:%M")}</span>')
#
w = b.get_object('open_reception_window')
return w
def create_new_reception_win():
b = Gtk.Builder()
reception_list = builder.get_object('reception_list')
row = reception_list.get_selected_row()
class NewReceptionWinHandler:
def show_choose_patient_win(self, button):
from mods.patients import create_choose_patient_win
choose_patient_win = create_choose_patient_win(b)
choose_patient_win.show_all()
def save_reception(self, button):
pat_label = patient_cont.get_children()[0]
if pat_label.props.db_id < 0:
return
_ = add_reception(pat_label.props.db_id, row.props.datetime)
w.destroy()
redraw_reception_list(row.props.datetime)
b.add_from_file(new_reception_win_file)
b.connect_signals(NewReceptionWinHandler())
#####
patient_label = NewReceptionPatientLabel()
patient_label.props.db_id = -1
#patient_label.set_markup('<span size="xx-large">test</span>')
#####
patient_cont = b.get_object('patient')
patient_cont.pack_start(patient_label, True, True, 0)
patient_cont.reorder_child(patient_label, 0)
w = b.get_object('new_reception_window')
dt_label = b.get_object('datetime')
dt_label.set_text(row.props.datetime.strftime('%d.%m.%Y - %H:%M'))
return w
def create_reception_list_settings_win():
b = Gtk.Builder()
class ReceptionListSettingsHandler:
def leading_zeroes(self, entry):
adj = entry.get_adjustment()
val = int(adj.get_value())
entry.set_text(f'{val:02d}')
return True
def save_settings(self, *a):
start_hour = int(b.get_object('start_hour').get_value())
start_minute = int(b.get_object('start_minute').get_value())
end_hour = int(b.get_object('end_hour').get_value())
end_minute = int(b.get_object('end_minute').get_value())
interval = int(b.get_object('interval').get_value())
s_set_reception_list([start_hour, start_minute], [end_hour, end_minute], interval)
redraw_reception_list(datetime.now())
b.get_object('reception_list_settings_win').close()
b.add_from_file(reception_list_settings_win_file)
b.connect_signals(ReceptionListSettingsHandler())
s = s_get_reception_list()
b.get_object('start_hour').set_value(s.day_start[0])
b.get_object('start_minute').set_value(s.day_start[1])
b.get_object('end_hour').set_value(s.day_end[0])
b.get_object('end_minute').set_value(s.day_end[1])
b.get_object('interval').set_value(s.interval)
w = b.get_object('reception_list_settings_win')
return w
def redraw_reception_list(selected_date):
reception_timelist = get_reception_timelist(selected_date)
reception_list = builder.get_object('reception_list')
for c in reception_list.get_children():
reception_list.remove(c)
for d in reception_timelist:
reception_list.add(build_reception_row(d))
reception_list.show_all()