Skip to content

Commit

Permalink
Merge pull request #3384 from mikhailprivalov/fixImportLabResearch
Browse files Browse the repository at this point in the history
fix - import laboratory service
  • Loading branch information
Wellheor1 authored Jan 15, 2024
2 parents d101033 + 60a6329 commit 2447077
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions directory/management/commands/import_lab_research_from_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def handle(self, *args, **kwargs):
ws = wb[wb.sheetnames[0]]
starts = False
internal_code, title, material, container, department, laboratory_duration = '', '', '', '', '', ''
step = 0
for row in ws.rows:
cells = [str(x.value) for x in row]
if not starts:
Expand All @@ -35,6 +36,8 @@ def handle(self, *args, **kwargs):
continue
material_obj = LaboratoryMaterial.objects.filter(title=cells[material].strip()).first()
department_obj = Podrazdeleniya.objects.filter(title=cells[department].strip()).first()
if cells[laboratory_duration] == "None":
cells[laboratory_duration] = ''
research = Researches(
title=cells[title].strip(),
internal_code=cells[internal_code],
Expand All @@ -46,12 +49,26 @@ def handle(self, *args, **kwargs):

tube = Tubes.objects.filter(title=cells[container].strip()).first()
fraction_data, relation_f = None, None
if ReleationsFT.objects.filter(tube=tube).first():
relation_f = ReleationsFT.objects.filter(tube=tube).first()
fraction_data = Fractions.objects.filter(relation=relation_f).first()
if ReleationsFT.objects.filter(tube=tube).exists():
relation_tmp = None
fraction_data = {}
for i in ReleationsFT.objects.filter(tube=tube):
if i.pk != relation_tmp:
fraction_obj = Fractions.objects.filter(relation_id=i.pk).first()
fraction_data[i.pk] = fraction_obj.research.laboratory_material
relation_tmp = i.pk

if not ReleationsFT.objects.filter(tube=tube).first() or (
fraction_data and (fraction_data.research.podrazdeleniye != department_obj or fraction_data.research.laboratory_material != material_obj)):
if not ReleationsFT.objects.filter(tube=tube).exists():
relation_f = ReleationsFT(tube=tube)
relation_f.save()

if fraction_data:
for k, v in fraction_data.items():
if v == material_obj:
relation_f = ReleationsFT.objects.filter(pk=k).first()
break

if not relation_f:
relation_f = ReleationsFT(tube=tube)
relation_f.save()

Expand All @@ -61,4 +78,5 @@ def handle(self, *args, **kwargs):
relation=relation_f
)
fraction.save()
self.stdout.write(f'Услуга добавлена - {research.title}, фракция - {fraction.title}')
step += 1
self.stdout.write(f'{step}-Услуга добавлена - {research.title}, фракция - {fraction.title}')

0 comments on commit 2447077

Please sign in to comment.