Initial commit to support filename generators
Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
@@ -11,6 +11,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from mayan.apps.acls.models import AccessControlList
|
||||
from mayan.apps.common.literals import TIME_DELTA_UNIT_CHOICES
|
||||
|
||||
from ..classes import BaseDocumentFilenameGenerator
|
||||
from ..events import event_document_type_created, event_document_type_edited
|
||||
from ..literals import DEFAULT_DELETE_PERIOD, DEFAULT_DELETE_TIME_UNIT
|
||||
from ..managers import DocumentTypeManager
|
||||
@@ -52,6 +53,12 @@ class DocumentType(models.Model):
|
||||
default=DEFAULT_DELETE_TIME_UNIT, max_length=8, null=True,
|
||||
verbose_name=_('Delete time unit')
|
||||
)
|
||||
filename_generator = models.CharField(
|
||||
help_text=_(
|
||||
'The class responsible for producing the actual filename used '
|
||||
'to store the uploaded documents.'
|
||||
), max_length=128, verbose_name=_('Filename generator')
|
||||
)
|
||||
|
||||
objects = DocumentTypeManager()
|
||||
|
||||
@@ -94,6 +101,10 @@ class DocumentType(models.Model):
|
||||
|
||||
return queryset.count()
|
||||
|
||||
def get_upload_filename(self, instance, filename):
|
||||
klass = BaseDocumentFilenameGenerator.get(name=self.filename_generator)
|
||||
return klass.upload_to(instance=instance, filename=filename)
|
||||
|
||||
def natural_key(self):
|
||||
return (self.label,)
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import hashlib
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import uuid
|
||||
|
||||
from django.apps import apps
|
||||
from django.core.files.base import ContentFile
|
||||
@@ -37,8 +36,10 @@ def hash_function():
|
||||
return hashlib.sha256()
|
||||
|
||||
|
||||
def UUID_FUNCTION(*args, **kwargs):
|
||||
return force_text(uuid.uuid4())
|
||||
def upload_to(instance, filename):
|
||||
return instance.document.document_type.get_upload_filename(
|
||||
instance=instance, filename=filename
|
||||
)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
@@ -86,7 +87,7 @@ class DocumentVersion(models.Model):
|
||||
|
||||
# File related fields
|
||||
file = models.FileField(
|
||||
storage=storage_documentversion, upload_to=UUID_FUNCTION,
|
||||
storage=storage_documentversion, upload_to=upload_to,
|
||||
verbose_name=_('File')
|
||||
)
|
||||
mimetype = models.CharField(
|
||||
|
||||
Reference in New Issue
Block a user