222 lines
9.5 KiB
Python
222 lines
9.5 KiB
Python
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()
|
||
####################################################################
|
||
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'})
|
||
|
||
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')
|
||
####################################################################
|
||
|
||
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 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='Юридическое лицо, на имя которого выдано регистрационное удостоверение')
|
||
country = ForeignKeyField(RlsCountry, backref='rlsinit', help_text='Страна производства')
|
||
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='Нормативная документация')
|
||
pharmacotherapeuticgroup = ForeignKeyField(RlsPharmacoTherapeuticGroup, backref='rlsinit', help_text='Фармако-терапевтическая группа')
|
||
class RlsIndex(BaseFTSModel):
|
||
rowid = RowIDField()
|
||
tradename = SearchField()
|
||
internationalname = 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,
|
||
RlsPharmacoTherapeuticGroup,
|
||
RlsCountry,
|
||
Rls,
|
||
RlsIndex,
|
||
Settings
|
||
])
|