2019-10-30 20:17:14 +03:00
|
|
|
import os
|
2019-12-13 21:07:17 +03:00
|
|
|
from peewee import Model, CharField, DateTimeField, TextField, ForeignKeyField, DateField, fn, BooleanField
|
2019-11-12 19:30:17 +03:00
|
|
|
from playhouse.sqlite_ext import CSqliteExtDatabase, FTS5Model, AutoIncrementField, SearchField, RowIDField, JSONField
|
2019-12-04 17:29:35 +03:00
|
|
|
import platform
|
2019-12-17 13:28:52 +03:00
|
|
|
from mods.files import init_db_dir
|
2019-10-30 20:17:14 +03:00
|
|
|
|
2019-12-04 17:29:35 +03:00
|
|
|
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')
|
2019-10-30 20:17:14 +03:00
|
|
|
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)
|
|
|
|
|
2019-12-17 13:28:52 +03:00
|
|
|
###################################################################
|
|
|
|
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()
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
|
2019-11-19 15:37:53 +03:00
|
|
|
db = CSqliteExtDatabase(db_file, pragmas={
|
2019-11-12 19:30:17 +03:00
|
|
|
'journal_mode': 'wal',
|
|
|
|
'cache_size': -1 * 64000, # 64MB
|
|
|
|
'foreign_keys': 'on',
|
|
|
|
'ignore_check_constraints': 'off',
|
|
|
|
'synchronous': 'off'})
|
2019-11-19 15:37:53 +03:00
|
|
|
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)
|
2019-11-23 23:36:26 +03:00
|
|
|
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()
|
2019-11-19 15:37:53 +03:00
|
|
|
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()
|
2019-12-13 21:07:17 +03:00
|
|
|
attended = BooleanField(default=False)
|
2019-12-17 13:28:52 +03:00
|
|
|
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()
|
2019-12-24 13:19:35 +03:00
|
|
|
code = SearchField()
|
2019-12-17 13:28:52 +03:00
|
|
|
title = SearchField()
|
2019-11-19 15:37:53 +03:00
|
|
|
class ReceptionDiagnosis(BaseModel):
|
|
|
|
id = AutoIncrementField()
|
|
|
|
reception = ForeignKeyField(Reception, backref='reception_diagnosisses', on_delete='CASCADE')
|
2019-12-24 13:19:35 +03:00
|
|
|
diagnosis = ForeignKeyField(Diagnosis, backref='reception_diagnosisses', on_delete='CASCADE')
|
2019-11-19 15:37:53 +03:00
|
|
|
class ReceptionAnamnesis(BaseModel):
|
|
|
|
id = AutoIncrementField()
|
|
|
|
reception = ForeignKeyField(Reception, backref='reception_anamnesisses', on_delete='CASCADE')
|
2019-12-26 15:20:50 +03:00
|
|
|
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')
|
2019-11-19 15:37:53 +03:00
|
|
|
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')
|
2019-11-12 19:30:17 +03:00
|
|
|
)
|
2019-11-19 15:37:53 +03:00
|
|
|
Catalog.add_index(Catalog.index(fn.lower(Catalog.name), unique=True, name='catalog_name_unique'))
|
2019-12-01 16:22:43 +03:00
|
|
|
ListRecord.add_index(ListRecord.index(ListRecord.list, fn.lower(ListRecord.text), unique=True, name='listrec_unique'))
|
2019-12-26 15:20:50 +03:00
|
|
|
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'))
|
2019-12-13 21:07:17 +03:00
|
|
|
db.connect()
|
2019-11-19 15:37:53 +03:00
|
|
|
db.create_tables([
|
|
|
|
Patient,
|
|
|
|
PatientIndex,
|
2019-11-23 23:36:26 +03:00
|
|
|
List,
|
|
|
|
ListRecord,
|
|
|
|
ListRecordIndex,
|
2019-11-19 15:37:53 +03:00
|
|
|
Catalog,
|
|
|
|
CatalogIndex,
|
|
|
|
CatalogRecord,
|
|
|
|
CatalogRecordIndex,
|
2019-12-17 13:28:52 +03:00
|
|
|
DiagnosisCategory,
|
|
|
|
Diagnosis,
|
|
|
|
DiagnosisIndex,
|
2019-11-19 15:37:53 +03:00
|
|
|
Reception,
|
|
|
|
ReceptionDiagnosis,
|
|
|
|
ReceptionAnamnesis,
|
2019-12-26 15:20:50 +03:00
|
|
|
ReceptionObserve,
|
2019-11-19 15:37:53 +03:00
|
|
|
Settings
|
2019-11-12 19:30:17 +03:00
|
|
|
])
|