From 992be2c7f2e525fa681d31ec86eb849b7ff89b66 Mon Sep 17 00:00:00 2001 From: Jason Moore Date: Mon, 12 Aug 2024 10:41:05 +0800 Subject: [PATCH] Create update_identification_document_fields.py --- .../update_identification_document_fields.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ledgergw/management/commands/update_identification_document_fields.py diff --git a/ledgergw/management/commands/update_identification_document_fields.py b/ledgergw/management/commands/update_identification_document_fields.py new file mode 100644 index 000000000..d62ffcca6 --- /dev/null +++ b/ledgergw/management/commands/update_identification_document_fields.py @@ -0,0 +1,36 @@ +from django.core.management.base import BaseCommand +from django.db.models import Q + +import logging +import pathlib + +from ledger.accounts.models import PrivateDocument + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + help = 'Updates all Identification document records to have all required fields populated.' + + def handle(self, *args, **options): + try: + logger.info('Running command {}'.format(__name__)) + + private_docs = PrivateDocument.objects.filter(Q(name=None)|Q(name="")|Q(extension=None)|Q(file_group=None)) + + for i in private_docs: + i.name = pathlib.Path(i.upload.name).name + i.extension = pathlib.Path(i.upload.name + ).suffix if (len( + pathlib.Path( + i.upload.name + ).suffix) <= PrivateDocument._meta.get_field('extension').max_length + ) else "" + i.file_group = 1 + i.save(update_fields=["name","extension","file_group"]) + + logger.info('Command {} finished'.format(__name__)) + + except Exception as e: + logger.error('Error command {0} : {1}'.format( + __name__, e))