add support for configurable templates as initial version content (#3769)
This commit is contained in:
parent
159e626788
commit
ee015e02ad
|
@ -1,3 +1,6 @@
|
|||
import json
|
||||
import os
|
||||
|
||||
from five import grok
|
||||
from zope.interface import implements
|
||||
from zope.component import getUtility, createObject
|
||||
|
@ -21,6 +24,8 @@ from plone.namedfile.field import NamedBlobFile as NamedBlobFileField
|
|||
from plone.namedfile.file import NamedBlobFile
|
||||
from plone.directives.form import default_value
|
||||
|
||||
from App.config import getConfiguration
|
||||
|
||||
|
||||
from collective.dms.basecontent.dmsfile import IDmsFile, DmsFile
|
||||
from collective.dms.basecontent.dmsdocument import IDmsDocument
|
||||
|
@ -43,7 +48,16 @@ def file_source(context):
|
|||
if results:
|
||||
items.append(('previous', _('Previous Version')))
|
||||
|
||||
items.append(('empty', _('Empty File')))
|
||||
config = getConfiguration()
|
||||
version_templates = os.path.join(config.zopehome, 'etc',
|
||||
'version_templates', 'list.json')
|
||||
if os.path.exists(version_templates):
|
||||
templates = json.load(file(version_templates))
|
||||
for entry in templates:
|
||||
if entry.get('portal_types') and context.portal_type not in entry.get('portal_types'):
|
||||
continue
|
||||
items.append(('template-%s' % entry.get('filename'), entry.get('label')))
|
||||
|
||||
return SimpleVocabulary(make_terms(items))
|
||||
|
||||
|
||||
|
@ -118,9 +132,16 @@ class AddVersion(DefaultAddForm, form.AddForm):
|
|||
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')
|
||||
elif data.get('create_from').startswith('template-'):
|
||||
config = getConfiguration()
|
||||
base_path = os.path.join(config.zopehome, 'etc', 'version_templates')
|
||||
filename = os.path.realpath(os.path.join(base_path,
|
||||
data.get('create_from')[9:]))
|
||||
if not filename.startswith(base_path):
|
||||
raise Exception('wrong template')
|
||||
|
||||
content.file = NamedBlobFile(file(filename).read(),
|
||||
filename=data.get('create_from')[9:])
|
||||
|
||||
annotations = IAnnotations(container)
|
||||
if 'higher_version' not in annotations:
|
||||
|
|
Reference in New Issue