def save(self, *args, **kwargs):
        """
        Overloaded save method that updates the document version's checksum,
        mimetype, and page count when created
        """
        user = kwargs.pop('_user', None)
        new_document_version = not self.pk

        if new_document_version:
            logger.info('Creating new version for document: %s', self.document)

        try:
            with transaction.atomic():
                self.execute_pre_save_hooks()

                signal_mayan_pre_save.send(instance=self,
                                           sender=DocumentVersion,
                                           user=user)

                super(DocumentVersion, self).save(*args, **kwargs)

                DocumentVersion._execute_hooks(
                    hook_list=DocumentVersion._post_save_hooks, instance=self)

                if new_document_version:
                    # Only do this for new documents
                    self.update_checksum(save=False)
                    self.update_mimetype(save=False)
                    self.save()
                    self.update_page_count(save=False)
                    if setting_fix_orientation.value:
                        self.fix_orientation()

                    logger.info(
                        'New document version "%s" created for document: %s',
                        self, self.document)

                    self.document.is_stub = False
                    if not self.document.label:
                        self.document.label = force_text(s=self.file)

                    self.document.save(_commit_events=False)
        except Exception as exception:
            logger.error(
                'Error creating new document version for document "%s"; %s',
                self.document, exception)
            raise
        else:
            if new_document_version:
                event_document_version_new.commit(actor=user,
                                                  target=self,
                                                  action_object=self.document)
                signal_post_version_upload.send(sender=DocumentVersion,
                                                instance=self)

                if tuple(self.document.versions.all()) == (self, ):
                    signal_post_document_created.send(instance=self.document,
                                                      sender=Document)
示例#2
0
    def save(self, *args, **kwargs):
        user = kwargs.pop('_user', None)
        _commit_events = kwargs.pop('_commit_events', True)
        new_document = not self.pk
        with transaction.atomic():
            signal_mayan_pre_save.send(
                sender=Document, instance=self, user=user
            )

            super(Document, self).save(*args, **kwargs)

            if new_document:
                if user:
                    self.add_as_recent_document_for_user(user=user)
                    event_document_create.commit(
                        actor=user, target=self, action_object=self.document_type
                    )
                else:
                    event_document_create.commit(
                        target=self, action_object=self.document_type
                    )
            else:
                if _commit_events:
                    event_document_properties_edit.commit(actor=user, target=self)