-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4350 from mikhailprivalov/script-upload-chambers
Скрипт - загрузка палат и кроватей
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.core.management.base import BaseCommand | ||
from openpyxl import load_workbook | ||
from podrazdeleniya.models import Podrazdeleniya, Chamber, Bed | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
""" | ||
:param path - файл | ||
""" | ||
parser.add_argument('path', type=str) | ||
|
||
def handle(self, *args, **kwargs): | ||
fp = kwargs["path"] | ||
wb = load_workbook(filename=fp) | ||
ws = wb[wb.sheetnames[0]] | ||
starts = False | ||
department_title_idx, chamber_title_idx, bed_number_idx = '', '', '' | ||
for row in ws.rows: | ||
cells = [str(x.value) for x in row] | ||
if not starts: | ||
if "Название палаты" in cells: | ||
department_title_idx = cells.index("Отделение") | ||
chamber_title_idx = cells.index("Название палаты") | ||
bed_number_idx = cells.index("Номер койки") | ||
starts = True | ||
else: | ||
current_department_title = cells[department_title_idx].strip() | ||
current_chamber_title = cells[chamber_title_idx].strip() | ||
current_bed_number = cells[bed_number_idx].strip() | ||
if not current_department_title or not current_chamber_title or not current_bed_number: | ||
self.stdout.write('Пустая строка') | ||
continue | ||
department = Podrazdeleniya.objects.filter(title__iexact=current_department_title).first() | ||
if not department: | ||
self.stdout.write(f' Подразделения {current_department_title} - нет') | ||
continue | ||
chamber = Chamber.objects.filter(title__iexact=current_chamber_title).first() | ||
if not chamber: | ||
chamber = Chamber(title=current_chamber_title, podrazdelenie_id=department.pk) | ||
chamber.save() | ||
self.stdout.write(f'Палата с названием {current_chamber_title} - добавлена') | ||
bed = Bed.objects.filter(chamber_id=chamber.pk, bed_number=current_bed_number).first() | ||
if not bed: | ||
bed = Bed(chamber_id=chamber.pk, bed_number=current_bed_number) | ||
bed.save() | ||
self.stdout.write(f'Кровать с номером {current_bed_number} - добавлена') |