Esempio n. 1
0
    def test_save_pdf_under_form_sets_relation_to_source_document(self, browser):
        self.login(self.regular_user, browser)
        browser.open(self.document, view='save_pdf_under')
        browser.forms.get('form').find_field("Destination").fill(self.empty_dossier)
        with self.observe_children(self.empty_dossier) as children:
            browser.click_on("Save")

        self.assertEqual(len(children["added"]), 1)
        created_document = children["added"].pop()

        related_items = IRelatedDocuments(created_document).relatedItems
        self.assertEqual(len(related_items), 1)
        self.assertEqual(related_items[0].to_object, self.document)
Esempio n. 2
0
    def extract_attachment_into_parent(self, position):
        """Extract one specified attachment into the mails parent dossier or
        inbox.

        Also add a reference from all attached documents (*not* mails) to self.

        Position must be an integer attachment positions. The position
        can be obtained from the attachment description returned by
        `get_attachments`.
        """
        parent = self.get_extraction_parent()
        if parent is None:
            raise RuntimeError("Could not find a parent dossier or inbox for "
                               "{}".format(self.absolute_url()))

        data, content_type, filename = self._get_attachment_data(position)
        title = os.path.splitext(filename)[0]

        if content_type == 'message/rfc822':
            doc = CreateEmailCommand(parent,
                                     filename,
                                     data,
                                     title=title,
                                     content_type=content_type,
                                     digitally_available=True).execute()
        else:
            doc = CreateDocumentCommand(parent,
                                        filename,
                                        data,
                                        title=title,
                                        content_type=content_type,
                                        digitally_available=True).execute()

            # add a reference from the attachment to the mail
            intids = getUtility(IIntIds)
            iid = intids.getId(self)

            IRelatedDocuments(doc).relatedItems = [RelationValue(iid)]
            doc.reindexObject()

        return doc
Esempio n. 3
0
    def related_items(self, bidirectional=False, documents_only=False):
        _related_items = []

        relations = IRelatedDocuments(self).relatedItems

        if relations:
            _related_items += [rel.to_object for rel in relations]

        if bidirectional:
            catalog = getUtility(ICatalog)
            doc_id = getUtility(IIntIds).getId(aq_inner(self))
            relations = catalog.findRelations(
                {'to_id': doc_id, 'from_attribute': 'relatedItems'})

            if documents_only:
                relations = filter(
                    lambda rel: IBaseDocument.providedBy(rel.from_object),
                    relations)

            _related_items += [rel.from_object for rel in relations]

        return _related_items
Esempio n. 4
0
    def create_destination_document(self):
        # get all the metadata that will be set on the created file.
        # We blacklist some fields that should not be copied
        fields_to_skip = set((
            "file",
            "archival_file",
            "archival_file_state",
            "thumbnail",
            "preview",
            "digitally_available",
            "changeNote",
            "changed",
            "relatedItems",
        ))
        metadata = {}
        for schema in iterSchemataForType(self.context.portal_type):
            for name, schema_field in getFieldsInOrder(schema):
                if name in fields_to_skip:
                    continue
                field_instance = schema_field.bind(self.context)
                metadata[name] = field_instance.get(self.context)

        command = CreateDocumentCommand(self.destination, None, None,
                                        **metadata)
        destination_document = command.execute()

        # We make it in shadow state until its file is set by the callback view
        destination_document.as_shadow_document()
        # Add marker interface. This should be useful in the future for
        # cleanup jobs, retries if the PDF was not delivered and so on.
        alsoProvides(destination_document, IDocumentSavedAsPDFMarker)
        # Add annotations needed for the SavePDFDocumentUnder view.
        annotations = IAnnotations(destination_document)
        annotations[PDF_SAVE_SOURCE_UUID_KEY] = IUUID(self.context)
        annotations[PDF_SAVE_SOURCE_VERSION_KEY] = self.version_id

        # The missing_value attribute of a z3c-form field is used
        # as soon as an object has no default_value i.e. after creating
        # an object trough the command-line.
        #
        # Because the relatedItems field needs a list as a missing_value,
        # we will fall into the "mutable keyword argument"-python gotcha.
        # The relatedItems will be shared between the object-instances.
        #
        # Unfortunately the z3c-form field does not provide a
        # missing_value-factory (like the defaultFactory) which would be
        # necessary to fix this issue properly.
        #
        # As a workaround we make sure that the new document's relatedItems
        # is different object from the source document's.
        IRelatedDocuments(destination_document).relatedItems = list(
            IRelatedDocuments(destination_document).relatedItems)

        IRelatedDocuments(destination_document).relatedItems.append(
            RelationValue(getUtility(IIntIds).getId(self.context)))

        msg = _(
            u'Document ${document} was successfully created in ${destination}',
            mapping={
                "document": destination_document.title,
                "destination": self.destination.title
            })
        api.portal.show_message(msg, self.request, type='info')

        return destination_document
Esempio n. 5
0
 def related_items(self):
     relations = IRelatedDocuments(self).relatedItems
     if relations:
         return [rel.to_object for rel in relations]
     return []