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