diff --git a/app.py b/app.py index 41ebfcc..95f30ad 100644 --- a/app.py +++ b/app.py @@ -2,9 +2,11 @@ import gi import os gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GObject, Gdk -from mods.db import store_patient_index, update_patient_index, search_patients, db, Patient, Reception, Catalog +from mods.db import store_patient_index, update_patient_index, search_patients, db, Patient, Reception, Catalog, List from mods.settings import s_get_reception_list, s_set_reception_list from mods.catalogs import add_catalog, search_catalogs +from mods.lists import add_list_record, create_open_list_win +from mods.files import list_row_ui_str from datetime import date, datetime, timedelta import peewee import re @@ -121,6 +123,19 @@ class CatalogRow(Gtk.ListBoxRow): @name.setter def name_setter(self, value): self._name = value +class ListRow(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 name(self): + return self._name + @name.setter + def name_setter(self, value): + self._name = value class MainWinHandler: def main_win_close(self, *args): @@ -147,6 +162,11 @@ class MainWinHandler: row = pl.get_selected_row() open_patient_win = create_open_patient_win(row.props.db_id) open_patient_win.show_all() + def show_open_list_win(self, button): + ll = builder.get_object('list_list') + row = ll.get_selected_row() + open_list_win = create_open_list_win(row.props.db_id) + open_list_win.show_all() def show_new_catalog_win(self, button): new_catalog_win = create_new_catalog_win() new_catalog_win.show_all() @@ -173,6 +193,9 @@ class MainWinHandler: for p in patients: pl.add(build_patient_row(p)) pl.show_all()''' + def list_list_selected(self, *a): + open_button = builder.get_object('list_open_button') + open_button.set_sensitive(True) def catalog_list_selected(self, *a): open_button = builder.get_object('catalog_open_button') rename_button = builder.get_object('catalog_rename_button') @@ -189,6 +212,19 @@ class MainWinHandler: self.catalog_list_unselected() cl.invalidate_filter() +def build_list_row(list_o): + b = Gtk.Builder() + b.add_from_string(list_row_ui_str) + win = b.get_object('win') + box = b.get_object('list_box') + b.get_object('name').set_text(list_o.name) + row = ListRow() + row.props.db_id = list_o.id + row.name = list_o.name + win.remove(win.get_children()[0]) + row.add(box) + return row + def build_catalog_row(catalog): b = Gtk.Builder() b.add_from_string(catalog_row_ui_str) @@ -256,6 +292,10 @@ def patient_filter_func(row): patient_filter.reset() return True return row.props.db_id in patient_filter.filter(fstr) +def list_sort_func(row1, row2, *a): + text1 = row1.props.name + text2 = row2.props.name + return (text1 > text2) - (text1 < text2) def catalog_sort_func(row1, row2, *a): text1 = row1.props.name text2 = row2.props.name @@ -288,6 +328,12 @@ with db.atomic(): for p in Patient.select(): patient_list.add(build_patient_row(p)) ##### +list_list = builder.get_object('list_list') +list_list.set_sort_func(list_sort_func) +with db.atomic(): + for l in List.select(): + list_list.add(build_list_row(l)) +##### catalog_list = builder.get_object('catalog_list') catalog_list.set_sort_func(catalog_sort_func) catalog_list.set_filter_func(catalog_filter_func) @@ -487,11 +533,10 @@ def create_new_catalog_win(): catname = re.sub(r'\s+', ' ', b.get_object('catalog_name').get_text()).strip() if not len(catname): return show_msg('Не указано имя справочника', 'Данное поле должно быть заполнено', level='warn') - with db.atomic(): - try: - catalog = add_catalog(catname) - except peewee.IntegrityError: - return show_msg('Данный справочник уже существует', 'Справочник с указанным названием уже есть с базе данных', level='warn') + try: + catalog = add_catalog(catname) + except peewee.IntegrityError: + return show_msg('Данный справочник уже существует', 'Справочник с указанным названием уже есть с базе данных', level='warn') row = build_catalog_row(catalog) catalog_list.add(row) catalog_list.select_row(row) diff --git a/mods/catalogs.py b/mods/catalogs.py index b51563a..f9024fc 100644 --- a/mods/catalogs.py +++ b/mods/catalogs.py @@ -1,20 +1,16 @@ from mods.db import db, Catalog, CatalogIndex -sys_catalogs = { - 'diagnoz': 'Диагноз', - 'anamnez': 'Анамнез', - 'observ': 'Осмотр', - 'druggs': 'Медикаменты' - } -def add_catalog(name, system_id = ''): - catalog = Catalog.create(name=name, system_id=system_id) - CatalogIndex.insert( - { - CatalogIndex.rowid: catalog.id, - CatalogIndex.text: name - } - ).execute() + +def add_catalog(name): + with db.atomic(): + catalog = Catalog.create(name=name) + CatalogIndex.insert( + { + CatalogIndex.rowid: catalog.id, + CatalogIndex.text: name + } + ).execute() return catalog def search_catalogs(q): @@ -26,8 +22,3 @@ def search_catalogs(q): .where(CatalogIndex.match(q)) .order_by(CatalogIndex.bm25())) -for s_id in sys_catalogs: - with db.atomic(): - q = Catalog.select().where(Catalog.system_id == s_id) - if not len(q): - add_catalog(sys_catalogs[s_id], s_id) diff --git a/mods/db.py b/mods/db.py index 993cf0a..b89d10b 100644 --- a/mods/db.py +++ b/mods/db.py @@ -39,9 +39,19 @@ class Patient(BaseModel): policy_company = TextField(null=True) snils_number = TextField(null=True) notes = TextField(null=True) +class List(BaseModel): + id = AutoIncrementField() + name = TextField() + system_id = TextField() +class ListRecord(BaseModel): + id = AutoIncrementField() + list = ForeignKeyField(List, backref='list_records', on_delete='CASCADE') + text = TextField() +class ListRecordIndex(BaseFTSModel): + rowid = RowIDField() + text = SearchField() class Catalog(BaseModel): id = AutoIncrementField() - system_id = TextField(null=True) name = TextField() class CatalogIndex(BaseFTSModel): rowid = RowIDField() @@ -77,9 +87,13 @@ Patient.add_index( Patient.index(fn.lower(Patient.last_name), fn.lower(Patient.first_name), Patient.birth_date, unique=True, name='patient_first_last_name_birth_date_unique') ) Catalog.add_index(Catalog.index(fn.lower(Catalog.name), unique=True, name='catalog_name_unique')) +ListRecord.add_index(ListRecord.index(ListRecord.list, fn.lower(ListRecord.text), unique=True)) db.create_tables([ Patient, PatientIndex, + List, + ListRecord, + ListRecordIndex, Catalog, CatalogIndex, CatalogRecord, diff --git a/mods/files.py b/mods/files.py new file mode 100644 index 0000000..36ec732 --- /dev/null +++ b/mods/files.py @@ -0,0 +1,11 @@ +import os +resource_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'res') +ui_dir = os.path.join(resource_dir, 'ui') + +list_row_file = os.path.join(ui_dir, 'list_row.glade') +with open(list_row_file, 'r') as f: + list_row_ui_str = f.read() +open_list_win_file = os.path.join(ui_dir, 'open_list_win.glade') +listrecord_row_file = os.path.join(ui_dir, 'listrecord_row.glade') +with open(listrecord_row_file, 'r') as f: + listrecord_row_ui_str = f.read() diff --git a/mods/lists.py b/mods/lists.py new file mode 100644 index 0000000..46e2c79 --- /dev/null +++ b/mods/lists.py @@ -0,0 +1,101 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk, GObject +from mods.db import db, List, ListRecord, ListRecordIndex +from mods.files import open_list_win_file, listrecord_row_ui_str + +lists_map = { + 'diagnoz': 'Диагноз', + 'anamnez': 'Анамнез', + 'observ': 'Осмотр', + 'druggs': 'Медикаменты' + } + +for s_id in lists_map: + with db.atomic(): + q = List.select().where(List.system_id == s_id) + if not len(q): + List.create(name=lists_map[s_id], system_id=s_id) + +class ListRecordRow(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 text(self): + return self._text + @text.setter + def text_setter(self, value): + self._text = value + +def build_listrecord_row(listrec_o): + b = Gtk.Builder() + b.add_from_string(listrecord_row_ui_str) + win = b.get_object('win') + box = b.get_object('listrecord_box') + b.get_object('text').set_text(listrec_o.text) + row = ListRecordRow() + row.props.db_id = listrec_o.id + row.text = listrec_o.text + win.remove(win.get_children()[0]) + row.add(box) + return row + +def get_list(list_id): + with db.atomic(): + return List.get_by_id(list_id) + +def get_all_list_records(list_id): + with db.atomic(): + list_o = List.get_by_id(list_id) + return ListRecord.select().where(ListRecord.list == list_o) +def add_list_record(list_id, text): + with db.atomic(): + list_o = List.get_by_id(list_id) + rec = ListRecord.create(list=list_o, text=text) + ListRecordIndex.insert( + { + ListRecordIndex.rowid: rec.id, + ListRecordIndex.text: text + } + ).execute() + return rec +def delete_list_record(listrec_id): + with db.atomic(): + ListRecord.delete().where(ListRecord.id == listrec_id).execute() + ListRecordIndex.delete().where(ListRecordIndex.rowid == listrec_id).execute() +def change_list_record(listrec_id, new_text): + listrec_o = ListRecord.get_by_id(listrec_id) + if listrec_o.text == new_text: + return False + with db.atomic(): + ListRecord.update(text=new_text).execute() + ListRecordIndex.update(text=new_text).execute() +def search_list_record(list_id, q): + list_o = List.get_by_id(list_id) + with db.atomic(): + return (ListRecord.select() + .join( + ListRecordIndex, + on=(ListRecord.id == ListRecordIndex.rowid)) + .where((ListRecord.list == list_o) & (ListRecordIndex.match(q))) + .order_by(ListRecordIndex.bm25())) + +def create_open_list_win(list_id): + list_o = get_list(list_id) + b = Gtk.Builder() + class OpenListHandler: + pass + b.add_from_file(open_list_win_file) + b.connect_signals(OpenListHandler()) + w = b.get_object('open_list_window') + listrec_list = b.get_object('listrecord_list') + list_header = b.get_object('list_win_header') + list_header.props.title = list_o.name + for lr in get_all_list_records(list_id): + listrec_list.add(build_listrecord_row(lr)) + return w + \ No newline at end of file diff --git a/res/.gitignore b/res/.gitignore new file mode 100644 index 0000000..fb40092 --- /dev/null +++ b/res/.gitignore @@ -0,0 +1 @@ +/ui.glade~ diff --git a/res/ui/.gitignore b/res/ui/.gitignore new file mode 100644 index 0000000..73006ac --- /dev/null +++ b/res/ui/.gitignore @@ -0,0 +1,9 @@ +/edit_patient_win.glade~ +/error_win.glade~ +/female_patient_row.glade~ +/info_win.glade~ +/main_win.glade~ +/male_patient_row.glade~ +/new_patient_win.glade~ +/open_patient_win.glade~ +/warn_win.glade~ diff --git a/res/ui/list_row.glade b/res/ui/list_row.glade new file mode 100644 index 0000000..6f49541 --- /dev/null +++ b/res/ui/list_row.glade @@ -0,0 +1,34 @@ + + + + + + False + False + + + + + + True + False + + + True + False + label + 0 + + + + + + True + True + 1 + + + + + + diff --git a/res/ui/listrecord_list_row.glade b/res/ui/listrecord_list_row.glade new file mode 100644 index 0000000..f60f56a --- /dev/null +++ b/res/ui/listrecord_list_row.glade @@ -0,0 +1,34 @@ + + + + + + False + False + + + + + + True + False + + + True + False + label + 0 + + + + + + True + True + 1 + + + + + + diff --git a/res/ui/listrecord_row.glade b/res/ui/listrecord_row.glade new file mode 100644 index 0000000..6f49541 --- /dev/null +++ b/res/ui/listrecord_row.glade @@ -0,0 +1,34 @@ + + + + + + False + False + + + + + + True + False + + + True + False + label + 0 + + + + + + True + True + 1 + + + + + + diff --git a/res/ui/main_win.glade b/res/ui/main_win.glade index fb7e71f..1e0d9c7 100644 --- a/res/ui/main_win.glade +++ b/res/ui/main_win.glade @@ -324,10 +324,74 @@ page0 - Паценты + Пациенты 1 + + + True + False + vertical + + + True + False + both + + + True + False + False + Открыть + True + gtk-open + + + + False + True + + + + + False + True + 0 + + + + + True + True + in + + + True + False + + + True + False + + + + + + + + True + True + 1 + + + + + page3 + Списки + 2 + + True @@ -468,7 +532,7 @@ page2 Справочники - 2 + 3 diff --git a/res/ui/new_catalog_win.glade b/res/ui/new_catalog_win.glade new file mode 100644 index 0000000..a5d0916 --- /dev/null +++ b/res/ui/new_catalog_win.glade @@ -0,0 +1,69 @@ + + + + + + False + True + center + dialog + + + True + False + Новый справочник + True + + + True + True + True + + + + True + False + document-save-symbolic + + + + + + + + + True + False + 5 + 5 + 5 + 5 + 0 + + + True + False + 5 + 5 + 12 + 5 + + + True + True + 30 + + + + + + + True + False + Имя нового справочника + + + + + + diff --git a/res/ui/open_list_win.glade b/res/ui/open_list_win.glade new file mode 100644 index 0000000..d904964 --- /dev/null +++ b/res/ui/open_list_win.glade @@ -0,0 +1,78 @@ + + + + + + False + True + center + 800 + 500 + dialog + + + True + False + True + + + gtk-add + True + True + True + True + True + + + + + gtk-edit + True + False + True + True + True + True + + + 1 + + + + + gtk-remove + True + False + True + True + True + True + + + 2 + + + + + + + True + True + never + in + + + True + False + + + True + False + + + + + + + +