refactored to merge document creation code
This commit is contained in:
parent
98b6b2fb71
commit
c68956b113
|
@ -21,6 +21,7 @@ from collective.dms.mailcontent.dmsmail import internalReferenceIncomingMailDefa
|
|||
from plone.app.registry.browser import controlpanel
|
||||
|
||||
from . import _
|
||||
from . import utils
|
||||
|
||||
log = logging.getLogger('collective.dms.batchimport')
|
||||
|
||||
|
@ -161,28 +162,9 @@ class BatchImporter(BrowserView):
|
|||
if hasattr(folder, document_id):
|
||||
raise BatchImportError('document already exists')
|
||||
|
||||
if not metadata:
|
||||
metadata = {}
|
||||
|
||||
if 'title' in metadata:
|
||||
document_title = metadata.get('title')
|
||||
del metadata['title']
|
||||
else:
|
||||
document_title = os.path.splitext(filename)[0].split('-', 1)[1]
|
||||
|
||||
if portal_type == 'dmsincomingmail':
|
||||
metadata['internal_reference_no'] = internalReferenceIncomingMailDefaultValue(self)
|
||||
metadata['reception_date'] = receptionDateDefaultValue(self)
|
||||
|
||||
log.info("creating the document for real (%s)" % document_id)
|
||||
folder.invokeFactory(portal_type, id=document_id, title=document_title,
|
||||
**metadata)
|
||||
|
||||
document = folder[document_id]
|
||||
|
||||
document_file = NamedBlobFile(file(filepath).read(), filename=unicode(filename))
|
||||
document.invokeFactory('dmsmainfile', id='main', title=_(u'Main File'),
|
||||
file=document_file)
|
||||
utils.createDocument(self, folder, portal_type, document_id,
|
||||
filename, document_file, metadata=metadata)
|
||||
|
||||
|
||||
class ControlPanelEditForm(controlpanel.RegistryEditForm):
|
||||
|
@ -193,4 +175,3 @@ class ControlPanelEditForm(controlpanel.RegistryEditForm):
|
|||
|
||||
class ControlPanel(controlpanel.ControlPanelFormWrapper):
|
||||
form = ControlPanelEditForm
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ import os
|
|||
import os.path
|
||||
|
||||
from zope import schema
|
||||
from plone.dexterity.utils import createContentInContainer
|
||||
|
||||
from zope.component import queryUtility
|
||||
from five import grok
|
||||
|
@ -15,11 +14,10 @@ from plone import api
|
|||
from plone.directives import form
|
||||
from plone.i18n.normalizer.interfaces import IIDNormalizer
|
||||
|
||||
from collective.dms.mailcontent.dmsmail import internalReferenceIncomingMailDefaultValue, receptionDateDefaultValue
|
||||
|
||||
from plone.namedfile.field import NamedFile, NamedBlobFile
|
||||
|
||||
from . import _
|
||||
from . import utils
|
||||
|
||||
log = logging.getLogger('collective.dms.batchimport')
|
||||
|
||||
|
@ -73,23 +71,7 @@ class ImportFileForm(form.SchemaForm):
|
|||
owner = data['owner']
|
||||
folder = self.get_folder(data['location'])
|
||||
|
||||
metadata = {}
|
||||
document_id = self.convertTitleToId(os.path.splitext(filename)[0])
|
||||
if data.get('title'):
|
||||
document_title = data.get('title')
|
||||
else:
|
||||
document_title = document_id
|
||||
|
||||
if portal_type == 'dmsincomingmail':
|
||||
metadata['internal_reference_no'] = internalReferenceIncomingMailDefaultValue(self)
|
||||
metadata['reception_date'] = receptionDateDefaultValue(self)
|
||||
|
||||
log.info('creating the document for real (%s)' % document_title)
|
||||
with api.env.adopt_user(username=owner):
|
||||
document = createContentInContainer(folder, portal_type,
|
||||
title=document_title, **metadata)
|
||||
log.info('document has been created (id: %s)' % document.id)
|
||||
|
||||
version = createContentInContainer(document, 'dmsmainfile',
|
||||
title=_('Scanned Mail'),
|
||||
file=data['file'])
|
||||
utils.createDocument(self, folder, portal_type, document_id,
|
||||
filename, data['file'], owner)
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
import logging
|
||||
import os
|
||||
|
||||
from plone import api
|
||||
from plone.dexterity.utils import createContentInContainer
|
||||
|
||||
from collective.dms.mailcontent.dmsmail import internalReferenceIncomingMailDefaultValue, receptionDateDefaultValue
|
||||
|
||||
from . import _
|
||||
|
||||
log = logging.getLogger('collective.dms.batchimport')
|
||||
|
||||
|
||||
def createDocument(context, folder, portal_type, document_id, filename,
|
||||
file_object, owner=None, metadata=None):
|
||||
if owner is None:
|
||||
owner = api.user.get_current().id
|
||||
|
||||
if not metadata:
|
||||
metadata = {}
|
||||
|
||||
if 'title' in metadata:
|
||||
document_title = metadata.get('title')
|
||||
del metadata['title']
|
||||
else:
|
||||
document_title = document_id
|
||||
|
||||
if portal_type == 'dmsincomingmail':
|
||||
metadata['internal_reference_no'] = internalReferenceIncomingMailDefaultValue(context)
|
||||
metadata['reception_date'] = receptionDateDefaultValue(context)
|
||||
|
||||
log.info('creating the document for real (%s)' % document_title)
|
||||
with api.env.adopt_user(username=owner):
|
||||
document = createContentInContainer(folder, portal_type,
|
||||
title=document_title, **metadata)
|
||||
log.info('document has been created (id: %s)' % document.id)
|
||||
|
||||
version = createContentInContainer(document, 'dmsmainfile',
|
||||
title=_('Scanned Mail'),
|
||||
file=file_object)
|
Reference in New Issue