-
Notifications
You must be signed in to change notification settings - Fork 24
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 #1096 from xzzy/master2
Create update_identification_document_fields.py
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
ledgergw/management/commands/update_identification_document_fields.py
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,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)) |