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()
|
2020-05-03 18:40:31 +03:00
|
|
|
|
####################################################################
|
|
|
|
|
rls_db_file = os.path.join(init_db_dir, 'rls.db')
|
|
|
|
|
rls_db = CSqliteExtDatabase(rls_db_file, pragmas={
|
|
|
|
|
'foreign_keys': 'on',
|
|
|
|
|
'ignore_check_constraints': 'off'})
|
2019-12-17 13:28:52 +03:00
|
|
|
|
|
2020-05-03 18:40:31 +03:00
|
|
|
|
class RlsBaseModel(Model):
|
|
|
|
|
'Базовая таблица'
|
|
|
|
|
class Meta:
|
|
|
|
|
database = rls_db
|
|
|
|
|
|
|
|
|
|
class RlsInitPharmacoTherapeuticGroup(RlsBaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
title = TextField(null=True)
|
|
|
|
|
class RlsInitCountry(RlsBaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
title = TextField(null=True)
|
|
|
|
|
class RlsInit(RlsBaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
regnumber = TextField(null=True)
|
|
|
|
|
regdate = DateField(null=True)
|
|
|
|
|
enddate = DateField(null=True)
|
|
|
|
|
cancellationdate = DateField(null=True)
|
|
|
|
|
nameregcertificate = TextField(null=True)
|
|
|
|
|
country = ForeignKeyField(RlsInitCountry, backref='rlsinit')
|
|
|
|
|
tradename = TextField(null=True)
|
|
|
|
|
internationalname = TextField(null=True)
|
|
|
|
|
formrelease = TextField(null=True)
|
|
|
|
|
stages = TextField(null=True)
|
|
|
|
|
barcodes = TextField(null=True)
|
|
|
|
|
normativedocumentation = TextField(null=True)
|
|
|
|
|
pharmacotherapeuticgroup = ForeignKeyField(RlsInitPharmacoTherapeuticGroup, backref='rlsinit')
|
2019-12-17 13:28:52 +03:00
|
|
|
|
####################################################################
|
|
|
|
|
|
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
|
2020-09-10 17:24:05 +03:00
|
|
|
|
options = {'tokenize': 'porter', 'content': ''}
|
2019-11-19 15:37:53 +03:00
|
|
|
|
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)
|
2020-05-06 19:08:58 +03:00
|
|
|
|
class PatientIndex(BaseFTSModel):
|
|
|
|
|
rowid = RowIDField()
|
|
|
|
|
fio = SearchField()
|
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()
|
2020-05-04 15:48:28 +03:00
|
|
|
|
class RlsPharmacoTherapeuticGroup(BaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
title = TextField(null=True)
|
|
|
|
|
class RlsCountry(BaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
title = TextField(null=True)
|
|
|
|
|
class Rls(BaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
regnumber = TextField(null=True, help_text='Номер регистрационного удостоверения лекарственного средства')
|
|
|
|
|
regdate = DateField(null=True, help_text='Дата регистрации лекарственного средства')
|
|
|
|
|
enddate = DateField(null=True, help_text='Дата окончания действия регистрационного удостоверения')
|
|
|
|
|
cancellationdate = DateField(null=True, help_text='Дата аннулирования регистрационного удостоверения')
|
|
|
|
|
nameregcertificate = TextField(null=True, help_text='Юридическое лицо, на имя которого выдано регистрационное удостоверение')
|
2020-09-10 17:24:05 +03:00
|
|
|
|
country = ForeignKeyField(RlsCountry, backref='rls', help_text='Страна производства')
|
2020-05-04 15:48:28 +03:00
|
|
|
|
tradename = TextField(null=True, help_text='Торговое наименование лекарственного средства')
|
|
|
|
|
internationalname = TextField(null=True, help_text='Международное непатентованное или химическое наименование лекарственного средства')
|
|
|
|
|
formrelease = TextField(null=True, help_text='Формы выпуска')
|
|
|
|
|
stages = TextField(null=True, help_text='Сведения о стадиях производства')
|
|
|
|
|
barcodes = TextField(null=True, help_text='Штрих-коды потребительской упаковки')
|
|
|
|
|
normativedocumentation = TextField(null=True, help_text='Нормативная документация')
|
2020-09-10 17:24:05 +03:00
|
|
|
|
pharmacotherapeuticgroup = ForeignKeyField(RlsPharmacoTherapeuticGroup, backref='rls', help_text='Фармако-терапевтическая группа')
|
2020-05-04 15:48:28 +03:00
|
|
|
|
class RlsIndex(BaseFTSModel):
|
|
|
|
|
rowid = RowIDField()
|
2020-05-04 20:30:32 +03:00
|
|
|
|
tradename = SearchField()
|
|
|
|
|
internationalname = SearchField()
|
2020-09-10 17:24:05 +03:00
|
|
|
|
class MedicineOrder(BaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
order = TextField(unique=True)
|
2020-05-06 19:08:58 +03:00
|
|
|
|
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 ReceptionMedicine(BaseModel):
|
|
|
|
|
id = AutoIncrementField()
|
|
|
|
|
reception = ForeignKeyField(Reception, backref='reception_medicine', on_delete='CASCADE')
|
|
|
|
|
medicine = ForeignKeyField(Rls, backref='reception_medicine', on_delete='CASCADE')
|
2020-09-10 17:24:05 +03:00
|
|
|
|
order = ForeignKeyField(MedicineOrder, backref='reception_medicine', on_delete='CASCADE')
|
2020-05-06 19:08:58 +03:00
|
|
|
|
|
2019-11-19 15:37:53 +03:00
|
|
|
|
### Настройки
|
|
|
|
|
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,
|
2020-05-06 19:08:58 +03:00
|
|
|
|
ReceptionMedicine,
|
2020-05-04 15:48:28 +03:00
|
|
|
|
RlsPharmacoTherapeuticGroup,
|
|
|
|
|
RlsCountry,
|
|
|
|
|
Rls,
|
2020-05-04 20:30:32 +03:00
|
|
|
|
RlsIndex,
|
2020-09-10 17:24:05 +03:00
|
|
|
|
MedicineOrder,
|
2019-11-19 15:37:53 +03:00
|
|
|
|
Settings
|
2019-11-12 19:30:17 +03:00
|
|
|
|
])
|