Commit 4be52abb authored by Gabriel Monnerat's avatar Gabriel Monnerat

erp5_document_scanner: refactor code to reuse Base_contribute and create PDF...

erp5_document_scanner: refactor code to reuse Base_contribute and create PDF with all images in background after submit
parent 97636bdb
"""
Use to contribute file to ERP5.
"""
portal = context.getPortalObject()
translateString = portal.Base_translateString
MARKER = ['', None]
portal_contributions = portal.portal_contributions
if synchronous_metadata_discovery is None:
synchronous_metadata_discovery = portal.portal_preferences.isPreferredSynchronousMetadataDiscovery(False)
user_login = portal.portal_membership.getAuthenticatedMember().getId()
document_kw = {'user_login': user_login,
'group': group,
'publication_section': publication_section}
if portal_type not in MARKER:
document_kw['portal_type'] = portal_type
from cStringIO import StringIO
document_kw['default_follow_up_uid'] = context.getUid()
for key in ('title', 'short_title', 'reference', 'language', 'version', 'description'):
value = kw.get(key, None)
if value not in MARKER:
document_kw[key] = value
document_kw.update({'discover_metadata': not synchronous_metadata_discovery})
portal = context.getPortalObject()
pdf_data_list = []
image_module = context.getPortalObject().image_module
image_module = portal.image_module
active_process = portal.restrictedTraverse(active_process_url)
for result in active_process.getResultList():
......@@ -38,40 +12,13 @@ for result in active_process.getResultList():
temp=True).convert(format="pdf")[1])
pdf_data = context.ERP5Site_mergePDFList(pdf_data_list=pdf_data_list)
file_object = StringIO(pdf_data)
# contribute file
document_kw.update({
'data': pdf_data,
extra_document_kw = {
"filename": "{}.pdf".format(
document_kw.get("title") or DateTime().strftime('%d-%m-%Y_%Hh%M'))
})
document = portal_contributions.newContent(**document_kw)
is_existing_document_updated = False
if synchronous_metadata_discovery:
# we need to do all synchronously, in other case portal_contributions will do
# this in an activity
if document.isSupportBaseDataConversion():
document.processFile()
filename = document.getFilename()
merged_document = document.Document_convertToBaseFormatAndDiscoverMetadata(
filename=filename,
user_login=user_login,
input_parameter_dict=document_kw)
is_existing_document_updated = (merged_document!=document)
document = merged_document
document_portal_type = document.getTranslatedPortalType()
if not is_existing_document_updated:
message = translateString('${portal_type} created successfully.',
mapping=dict(portal_type=document_portal_type))
else:
message = translateString('${portal_type} updated successfully.',
mapping=dict(portal_type=document_portal_type))
if batch_mode:
return document
kw.get("title") or DateTime().strftime('%d-%m-%Y_%Hh%M'))
}
return document.Base_redirect('view',
keep_items={'portal_status_message': message,
'editable_mode': editable_mode})
return context.Base_contribute(file=file_object,
extra_document_kw=extra_document_kw,
**kw)
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>portal_type=None, classification=None, synchronous_metadata_discovery=None, cancel_url=None, batch_mode=False, editable_mode=1, group=None, publication_section=None, document_scanner_gadget=None, active_process_url=None, **kw</string> </value>
<value> <string>active_process_url=None, **kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
portal = context.getPortalObject()
translateString = portal.Base_translateString
portal.portal_activities.activate().Base_uploadDocumentFromCamera(
active_process_url=active_process_url, **kw)
return context.Base_redirect('view',
keep_items={'portal_status_message':
translateString('The document is being created in background.')})
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>active_process_url=None, **kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_uploadDocumentFromCameraByActivity</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Contribute file or url to ERP5</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -35,7 +35,7 @@
</item>
<item>
<key> <string>action</string> </key>
<value> <string>Base_uploadDocumentFromCamera</string> </value>
<value> <string>Base_uploadDocumentFromCameraByActivity</string> </value>
</item>
<item>
<key> <string>description</string> </key>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment