import gi from mods.utils import disable_widget, enable_widget gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GObject, Gdk from mods.db import db, Reception, Patient, ReceptionDiagnosis, ReceptionAnamnesis, ReceptionObserve from mods.files import new_reception_win_file, reception_row_ui_str, reception_list_settings_win_file, open_reception_win_file, diagnosis_row_ui_str from mods.files import anamnesis_row_ui_str, observe_row_ui_str from mods.settings import s_get_reception_list, s_set_reception_list from mods.diagnosis import create_choose_diagnosis_win, get_giagnosis from mods.lists import get_listrecord, get_list_by_system_id, create_open_list_win, ANAMNEZ_LIST, OBSERVE_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 class ReceptionDiagnosisRow(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 class ReceptionAnamnesisRow(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 class ReceptionObserveRow(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 def get_reception(reception_id): with db.atomic(): return Reception.get_by_id(reception_id) def get_reception_diagnosis_items(reception): with db.atomic(): return ReceptionDiagnosis.select().where(ReceptionDiagnosis.reception == reception) def get_reception_anamnesis_items(reception): with db.atomic(): return ReceptionAnamnesis.select().where(ReceptionAnamnesis.reception == reception) def get_reception_observe_items(reception): with db.atomic(): return ReceptionObserve.select().where(ReceptionObserve.reception == reception) def delete_reception_diagnosisses(reception, diag_id_list): if not len(diag_id_list): return with db.atomic(): ReceptionDiagnosis.delete().where((ReceptionDiagnosis.reception == reception) & (ReceptionDiagnosis.diagnosis.in_(diag_id_list))).execute() def delete_reception_anamnesisses(reception, anam_id_list): if not len(anam_id_list): return with db.atomic(): ReceptionAnamnesis.delete().where((ReceptionAnamnesis.reception == reception) & (ReceptionAnamnesis.anamnesis.in_(anam_id_list))).execute() def delete_reception_observe_symptoms(reception, observe_id_list): if not len(observe_id_list): return with db.atomic(): ReceptionObserve.delete().where((ReceptionObserve.reception == reception) & (ReceptionObserve.symptom.in_(observe_id_list))).execute() def add_reception_diagnosisses(reception, diag_id_list): if not len(diag_id_list): return with db.atomic(): inserts = list(map(lambda x: {'reception': reception, 'diagnosis': get_giagnosis(x)}, diag_id_list)) _ = ReceptionDiagnosis.insert_many(inserts).execute() def add_reception_anamnesisses(reception, anam_id_list): if not len(anam_id_list): return with db.atomic(): inserts = list(map(lambda x: {'reception': reception, 'anamnesis': get_listrecord(x)}, anam_id_list)) _ = ReceptionAnamnesis.insert_many(inserts).execute() def add_reception_observe_symptoms(reception, observe_id_list): if not len(observe_id_list): return with db.atomic(): inserts = list(map(lambda x: {'reception': reception, 'symptom': get_listrecord(x)}, observe_id_list)) _ = ReceptionObserve.insert_many(inserts).execute() def save_reception_diagnosisses(reception, diag_id_list): exist_diags = set(map(lambda x: x.diagnosis.id, get_reception_diagnosis_items(reception))) new_diags = set(diag_id_list) to_del = list(exist_diags - new_diags) to_add = list(new_diags - exist_diags) delete_reception_diagnosisses(reception, to_del) add_reception_diagnosisses(reception, to_add) def save_reception_anamnesisses(reception, anam_id_list): exist_anams = set(map(lambda x: x.anamnesis.id, get_reception_anamnesis_items(reception))) new_anams = set(anam_id_list) to_del = list(exist_anams - new_anams) to_add = list(new_anams - exist_anams) delete_reception_anamnesisses(reception, to_del) add_reception_anamnesisses(reception, to_add) def save_reception_observe(reception, observe_id_list): exist_symptoms = set(map(lambda x: x.symptom.id, get_reception_observe_items(reception))) new_symptoms = set(observe_id_list) to_del = list(exist_symptoms - new_symptoms) to_add = list(new_symptoms - exist_symptoms) delete_reception_observe_symptoms(reception, to_del) add_reception_observe_symptoms(reception, to_add) ################################################################################## 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'{" ".join([reception.patient.last_name, reception.patient.first_name, reception.patient.middle_name])}') #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 build_reception_diagnosis_row(diagnosis): b = Gtk.Builder() b.add_from_string(diagnosis_row_ui_str) win = b.get_object('win') box = b.get_object('diagnosis_box') b.get_object('title').set_text(f'{diagnosis.code:<8} {diagnosis.title}') win.remove(win.get_children()[0]) row = ReceptionDiagnosisRow() row.props.db_id = diagnosis.id row.add(box) return row def build_reception_anamnesis_row(anamnesis): b = Gtk.Builder() b.add_from_string(anamnesis_row_ui_str) win = b.get_object('win') box = b.get_object('anamnesis_box') b.get_object('title').set_text(f'{anamnesis.text}') win.remove(win.get_children()[0]) row = ReceptionAnamnesisRow() row.props.db_id = anamnesis.id row.add(box) return row def build_reception_observe_row(observe): b = Gtk.Builder() b.add_from_string(observe_row_ui_str) win = b.get_object('win') box = b.get_object('observe_box') b.get_object('title').set_text(f'{observe.text}') win.remove(win.get_children()[0]) row = ReceptionObserveRow() row.props.db_id = observe.id 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: def show_choose_diagnosis_win(self, button): choose_diagnosis_win = create_choose_diagnosis_win(b) choose_diagnosis_win.show_all() def show_choose_anamnesis_win(self, button): anam_db_list = get_list_by_system_id(ANAMNEZ_LIST) choose_anamnesis_win = create_open_list_win(anam_db_list.id, choose=True, parent_list=reception_anam_list) choose_anamnesis_win.show_all() def show_choose_observe_win(self, button): observe_db_list = get_list_by_system_id(OBSERVE_LIST) choose_symptom_win = create_open_list_win(observe_db_list.id, choose=True, parent_list=reception_observe_list) choose_symptom_win.show_all() def remove_diagnosis(self, button): row = reception_diag_list.get_selected_row() reception_diag_list.remove(row) disable_widget([remove_diag_button]) reception_diag_list.show_all() def remove_anamnesis(self, button): row = reception_anam_list.get_selected_row() reception_anam_list.remove(row) disable_widget([remove_anam_button]) reception_anam_list.show_all() def remove_observe(self, button): row = reception_observe_list.get_selected_row() reception_observe_list.remove(row) disable_widget([remove_observe_button]) reception_observe_list.show_all() def diagnosis_selected(self, *s): enable_widget([remove_diag_button]) def anamnesis_selected(self, *a): enable_widget([remove_anam_button]) def observe_selected(self, *a): enable_widget([remove_observe_button]) def save(self, button): # Диагнозы diag_id_list = list(map(lambda x: x.props.db_id, reception_diag_list.get_children())) save_reception_diagnosisses(reception, diag_id_list) ####### # Анамнез anam_id_list = list(map(lambda x: x.props.db_id, reception_anam_list.get_children())) save_reception_anamnesisses(reception, anam_id_list) ####### # Осмотр observe_id_list = list(map(lambda x: x.props.db_id, reception_observe_list.get_children())) save_reception_observe(reception, observe_id_list) ####### w.destroy() b.add_from_file(open_reception_win_file) b.connect_signals(OpenReceptionWinHandler()) # patient_fio = b.get_object('patient_fio') patient_fio.set_markup(f'{" ".join([reception.patient.last_name, reception.patient.first_name, reception.patient.middle_name])}') reception_dt = b.get_object('reception_datetime') reception_dt.set_markup(f'{reception_row.props.datetime.strftime("%d.%m.%Y - %H:%M")}') reception_diag_list = b.get_object('diagnosis_list') reception_anam_list = b.get_object('anamnesis_list') reception_observe_list = b.get_object('observe_list') remove_diag_button = b.get_object('remove_diag_button') remove_anam_button = b.get_object('remove_anam_button') remove_observe_button = b.get_object('remove_observe_button') # Анамнез for ra in [x.anamnesis for x in get_reception_anamnesis_items(reception)]: reception_anam_list.add(build_reception_anamnesis_row(ra)) # # Осмотр for ro in [x.symptom for x in get_reception_observe_items(reception)]: reception_observe_list.add(build_reception_observe_row(ro)) # # Диагнозы for rd in [x.diagnosis for x in get_reception_diagnosis_items(reception)]: reception_diag_list.add(build_reception_diagnosis_row(rd)) # 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('test') ##### 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) cal = builder.get_object('reception_cal') cal_date = cal.get_date() d = datetime(cal_date.year, cal_date.month + 1, cal_date.day) redraw_reception_list(d) 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()