import os from peewee import Model, CharField, DateTimeField, TextField, ForeignKeyField, DateField, fn, BooleanField from playhouse.sqlite_ext import CSqliteExtDatabase, FTS5Model, AutoIncrementField, SearchField, RowIDField, JSONField import platform from mods.files import init_db_dir if 'windows' in platform.system().lower(): user_dir = os.path.abspath(os.environ['APPDATA']) else: user_dir = os.path.abspath(os.environ['HOME']) var_dir = os.path.join(user_dir, '.eldoc') db_dir = os.path.join(var_dir, 'db') db_file = os.path.join(db_dir, 'eldoc.sqlite3') if not os.path.exists(var_dir): os.mkdir(var_dir) if not os.path.exists(db_dir): os.mkdir(db_dir) ################################################################### diag_db_file = os.path.join(init_db_dir, 'diag.db') diag_db = CSqliteExtDatabase(diag_db_file, pragmas={ 'foreign_keys': 'on', 'ignore_check_constraints': 'off'}) class DiagBaseModel(Model): 'Базовая таблица' class Meta: database = diag_db class DiagCategory(DiagBaseModel): id = AutoIncrementField() parent = ForeignKeyField('self', null=True, backref='subcat') title = TextField() class Diag(DiagBaseModel): id = AutoIncrementField() category = ForeignKeyField(DiagCategory, backref='diagnosis') code = CharField(32) title = TextField() #################################################################### db = CSqliteExtDatabase(db_file, pragmas={ 'journal_mode': 'wal', 'cache_size': -1 * 64000, # 64MB 'foreign_keys': 'on', 'ignore_check_constraints': 'off', 'synchronous': 'off'}) class BaseModel(Model): 'Базовая таблица' class Meta: database = db class BaseFTSModel(FTS5Model): class Meta: database = db options = {'tokenize': 'porter'} class Patient(BaseModel): id = AutoIncrementField() last_name = TextField() first_name = TextField() middle_name = TextField(null=True) birth_date = DateField() gender = CharField(6) doc_type = CharField(32, null=True) doc_serial = TextField(null=True) doc_number = TextField(null=True) policy_number = TextField(null=True) 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() name = TextField() class CatalogIndex(BaseFTSModel): rowid = RowIDField() text = SearchField() class CatalogRecord(BaseModel): id = AutoIncrementField() catalog = ForeignKeyField(Catalog, backref='catalog_records', on_delete='CASCADE') text = TextField() class CatalogRecordIndex(BaseFTSModel): rowid = RowIDField() text = SearchField() class Reception(BaseModel): id = AutoIncrementField() patient = ForeignKeyField(Patient, backref='receptions', on_delete='CASCADE') time = DateTimeField() attended = BooleanField(default=False) class DiagnosisCategory(BaseModel): id = AutoIncrementField() parent = ForeignKeyField('self', null=True, backref='subcat') title = TextField() class Diagnosis(BaseModel): id = AutoIncrementField() category = ForeignKeyField(DiagnosisCategory, backref='diagnosis') code = CharField(32) title = TextField() class DiagnosisIndex(BaseFTSModel): rowid = RowIDField() code = SearchField() title = SearchField() class ReceptionDiagnosis(BaseModel): id = AutoIncrementField() reception = ForeignKeyField(Reception, backref='reception_diagnosisses', on_delete='CASCADE') diagnosis = ForeignKeyField(Diagnosis, backref='reception_diagnosisses', on_delete='CASCADE') class ReceptionAnamnesis(BaseModel): id = AutoIncrementField() reception = ForeignKeyField(Reception, backref='reception_anamnesisses', on_delete='CASCADE') anamnesis = ForeignKeyField(ListRecord, backref='reception_anamnesisses', on_delete='CASCADE') class ReceptionObserve(BaseModel): id = AutoIncrementField() reception = ForeignKeyField(Reception, backref='reception_symptoms', on_delete='CASCADE') symptom = ForeignKeyField(ListRecord, backref='reception_symptoms', on_delete='CASCADE') class PatientIndex(BaseFTSModel): rowid = RowIDField() fio = SearchField() ### Настройки class Settings(BaseModel): key = TextField() val = JSONField() ### 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, name='listrec_unique')) ReceptionDiagnosis.add_index(ReceptionDiagnosis.index(ReceptionDiagnosis.reception, ReceptionDiagnosis.diagnosis, unique=True, name='reception_diagnosis_unique')) ReceptionAnamnesis.add_index(ReceptionAnamnesis.index(ReceptionAnamnesis.reception, ReceptionAnamnesis.anamnesis, unique=True, name='reception_anamnesis_unique')) ReceptionObserve.add_index(ReceptionObserve.index(ReceptionObserve.reception, ReceptionObserve.symptom, unique=True, name='reception_observe_unique')) db.connect() db.create_tables([ Patient, PatientIndex, List, ListRecord, ListRecordIndex, Catalog, CatalogIndex, CatalogRecord, CatalogRecordIndex, DiagnosisCategory, Diagnosis, DiagnosisIndex, Reception, ReceptionDiagnosis, ReceptionAnamnesis, ReceptionObserve, Settings ])