def reply(self): userid = self.request.form.get('userid') if not userid: return self.error(message='Missing userid.') file_ = self.request.form.get('file') if not file_: return self.error(message='Missing file.') # Disable CSRF protection if 'IDisableCSRFProtection' in dir(plone.protect.interfaces): alsoProvides(self.request, plone.protect.interfaces.IDisableCSRFProtection) try: adopt_user = api.env.adopt_user(username=userid) except api.exc.UserNotFoundError: return self.error(status=404, message='Unknown user.') with adopt_user: destination = self.destination() if destination: # XXX - Duck typing as error handling # We've encountered an error while discovering a destination if isinstance(destination, dict): return destination filename = file_.filename.decode('utf8') command = CreateDocumentCommand(destination, filename, file_) with elevated_privileges(): command.execute() self.request.response.setStatus(201) return super(ScanIn, self).reply()
def reply(self): userid = self.request.form.get('userid') if not userid: return self.error(message='Missing userid.') file_ = self.request.form.get('file') if not file_: return self.error(message='Missing file.') # Disable CSRF protection if 'IDisableCSRFProtection' in dir(plone.protect.interfaces): alsoProvides(self.request, plone.protect.interfaces.IDisableCSRFProtection) try: adopt_user = api.env.adopt_user(username=userid) except api.exc.UserNotFoundError: return self.error(message='Unknown user.') with adopt_user: destination = self.destination() if destination is None: return self.error(message='Destination does not exist.') filename = file_.filename.decode('utf8') command = CreateDocumentCommand(destination, filename, file_) command.execute() self.request.response.setStatus(201) return super(ScanIn, self).reply()
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
def test_create_document_from_command(self): self.login(self.regular_user) command = CreateDocumentCommand( self.dossier, 'testm\xc3\xa4il.txt', 'buh!', title='\xc3\x9cnicode') document = command.execute() self.assertIsInstance(document.title, unicode) self.assertEqual(u'\xdcnicode', document.title) self.assertEqual('buh!', document.file.data) self.assertEqual('text/plain', document.file.contentType)
def test_create_document_from_command(self): self.login(self.regular_user) command = CreateDocumentCommand(self.dossier, 'testm\xc3\xa4il.txt', 'buh!', title='\xc3\x9cnicode') document = command.execute() self.assertIsInstance(document.title, unicode) self.assertEqual(u'\xdcnicode', document.title) self.assertEqual('buh!', document.file.data) self.assertEqual('text/plain', document.file.contentType)
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