Списки заполнения. часть

This commit is contained in:
Бородин Роман 2019-11-23 23:36:26 +03:00
parent fbafb5b5ee
commit 3731153226
13 changed files with 513 additions and 28 deletions

57
app.py
View File

@ -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)

View File

@ -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)

View File

@ -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,

11
mods/files.py Normal file
View File

@ -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()

101
mods/lists.py Normal file
View File

@ -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

1
res/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/ui.glade~

9
res/ui/.gitignore vendored Normal file
View File

@ -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~

34
res/ui/list_row.glade Normal file
View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="win">
<property name="can_focus">False</property>
<property name="decorated">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox" id="list_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="name">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
<property name="xalign">0</property>
<attributes>
<attribute name="scale" value="2"/>
</attributes>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="win">
<property name="can_focus">False</property>
<property name="decorated">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox" id="listrecord_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="text">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
<property name="xalign">0</property>
<attributes>
<attribute name="scale" value="2"/>
</attributes>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="win">
<property name="can_focus">False</property>
<property name="decorated">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox" id="list_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="name">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
<property name="xalign">0</property>
<attributes>
<attribute name="scale" value="2"/>
</attributes>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

@ -324,10 +324,74 @@
</object>
<packing>
<property name="name">page0</property>
<property name="title" translatable="yes">Паценты</property>
<property name="title" translatable="yes">Пациенты</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="list_page">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkToolbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="toolbar_style">both</property>
<child>
<object class="GtkToolButton" id="list_open_button">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Открыть</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-open</property>
<signal name="clicked" handler="show_open_list_win" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkListBox" id="list_list">
<property name="visible">True</property>
<property name="can_focus">False</property>
<signal name="row-selected" handler="list_list_selected" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="name">page3</property>
<property name="title" translatable="yes">Списки</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkBox" id="catalog_page">
<property name="visible">True</property>
@ -468,7 +532,7 @@
<packing>
<property name="name">page2</property>
<property name="title" translatable="yes">Справочники</property>
<property name="position">2</property>
<property name="position">3</property>
</packing>
</child>
</object>

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="new_catalog_window">
<property name="can_focus">False</property>
<property name="modal">True</property>
<property name="window_position">center</property>
<property name="type_hint">dialog</property>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">Новый справочник</property>
<property name="show_close_button">True</property>
<child>
<object class="GtkButton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="save_catalog" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">document-save-symbolic</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="top_padding">5</property>
<property name="bottom_padding">5</property>
<property name="left_padding">12</property>
<property name="right_padding">5</property>
<child>
<object class="GtkEntry" id="catalog_name">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="width_chars">30</property>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Имя нового справочника</property>
</object>
</child>
</object>
</child>
</object>
</interface>

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="open_list_window">
<property name="can_focus">False</property>
<property name="modal">True</property>
<property name="window_position">center</property>
<property name="default_width">800</property>
<property name="default_height">500</property>
<property name="type_hint">dialog</property>
<child type="titlebar">
<object class="GtkHeaderBar" id="list_win_header">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="show_close_button">True</property>
<child>
<object class="GtkButton">
<property name="label">gtk-add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
</object>
</child>
<child>
<object class="GtkButton">
<property name="label">gtk-edit</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label">gtk-remove</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">never</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkListBox" id="listrecord_list">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>