eldoc/mods/receptions.py

125 lines
5.2 KiB
Python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, Gdk
from mods.db import db, Reception
from mods.files import new_reception_win_file, reception_row_ui_str, reception_list_settings_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 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 patient_db_id(self):
return self._patient_db_id
@patient_db_id.setter
def db_id_setter(self, value):
self._patient_db_id = value
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():
reception = Reception.select().where(Reception.time == reception_datetime)
row = ReceptionRow()
if len(reception):
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.5, green=0.7, blue=0.5, alpha=1.0))
reception_patient = Gtk.Label()
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_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()
b.add_from_file(new_reception_win_file)
b.connect_signals(NewReceptionWinHandler())
#####
patient_label = NewReceptionPatientLabel()
patient_label.props.patient_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('%m.%d.%Y - %H:%M'))
return w
def create_reception_list_settings_win():
b = Gtk.Builder()
class ReceptionListSettingsHandler:
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()