diff --git a/external_system/management/commands/cda_import.py b/external_system/management/commands/cda_import.py new file mode 100644 index 0000000000..848580b7aa --- /dev/null +++ b/external_system/management/commands/cda_import.py @@ -0,0 +1,40 @@ +from django.core.management.base import BaseCommand +from openpyxl import load_workbook + +from external_system.models import CdaFields + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument('path', type=str) + + def handle(self, *args, **kwargs): + """ + :param path - xlsx файл с CDA-titles столбцами: + NAME + """ + fp = kwargs["path"] + wb = load_workbook(filename=fp) + ws = wb[wb.sheetnames[0]] + starts = False + + title, doc_refferal, extract = '', '', '' + for row in ws.rows: + is_doc_refferal = False + is_extract = False + cells = [str(x.value) for x in row] + if not starts: + if "NAME" in cells: + title = cells.index("NAME") + doc_refferal = cells.index("doc_refferal") + extract = cells.index("extract") + starts = True + else: + cda = CdaFields.objects.filter(title=cells[title]) + if not cda.exists(): + if doc_refferal == 1: + is_doc_refferal = True + if extract == 1: + is_extract = True + CdaFields(title=cells[title], is_doc_refferal=is_doc_refferal, is_extract=is_extract).save() + self.stdout.write('сохранено', cells[title])