From 50e72fdb4e8c2c81101b9362c34b78d8b2b91781 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 7 Dec 2018 17:35:04 -0400 Subject: [PATCH] Style: Move document model functions Move the document UUID and document hash functions to the documents.utils module. Signed-off-by: Roberto Rosario --- .../migrations/0003_auto_20150608_1915.py | 4 ++-- .../migrations/0042_auto_20180403_0702.py | 2 +- .../migrations/0043_auto_20180429_0759.py | 2 +- mayan/apps/documents/models.py | 15 +++------------ mayan/apps/documents/utils.py | 12 ++++++++++++ 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/mayan/apps/documents/migrations/0003_auto_20150608_1915.py b/mayan/apps/documents/migrations/0003_auto_20150608_1915.py index 8628826aad..722f193a2b 100644 --- a/mayan/apps/documents/migrations/0003_auto_20150608_1915.py +++ b/mayan/apps/documents/migrations/0003_auto_20150608_1915.py @@ -17,7 +17,7 @@ class Migration(migrations.Migration): model_name='document', name='uuid', field=models.CharField( - default=mayan.apps.documents.models.UUID_FUNCTION, + default=mayan.apps.documents.utils.document_uuid_function, editable=False, max_length=48, ), preserve_default=True, @@ -27,7 +27,7 @@ class Migration(migrations.Migration): name='file', field=models.FileField( storage=FileSystemStorage(), - upload_to=mayan.apps.documents.models.UUID_FUNCTION, + upload_to=mayan.apps.documents.utils.document_uuid_function, verbose_name='File' ), preserve_default=True, diff --git a/mayan/apps/documents/migrations/0042_auto_20180403_0702.py b/mayan/apps/documents/migrations/0042_auto_20180403_0702.py index 6c9c12f977..60ccd85272 100644 --- a/mayan/apps/documents/migrations/0042_auto_20180403_0702.py +++ b/mayan/apps/documents/migrations/0042_auto_20180403_0702.py @@ -17,7 +17,7 @@ class Migration(migrations.Migration): field=models.FileField( storage=django.core.files.storage.FileSystemStorage( location=b'mayan/media/document_storage' - ), upload_to=mayan.apps.documents.models.UUID_FUNCTION, + ), upload_to=mayan.apps.documents.utils.document_uuid_function, verbose_name='File' ), model_name='documentversion', name='file', diff --git a/mayan/apps/documents/migrations/0043_auto_20180429_0759.py b/mayan/apps/documents/migrations/0043_auto_20180429_0759.py index eea1269ffe..8a2ac210fd 100644 --- a/mayan/apps/documents/migrations/0043_auto_20180429_0759.py +++ b/mayan/apps/documents/migrations/0043_auto_20180429_0759.py @@ -24,7 +24,7 @@ class Migration(migrations.Migration): field=models.FileField( storage=django.core.files.storage.FileSystemStorage( location=b'/home/rosarior/development/mayan-edms/mayan/media/document_storage' - ), upload_to=mayan.apps.documents.models.UUID_FUNCTION, + ), upload_to=mayan.apps.documents.utils.document_uuid_function, verbose_name='File' ), model_name='documentversion', name='file', diff --git a/mayan/apps/documents/models.py b/mayan/apps/documents/models.py index 5e5ecfe2bc..a01caf2bfb 100644 --- a/mayan/apps/documents/models.py +++ b/mayan/apps/documents/models.py @@ -1,6 +1,5 @@ from __future__ import absolute_import, unicode_literals -import hashlib import logging import os import uuid @@ -55,19 +54,11 @@ from .signals import ( post_document_created, post_document_type_change, post_version_upload ) from .storages import storage_documentversion +from .utils import document_hash_function, document_uuid_function logger = logging.getLogger(__name__) -# document image cache name hash function -def HASH_FUNCTION(data): - return hashlib.sha256(data).hexdigest() - - -def UUID_FUNCTION(*args, **kwargs): - return force_text(uuid.uuid4()) - - @python_2_unicode_compatible class DocumentType(models.Model): """ @@ -433,7 +424,7 @@ class DocumentVersion(models.Model): # File related fields file = models.FileField( - storage=storage_documentversion, upload_to=UUID_FUNCTION, + storage=storage_documentversion, upload_to=document_uuid_function, verbose_name=_('File') ) mimetype = models.CharField( @@ -701,7 +692,7 @@ class DocumentVersion(models.Model): """ if self.exists(): source = self.open() - self.checksum = force_text(HASH_FUNCTION(source.read())) + self.checksum = force_text(document_hash_function(source.read())) source.close() if save: self.save() diff --git a/mayan/apps/documents/utils.py b/mayan/apps/documents/utils.py index 16f6e25d7b..e4d3e8e285 100644 --- a/mayan/apps/documents/utils.py +++ b/mayan/apps/documents/utils.py @@ -1,6 +1,10 @@ from __future__ import unicode_literals +import hashlib +import uuid + from django.apps import apps +from django.utils.encoding import force_text from .literals import DOCUMENT_IMAGES_CACHE_NAME @@ -12,6 +16,14 @@ def callback_update_cache_size(setting): cache.save() +def document_hash_function(data): + return hashlib.sha256(data).hexdigest() + + +def document_uuid_function(*args, **kwargs): + return force_text(uuid.uuid4()) + + def parse_range(astr): # http://stackoverflow.com/questions/4248399/ # page-range-for-printing-algorithm