From 6534326df9dac44f542cbca130d3a3f8d30263a8 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 28 Oct 2014 01:35:13 -0400 Subject: [PATCH] Issue #8, Initial changes to allow update a document's document type. Added signal emited then the type is changed, added Document model method to update the document type --- mayan/apps/documents/models.py | 26 ++++++++++++++++++-------- mayan/apps/documents/signals.py | 1 + 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/mayan/apps/documents/models.py b/mayan/apps/documents/models.py index 046c31d8c5..b50dc91f20 100644 --- a/mayan/apps/documents/models.py +++ b/mayan/apps/documents/models.py @@ -37,7 +37,7 @@ from .managers import (DocumentManager, DocumentPageTransformationManager, from .runtime import storage_backend from .settings import (CACHE_PATH, CHECKSUM_FUNCTION, DISPLAY_SIZE, LANGUAGE, UUID_FUNCTION, ZOOM_MAX_LEVEL, ZOOM_MIN_LEVEL) -from .signals import post_version_upload +from .signals import post_version_upload, post_document_type_change HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() # document image cache name hash function logger = logging.getLogger(__name__) @@ -86,11 +86,26 @@ class Document(models.Model): label = models.CharField(max_length=255, default=_('Uninitialized document'), db_index=True, verbose_name=_('Label')) description = models.TextField(blank=True, null=True, verbose_name=_(u'Description')) date_added = models.DateTimeField(verbose_name=_(u'Added'), auto_now_add=True) - language = models.CharField(choices=LANGUAGE_CHOICES, default=LANGUAGE, max_length=8, verbose_name=_('Language') - ) + language = models.CharField(choices=LANGUAGE_CHOICES, default=LANGUAGE, max_length=8, verbose_name=_('Language')) objects = DocumentManager() + class Meta: + verbose_name = _(u'Document') + verbose_name_plural = _(u'Documents') + ordering = ['-date_added'] + + def set_document_type(self, document_type, force=False): + has_changed = self.document_type != document_type + print 'has_changed', has_changed + print 'self.document_type != document_type', self.document_type, document_type + + self.document_type = document_type + self.save() + if has_changed or force: + print 'sending!' + post_document_type_change.send(sender=self.__class__, instance=self) + @staticmethod def clear_image_cache(): for the_file in os.listdir(CACHE_PATH): @@ -98,11 +113,6 @@ class Document(models.Model): if os.path.isfile(file_path): os.unlink(file_path) - class Meta: - verbose_name = _(u'Document') - verbose_name_plural = _(u'Documents') - ordering = ['-date_added'] - def __unicode__(self): return self.label diff --git a/mayan/apps/documents/signals.py b/mayan/apps/documents/signals.py index 05b3991020..664e58aded 100644 --- a/mayan/apps/documents/signals.py +++ b/mayan/apps/documents/signals.py @@ -1,3 +1,4 @@ from django.dispatch import Signal post_version_upload = Signal(providing_args=['instance'], use_caching=True) +post_document_type_change = Signal(providing_args=['instance'], use_caching=True)