add possibility to create version from previous or empty file (#3769)
This commit is contained in:
parent
01412996bc
commit
159e626788
|
@ -7,12 +7,18 @@ from Acquisition.interfaces import IAcquirer
|
|||
from zope.annotation.interfaces import IAnnotations
|
||||
|
||||
from zope import schema
|
||||
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
|
||||
from zope.schema.interfaces import IContextSourceBinder
|
||||
from zope.schema.interfaces import RequiredMissing
|
||||
from plone.supermodel import model
|
||||
from z3c.form import field, form, button
|
||||
|
||||
from Products.CMFCore.utils import getToolByName
|
||||
|
||||
from plone.dexterity.interfaces import IDexterityFTI
|
||||
from plone.dexterity.browser.add import DefaultAddView, DefaultAddForm
|
||||
from plone.namedfile.field import NamedBlobFile
|
||||
from plone.namedfile.field import NamedBlobFile as NamedBlobFileField
|
||||
from plone.namedfile.file import NamedBlobFile
|
||||
from plone.directives.form import default_value
|
||||
|
||||
|
||||
|
@ -21,14 +27,38 @@ from collective.dms.basecontent.dmsdocument import IDmsDocument
|
|||
|
||||
from collective.dms.basecontent import _
|
||||
|
||||
def make_terms(items):
|
||||
""" Create zope.schema terms for vocab from tuples """
|
||||
terms = [SimpleTerm(value=pair[0], token=pair[0], title=pair[1]) for pair in items]
|
||||
return terms
|
||||
|
||||
@grok.provider(IContextSourceBinder)
|
||||
def file_source(context):
|
||||
items = [('upload', _('Uploaded File'))]
|
||||
|
||||
# add an entry for previous version only if there's one
|
||||
portal_catalog = getToolByName(context , 'portal_catalog')
|
||||
folder_path = '/'.join(context.getPhysicalPath())
|
||||
results = portal_catalog.searchResults({'path': {'query': folder_path}, 'portal_type': 'dmsmainfile'})
|
||||
if results:
|
||||
items.append(('previous', _('Previous Version')))
|
||||
|
||||
items.append(('empty', _('Empty File')))
|
||||
return SimpleVocabulary(make_terms(items))
|
||||
|
||||
|
||||
class IAddVersion(model.Schema):
|
||||
model.primary('file')
|
||||
file = NamedBlobFile(
|
||||
file = NamedBlobFileField(
|
||||
title=_(u"File"),
|
||||
required=True,
|
||||
required=False,
|
||||
)
|
||||
|
||||
create_from = schema.Choice(
|
||||
title=_('Create File From'),
|
||||
required=True,
|
||||
source=file_source)
|
||||
|
||||
label = schema.TextLine(
|
||||
title=_(u'Label'),
|
||||
required=False,
|
||||
|
@ -48,6 +78,13 @@ class AddVersion(DefaultAddForm, form.AddForm):
|
|||
schema = IAddVersion
|
||||
portal_type = 'dmsmainfile'
|
||||
|
||||
def extractData(self):
|
||||
data, errors = super(AddVersion, self).extractData()
|
||||
if not errors:
|
||||
if data.get('create_from') == 'upload' and not data.get('file'):
|
||||
errors = (RequiredMissing('file'),)
|
||||
return (data, errors)
|
||||
|
||||
def create(self, data):
|
||||
fti = getUtility(IDexterityFTI, name=self.portal_type)
|
||||
|
||||
|
@ -69,7 +106,21 @@ class AddVersion(DefaultAddForm, form.AddForm):
|
|||
|
||||
content.signed = data.get('signed')
|
||||
content.label = data.get('label')
|
||||
content.file = data.get('file')
|
||||
if data.get('create_from') in (None, 'upload'):
|
||||
content.file = data.get('file')
|
||||
elif data.get('create_from') == 'previous':
|
||||
portal_catalog = getToolByName(self.context , 'portal_catalog')
|
||||
folder_path = '/'.join(self.context.getPhysicalPath())
|
||||
results = portal_catalog.searchResults({'path': {'query': folder_path},
|
||||
'portal_type': 'dmsmainfile',
|
||||
'sort_on': 'getObjPositionInParent',
|
||||
'sort_order': 'descending'})
|
||||
previous_file = results[0].getObject().file
|
||||
content.file = NamedBlobFile(previous_file.data,
|
||||
filename=previous_file.filename)
|
||||
elif data.get('create_from') == 'empty':
|
||||
content.file = NamedBlobFile(file('/tmp/empty.odt').read(),
|
||||
filename=u'empty.odt')
|
||||
|
||||
annotations = IAnnotations(container)
|
||||
if 'higher_version' not in annotations:
|
||||
|
|
Reference in New Issue