Работа со списками

This commit is contained in:
Бородин Роман 2019-12-01 16:22:43 +03:00
parent 3731153226
commit efdf7ebf13
10 changed files with 308 additions and 73 deletions

37
app.py
View File

@ -5,8 +5,9 @@ from gi.repository import Gtk, GObject, Gdk
from mods.db import store_patient_index, update_patient_index, search_patients, db, Patient, Reception, Catalog, List 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.settings import s_get_reception_list, s_set_reception_list
from mods.catalogs import add_catalog, search_catalogs from mods.catalogs import add_catalog, search_catalogs
from mods.lists import add_list_record, create_open_list_win from mods.lists import create_open_list_win
from mods.files import list_row_ui_str from mods.files import list_row_ui_str
from mods.utils import show_msg, ConditionalFilter
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
import peewee import peewee
import re import re
@ -55,18 +56,6 @@ def get_reception_timelist(rec_date):
shift_minutes_range = range(0, work_day_minutes_total, s.interval) shift_minutes_range = range(0, work_day_minutes_total, s.interval)
return [dstart + timedelta(minutes=x) for x in shift_minutes_range] return [dstart + timedelta(minutes=x) for x in shift_minutes_range]
class ConditionalFilter:
def __init__(self, search_func):
self.search_func = search_func
self.reset()
def filter(self, query):
if query != self.fstr:
self.fstr = query
self.ids = list(map(lambda x: x.id, self.search_func(query)))
return self.ids
def reset(self):
self.fstr = ''
self.ids = []
patient_filter = ConditionalFilter(search_patients) patient_filter = ConditionalFilter(search_patients)
catalog_filter = ConditionalFilter(search_catalogs) catalog_filter = ConditionalFilter(search_catalogs)
@ -181,18 +170,6 @@ class MainWinHandler:
pl.unselect_all() pl.unselect_all()
self.patient_list_unselected() self.patient_list_unselected()
pl.invalidate_filter() pl.invalidate_filter()
'''pl = builder.get_object('patient_list')
for r in pl.get_children():
pl.remove(r)
fstr = builder.get_object('patient_filter').get_text().strip()
if not fstr:
for p in Patient.select():
pl.add(build_patient_row(p))
else:
patients = search_patients(fstr)
for p in patients:
pl.add(build_patient_row(p))
pl.show_all()'''
def list_list_selected(self, *a): def list_list_selected(self, *a):
open_button = builder.get_object('list_open_button') open_button = builder.get_object('list_open_button')
open_button.set_sensitive(True) open_button.set_sensitive(True)
@ -341,15 +318,7 @@ with db.atomic():
for c in Catalog.select(): for c in Catalog.select():
catalog_list.add(build_catalog_row(c)) catalog_list.add(build_catalog_row(c))
def show_msg(text, sec_text='', level='info'):
msg_win_file = os.path.join(ui_dir, f'{level}_win.glade')
b = Gtk.Builder()
b.add_from_file(msg_win_file)
w = b.get_object(level)
w.set_property('text',text)
w.set_property('secondary_text',sec_text)
w.run()
w.close()
def get_patient_win_values(b): def get_patient_win_values(b):
l_name = b.get_object('last_name').get_text().replace(' ', '') l_name = b.get_object('last_name').get_text().replace(' ', '')

View File

@ -1,6 +1,5 @@
from mods.db import db, Catalog, CatalogIndex from mods.db import db, Catalog, CatalogIndex
import re
def add_catalog(name): def add_catalog(name):
with db.atomic(): with db.atomic():
@ -14,6 +13,8 @@ def add_catalog(name):
return catalog return catalog
def search_catalogs(q): def search_catalogs(q):
q = re.sub(r'\s+', ' ', q).strip()
q = ' '.join([ f'{x}*' for x in q.split(' ')])
with db.atomic(): with db.atomic():
return (Catalog.select() return (Catalog.select()
.join( .join(

View File

@ -1,6 +1,7 @@
import os import os
from peewee import Model, CharField, DateTimeField, TextField, ForeignKeyField, DateField, fn from peewee import Model, CharField, DateTimeField, TextField, ForeignKeyField, DateField, fn
from playhouse.sqlite_ext import CSqliteExtDatabase, FTS5Model, AutoIncrementField, SearchField, RowIDField, JSONField from playhouse.sqlite_ext import CSqliteExtDatabase, FTS5Model, AutoIncrementField, SearchField, RowIDField, JSONField
import re
var_dir = os.path.join(os.path.abspath(os.environ['HOME']), '.eldoc') var_dir = os.path.join(os.path.abspath(os.environ['HOME']), '.eldoc')
db_dir = os.path.join(var_dir, 'db') db_dir = os.path.join(var_dir, 'db')
@ -87,7 +88,7 @@ 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') 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')) 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)) ListRecord.add_index(ListRecord.index(ListRecord.list, fn.lower(ListRecord.text), unique=True, name='listrec_unique'))
db.create_tables([ db.create_tables([
Patient, Patient,
PatientIndex, PatientIndex,
@ -105,6 +106,8 @@ db.create_tables([
]) ])
def search_patients(q): def search_patients(q):
q = re.sub(r'\s+', ' ', q).strip()
q = ' '.join([ f'{x}*' for x in q.split(' ')])
with db.atomic(): with db.atomic():
return (Patient.select() return (Patient.select()
.join( .join(

View File

@ -9,3 +9,6 @@ open_list_win_file = os.path.join(ui_dir, 'open_list_win.glade')
listrecord_row_file = os.path.join(ui_dir, 'listrecord_row.glade') listrecord_row_file = os.path.join(ui_dir, 'listrecord_row.glade')
with open(listrecord_row_file, 'r') as f: with open(listrecord_row_file, 'r') as f:
listrecord_row_ui_str = f.read() listrecord_row_ui_str = f.read()
editable_row_file = os.path.join(ui_dir, 'editable_row.glade')
with open(editable_row_file, 'r') as f:
editable_row_ui_str = f.read()

View File

@ -1,8 +1,11 @@
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject from gi.repository import Gtk, GObject, Gdk
from mods.db import db, List, ListRecord, ListRecordIndex from mods.db import db, List, ListRecord, ListRecordIndex
from mods.files import open_list_win_file, listrecord_row_ui_str from mods.files import open_list_win_file, listrecord_row_ui_str, editable_row_ui_str
from mods.utils import show_msg, disable_widget, enable_widget, ConditionalFilter
import re
import peewee
lists_map = { lists_map = {
'diagnoz': 'Диагноз', 'diagnoz': 'Диагноз',
@ -17,6 +20,13 @@ for s_id in lists_map:
if not len(q): if not len(q):
List.create(name=lists_map[s_id], system_id=s_id) List.create(name=lists_map[s_id], system_id=s_id)
class ListRecFilter(ConditionalFilter):
def filter(self, list_id, query):
if query != self.fstr:
self.fstr = query
self.ids = list(map(lambda x: x.id, self.search_func(list_id, query)))
return self.ids
class ListRecordRow(Gtk.ListBoxRow): class ListRecordRow(Gtk.ListBoxRow):
@GObject.Property @GObject.Property
def db_id(self): def db_id(self):
@ -31,22 +41,12 @@ class ListRecordRow(Gtk.ListBoxRow):
def text_setter(self, value): def text_setter(self, value):
self._text = 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): def get_list(list_id):
with db.atomic(): with db.atomic():
return List.get_by_id(list_id) return List.get_by_id(list_id)
def get_listrecord(listrec_id):
with db.atomic():
return ListRecord.get_by_id(listrec_id)
def get_all_list_records(list_id): def get_all_list_records(list_id):
with db.atomic(): with db.atomic():
@ -68,14 +68,17 @@ def delete_list_record(listrec_id):
ListRecord.delete().where(ListRecord.id == listrec_id).execute() ListRecord.delete().where(ListRecord.id == listrec_id).execute()
ListRecordIndex.delete().where(ListRecordIndex.rowid == listrec_id).execute() ListRecordIndex.delete().where(ListRecordIndex.rowid == listrec_id).execute()
def change_list_record(listrec_id, new_text): def change_list_record(listrec_id, new_text):
listrec_o = ListRecord.get_by_id(listrec_id) listrec_o = get_listrecord(listrec_id)
if listrec_o.text == new_text: if listrec_o.text == new_text:
return False return listrec_o
with db.atomic(): with db.atomic():
ListRecord.update(text=new_text).execute() ListRecord.update(text=new_text).where(ListRecord.id == listrec_id).execute()
ListRecordIndex.update(text=new_text).execute() ListRecordIndex.update(text=new_text).where(ListRecordIndex.rowid == listrec_id).execute()
return ListRecord.get_by_id(listrec_id)
def search_list_record(list_id, q): def search_list_record(list_id, q):
list_o = List.get_by_id(list_id) q = re.sub(r'\s+', ' ', q).strip()
q = ' '.join([ f'{x}*' for x in q.split(' ')])
list_o = get_list(list_id)
with db.atomic(): with db.atomic():
return (ListRecord.select() return (ListRecord.select()
.join( .join(
@ -83,19 +86,146 @@ def search_list_record(list_id, q):
on=(ListRecord.id == ListRecordIndex.rowid)) on=(ListRecord.id == ListRecordIndex.rowid))
.where((ListRecord.list == list_o) & (ListRecordIndex.match(q))) .where((ListRecord.list == list_o) & (ListRecordIndex.match(q)))
.order_by(ListRecordIndex.bm25())) .order_by(ListRecordIndex.bm25()))
#####################################################################################################################
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 build_listrec_editable_row(listrec_o=None):
b = Gtk.Builder()
b.add_from_string(editable_row_ui_str)
#b.connect_signals({'editing_done': listrec_editing_done})
win = b.get_object('win')
box = b.get_object('editable_box')
text_input = b.get_object('text_input')
text_input.set_text(listrec_o.text if listrec_o else '')
row = ListRecordRow()
row.props.db_id = listrec_o.id if listrec_o else -1
row.text = listrec_o.text if listrec_o else ''
win.remove(win.get_children()[0])
row.add(box)
return (row, text_input)
def create_open_list_win(list_id): def create_open_list_win(list_id):
list_o = get_list(list_id) list_o = get_list(list_id)
b = Gtk.Builder() b = Gtk.Builder()
listrecord_filter = ListRecFilter(search_list_record)
class OpenListHandler: class OpenListHandler:
pass def row_add_cancel(self, entry, ev, row):
if ev.keyval == Gdk.KEY_Escape:
lr_list.remove(row)
row.destroy()
def row_edit_cancel(self, entry, ev, edit_row):
if ev.keyval == Gdk.KEY_Escape:
rec = get_listrecord(edit_row.props.db_id)
row = build_listrecord_row(rec)
lr_list.remove(edit_row)
edit_row.destroy()
lr_list.add(row)
lr_list.show_all()
def enable_buttons_on_add_edit(self, *a):
enable_widget([add_button, edit_button, remove_button, lr_filter])
def remove_row(self, button):
row = lr_list.get_selected_row()
delete_list_record(row.props.db_id)
lr_list.remove(row)
row.destroy()
lr_list.show_all()
self.listrec_row_unselected()
def edit_row(self, button):
row = lr_list.get_selected_row()
rec = get_listrecord(row.props.db_id)
(edit_row, inp) = build_listrec_editable_row(rec)
lr_list.remove(row)
row.destroy()
lr_list.add(edit_row)
inp.grab_focus()
inp.connect('key-release-event', self.row_edit_cancel, edit_row)
inp.connect('activate', self.update_row, edit_row)
inp.connect('destroy', self.enable_buttons_on_add_edit)
disable_widget([add_button, edit_button, remove_button, lr_filter])
lr_list.show_all()
def add_new_row(self, button):
(row, text_input) = build_listrec_editable_row()
lr_list.add(row)
text_input.grab_focus()
text_input.connect('key-release-event', self.row_add_cancel, row)
text_input.connect('activate', self.save_new_row, row)
text_input.connect('destroy', self.enable_buttons_on_add_edit)
disable_widget([add_button, edit_button, remove_button, lr_filter])
lr_list.show_all()
def update_row(self, inp, edit_row):
new_text = re.sub(r'\s+', ' ', inp.get_text()).strip()
if new_text:
try:
rec = change_list_record(edit_row.props.db_id, new_text)
except peewee.IntegrityError:
return show_msg('Такая же запись уже существует', 'В данном списке уже существует\nдругая запись с данным текстом', level='warn')
row = build_listrecord_row(rec)
else:
row = build_listrecord_row(get_listrecord(edit_row.props.db_id))
lr_list.add(row)
lr_list.select_row(row)
lr_list.show_all()
lr_list.remove(edit_row)
edit_row.destroy()
def save_new_row(self, inp, row):
row_text = re.sub(r'\s+', ' ', inp.get_text()).strip()
if row_text:
try:
rec = add_list_record(list_id, row_text)
except peewee.IntegrityError:
return show_msg('Данная запись уже существует', 'В данном списке уже существует\nзапись с данным текстом', level='warn')
new_row = build_listrecord_row(rec)
lr_list.add(new_row)
lr_list.select_row(new_row)
lr_list.show_all()
lr_list.remove(row)
row.destroy()
def listrec_row_selected(self, *a):
enable_widget([edit_button, remove_button])
def listrec_row_unselected(self, *a):
disable_widget([edit_button, remove_button])
def listrec_filter_changed(self, filter_widget):
lr_list.unselect_all()
self.listrec_row_unselected()
lr_list.invalidate_filter()
b.add_from_file(open_list_win_file) b.add_from_file(open_list_win_file)
b.connect_signals(OpenListHandler()) b.connect_signals(OpenListHandler())
# Gtk objects
w = b.get_object('open_list_window') w = b.get_object('open_list_window')
listrec_list = b.get_object('listrecord_list') lr_list = b.get_object('listrecord_list')
lr_filter = b.get_object('listrecord_filter')
add_button = b.get_object('add_button')
edit_button = b.get_object('edit_button')
remove_button = b.get_object('remove_button')
list_header = b.get_object('list_win_header') list_header = b.get_object('list_win_header')
#############
######
def listrecord_sort_func(row1, row2, *a):
text1 = row1.props.text
text2 = row2.props.text
return (text1 > text2) - (text1 < text2)
def listrecord_filter_func(row):
fstr = lr_filter.get_text().strip()
if not fstr:
listrecord_filter.reset()
return True
return row.props.db_id in listrecord_filter.filter(list_id, fstr)
######
lr_list.set_sort_func(listrecord_sort_func)
lr_list.set_filter_func(listrecord_filter_func)
list_header.props.title = list_o.name list_header.props.title = list_o.name
for lr in get_all_list_records(list_id): for lr in get_all_list_records(list_id):
listrec_list.add(build_listrecord_row(lr)) lr_list.add(build_listrecord_row(lr))
return w return w

34
mods/utils.py Normal file
View File

@ -0,0 +1,34 @@
import os
from mods.files import ui_dir
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
def show_msg(text, sec_text='', level='info'):
msg_win_file = os.path.join(ui_dir, f'{level}_win.glade')
b = Gtk.Builder()
b.add_from_file(msg_win_file)
w = b.get_object(level)
w.set_property('text',text)
w.set_property('secondary_text',sec_text)
w.run()
w.close()
def enable_widget(w_list):
for widget in w_list:
widget.set_sensitive(True)
def disable_widget(w_list):
for widget in w_list:
widget.set_sensitive(False)
class ConditionalFilter:
def __init__(self, search_func):
self.search_func = search_func
self.reset()
def filter(self, query):
if query != self.fstr:
self.fstr = query
self.ids = list(map(lambda x: x.id, self.search_func(query)))
return self.ids
def reset(self):
self.fstr = ''
self.ids = []

29
res/ui/editable_row.glade Normal file
View File

@ -0,0 +1,29 @@
<?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>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox" id="editable_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkEntry" id="text_input">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

@ -19,7 +19,7 @@
<property name="label" translatable="yes">label</property> <property name="label" translatable="yes">label</property>
<property name="xalign">0</property> <property name="xalign">0</property>
<attributes> <attributes>
<attribute name="scale" value="2"/> <attribute name="font-desc" value="Comic Sans MS 14"/>
</attributes> </attributes>
</object> </object>
<packing> <packing>

View File

@ -9,11 +9,11 @@
<placeholder/> <placeholder/>
</child> </child>
<child> <child>
<object class="GtkBox" id="list_box"> <object class="GtkBox" id="listrecord_box">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child> <child>
<object class="GtkLabel" id="name"> <object class="GtkLabel" id="text">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="label" translatable="yes">label</property> <property name="label" translatable="yes">label</property>

View File

@ -15,17 +15,18 @@
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="show_close_button">True</property> <property name="show_close_button">True</property>
<child> <child>
<object class="GtkButton"> <object class="GtkButton" id="add_button">
<property name="label">gtk-add</property> <property name="label">gtk-add</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
<property name="use_stock">True</property> <property name="use_stock">True</property>
<property name="always_show_image">True</property> <property name="always_show_image">True</property>
<signal name="clicked" handler="add_new_row" swapped="no"/>
</object> </object>
</child> </child>
<child> <child>
<object class="GtkButton"> <object class="GtkButton" id="edit_button">
<property name="label">gtk-edit</property> <property name="label">gtk-edit</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="sensitive">False</property> <property name="sensitive">False</property>
@ -33,13 +34,14 @@
<property name="receives_default">True</property> <property name="receives_default">True</property>
<property name="use_stock">True</property> <property name="use_stock">True</property>
<property name="always_show_image">True</property> <property name="always_show_image">True</property>
<signal name="clicked" handler="edit_row" swapped="no"/>
</object> </object>
<packing> <packing>
<property name="position">1</property> <property name="position">1</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton"> <object class="GtkButton" id="remove_button">
<property name="label">gtk-remove</property> <property name="label">gtk-remove</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="sensitive">False</property> <property name="sensitive">False</property>
@ -47,6 +49,7 @@
<property name="receives_default">True</property> <property name="receives_default">True</property>
<property name="use_stock">True</property> <property name="use_stock">True</property>
<property name="always_show_image">True</property> <property name="always_show_image">True</property>
<signal name="clicked" handler="remove_row" swapped="no"/>
</object> </object>
<packing> <packing>
<property name="position">2</property> <property name="position">2</property>
@ -55,22 +58,85 @@
</object> </object>
</child> </child>
<child> <child>
<object class="GtkScrolledWindow"> <object class="GtkBox">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">False</property>
<property name="hscrollbar_policy">never</property> <property name="orientation">vertical</property>
<property name="shadow_type">in</property>
<child> <child>
<object class="GtkViewport"> <object class="GtkScrolledWindow">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">True</property>
<property name="hscrollbar_policy">never</property>
<property name="shadow_type">in</property>
<child> <child>
<object class="GtkListBox" id="listrecord_list"> <object class="GtkViewport">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</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>
<signal name="row-selected" handler="listrec_row_selected" swapped="no"/>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">3</property>
<property name="margin_bottom">3</property>
<child>
<object class="GtkFixed">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSearchEntry" id="listrecord_filter">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="primary_icon_name">edit-find-symbolic</property>
<property name="primary_icon_activatable">False</property>
<property name="primary_icon_sensitive">False</property>
<signal name="changed" handler="listrec_filter_changed" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkFixed">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child> </child>
</object> </object>
</child> </child>