collective.documentgenerator 3.46__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- collective/documentgenerator/AT_renderer.py +98 -0
- collective/documentgenerator/DX_renderer.py +52 -0
- collective/documentgenerator/__init__.py +17 -0
- collective/documentgenerator/adapters.py +23 -0
- collective/documentgenerator/browser/__init__.py +0 -0
- collective/documentgenerator/browser/actionspanel.py +19 -0
- collective/documentgenerator/browser/check_pod_templates.pt +63 -0
- collective/documentgenerator/browser/children_pod_template.pt +16 -0
- collective/documentgenerator/browser/configure.zcml +220 -0
- collective/documentgenerator/browser/controlpanel.py +159 -0
- collective/documentgenerator/browser/converter.py +43 -0
- collective/documentgenerator/browser/generation_view.py +532 -0
- collective/documentgenerator/browser/overrides.py +9 -0
- collective/documentgenerator/browser/resources/css/documentgenerator.css +133 -0
- collective/documentgenerator/browser/static/.gitkeep +0 -0
- collective/documentgenerator/browser/static/configurable.svg +1232 -0
- collective/documentgenerator/browser/static/csv.svg +521 -0
- collective/documentgenerator/browser/static/doc.svg +23 -0
- collective/documentgenerator/browser/static/docx.svg +20 -0
- collective/documentgenerator/browser/static/download_icon.svg +54 -0
- collective/documentgenerator/browser/static/libreoffice-blanck.svg +481 -0
- collective/documentgenerator/browser/static/mailinglooptemplate.svg +1207 -0
- collective/documentgenerator/browser/static/missing_use.svg +42 -0
- collective/documentgenerator/browser/static/nok.svg +38 -0
- collective/documentgenerator/browser/static/nok_use.svg +38 -0
- collective/documentgenerator/browser/static/nok_used.svg +38 -0
- collective/documentgenerator/browser/static/odp.svg +1264 -0
- collective/documentgenerator/browser/static/ods.svg +1354 -0
- collective/documentgenerator/browser/static/odt.svg +1347 -0
- collective/documentgenerator/browser/static/ok.svg +38 -0
- collective/documentgenerator/browser/static/ok_use.svg +38 -0
- collective/documentgenerator/browser/static/ok_used.svg +38 -0
- collective/documentgenerator/browser/static/pdf.svg +557 -0
- collective/documentgenerator/browser/static/podtemplate.svg +1197 -0
- collective/documentgenerator/browser/static/rtf.svg +521 -0
- collective/documentgenerator/browser/static/style.svg +1207 -0
- collective/documentgenerator/browser/static/subtemplate.svg +1250 -0
- collective/documentgenerator/browser/static/xls.svg +159 -0
- collective/documentgenerator/browser/static/xlsx.svg +48 -0
- collective/documentgenerator/browser/table.py +277 -0
- collective/documentgenerator/browser/templates_listing.pt +23 -0
- collective/documentgenerator/browser/views.py +365 -0
- collective/documentgenerator/config.py +162 -0
- collective/documentgenerator/configure.zcml +41 -0
- collective/documentgenerator/content/__init__.py +0 -0
- collective/documentgenerator/content/condition.py +60 -0
- collective/documentgenerator/content/conditions.zcml +29 -0
- collective/documentgenerator/content/configure.zcml +25 -0
- collective/documentgenerator/content/document_factory.py +73 -0
- collective/documentgenerator/content/file_representation.py +73 -0
- collective/documentgenerator/content/file_representation.zcml +28 -0
- collective/documentgenerator/content/merge_templates.py +15 -0
- collective/documentgenerator/content/merge_templates.zcml +10 -0
- collective/documentgenerator/content/pod_template.py +547 -0
- collective/documentgenerator/content/style_template.py +52 -0
- collective/documentgenerator/content/vocabulary.py +315 -0
- collective/documentgenerator/content/vocabulary.zcml +104 -0
- collective/documentgenerator/demo/__init__.py +0 -0
- collective/documentgenerator/demo/archetypes.zcml +10 -0
- collective/documentgenerator/demo/configure.zcml +19 -0
- collective/documentgenerator/demo/helper.py +198 -0
- collective/documentgenerator/events/__init__.py +0 -0
- collective/documentgenerator/events/configure.zcml +30 -0
- collective/documentgenerator/events/pod_templates_events.py +66 -0
- collective/documentgenerator/events/styles_events.py +122 -0
- collective/documentgenerator/helper/__init__.py +9 -0
- collective/documentgenerator/helper/archetypes-adapter.zcml +10 -0
- collective/documentgenerator/helper/archetypes-page.zcml +9 -0
- collective/documentgenerator/helper/archetypes.py +67 -0
- collective/documentgenerator/helper/base.py +266 -0
- collective/documentgenerator/helper/configure.zcml +38 -0
- collective/documentgenerator/helper/dexterity.py +174 -0
- collective/documentgenerator/interfaces.py +165 -0
- collective/documentgenerator/locales/collective.documentgenerator.pot +564 -0
- collective/documentgenerator/locales/es/LC_MESSAGES/collective.documentgenerator.po +572 -0
- collective/documentgenerator/locales/es/LC_MESSAGES/plone.po +32 -0
- collective/documentgenerator/locales/fr/LC_MESSAGES/collective.documentgenerator.po +566 -0
- collective/documentgenerator/locales/fr/LC_MESSAGES/plone.po +27 -0
- collective/documentgenerator/locales/manual.pot +30 -0
- collective/documentgenerator/locales/plone-manual.pot +22 -0
- collective/documentgenerator/locales/plone.pot +30 -0
- collective/documentgenerator/locales/update.sh +22 -0
- collective/documentgenerator/migrations/__init__.py +0 -0
- collective/documentgenerator/migrations/migrate_to_11.py +53 -0
- collective/documentgenerator/migrations/migrate_to_12.py +30 -0
- collective/documentgenerator/migrations/migrate_to_13.py +37 -0
- collective/documentgenerator/migrations/migrate_to_3.py +36 -0
- collective/documentgenerator/migrations/migrate_to_5.py +30 -0
- collective/documentgenerator/migrations/migrate_to_6.py +27 -0
- collective/documentgenerator/migrations/migrate_to_7.py +32 -0
- collective/documentgenerator/migrations/migrate_to_8.py +34 -0
- collective/documentgenerator/migrations/migrate_to_9.py +48 -0
- collective/documentgenerator/profiles/base/browserlayer.xml +7 -0
- collective/documentgenerator/profiles/base/controlpanel.xml +20 -0
- collective/documentgenerator/profiles/base/metadata.xml +7 -0
- collective/documentgenerator/profiles/base/types/ConfigurablePODTemplate.xml +43 -0
- collective/documentgenerator/profiles/base/types/MailingLoopTemplate.xml +42 -0
- collective/documentgenerator/profiles/base/types/PODTemplate.xml +42 -0
- collective/documentgenerator/profiles/base/types/StyleTemplate.xml +42 -0
- collective/documentgenerator/profiles/base/types/SubTemplate.xml +42 -0
- collective/documentgenerator/profiles/base/types.xml +10 -0
- collective/documentgenerator/profiles/base/viewlets.xml +6 -0
- collective/documentgenerator/profiles/demo/browserlayer.xml +7 -0
- collective/documentgenerator/profiles/demo/collectivedocumentgenerator_demo_marker.txt +0 -0
- collective/documentgenerator/profiles/demo/import_steps.xml +11 -0
- collective/documentgenerator/profiles/demo/metadata.xml +7 -0
- collective/documentgenerator/profiles/demo/registry.xml +13 -0
- collective/documentgenerator/profiles/demo/templates/mailing.odt +0 -0
- collective/documentgenerator/profiles/demo/templates/modele_general.ods +0 -0
- collective/documentgenerator/profiles/demo/templates/modele_general.odt +0 -0
- collective/documentgenerator/profiles/demo/templates/mod/303/250le_collection.odt +0 -0
- collective/documentgenerator/profiles/demo/templates/possibly_mailed_model.odt +0 -0
- collective/documentgenerator/profiles/demo/templates/styles.odt +0 -0
- collective/documentgenerator/profiles/demo/templates/styles_2.odt +0 -0
- collective/documentgenerator/profiles/demo/templates/sub_template.odt +0 -0
- collective/documentgenerator/profiles/demo/viewlets.xml +3 -0
- collective/documentgenerator/profiles/plone4/collectivedocumentgenerator_marker.txt +0 -0
- collective/documentgenerator/profiles/plone4/cssregistry.xml +11 -0
- collective/documentgenerator/profiles/plone4/metadata.xml +7 -0
- collective/documentgenerator/profiles/plone4/registry.xml +6 -0
- collective/documentgenerator/profiles/plone5/metadata.xml +7 -0
- collective/documentgenerator/profiles/plone5/registry.xml +15 -0
- collective/documentgenerator/profiles/testing/metadata.xml +7 -0
- collective/documentgenerator/profiles/testing/propertiestool.xml +5 -0
- collective/documentgenerator/profiles/testing/types/member.xml +85 -0
- collective/documentgenerator/profiles/testing/types.xml +5 -0
- collective/documentgenerator/profiles.zcml +186 -0
- collective/documentgenerator/renderer.zcml +145 -0
- collective/documentgenerator/search_replace/__init__.py +0 -0
- collective/documentgenerator/search_replace/configure.zcml +19 -0
- collective/documentgenerator/search_replace/pod_template.py +146 -0
- collective/documentgenerator/search_replace/search_result_form.pt +48 -0
- collective/documentgenerator/search_replace/searchreplace_panel.pt +22 -0
- collective/documentgenerator/search_replace/searchreplace_panel.py +261 -0
- collective/documentgenerator/setuphandlers.py +287 -0
- collective/documentgenerator/testing.py +324 -0
- collective/documentgenerator/testing.zcml +16 -0
- collective/documentgenerator/tests/__init__.py +0 -0
- collective/documentgenerator/tests/failing_template.odt +0 -0
- collective/documentgenerator/tests/robot/.gitkeep +0 -0
- collective/documentgenerator/tests/template_to_clean.odt +0 -0
- collective/documentgenerator/tests/template_without_style_page.odt +0 -0
- collective/documentgenerator/tests/test_AT_helper_view.py +276 -0
- collective/documentgenerator/tests/test_DX_helper_view.py +355 -0
- collective/documentgenerator/tests/test_base_helper_view.py +128 -0
- collective/documentgenerator/tests/test_check_podtemplates_view.py +138 -0
- collective/documentgenerator/tests/test_config.py +106 -0
- collective/documentgenerator/tests/test_configurable_pod_template.py +383 -0
- collective/documentgenerator/tests/test_demo_profile.py +115 -0
- collective/documentgenerator/tests/test_events.py +169 -0
- collective/documentgenerator/tests/test_file.odt +0 -0
- collective/documentgenerator/tests/test_generation_view.py +652 -0
- collective/documentgenerator/tests/test_mailing.odt +0 -0
- collective/documentgenerator/tests/test_mailing_loop_template.py +48 -0
- collective/documentgenerator/tests/test_pod_template.py +178 -0
- collective/documentgenerator/tests/test_robot.py +26 -0
- collective/documentgenerator/tests/test_search_replace.py +419 -0
- collective/documentgenerator/tests/test_setup.py +49 -0
- collective/documentgenerator/tests/test_style_template.py +19 -0
- collective/documentgenerator/tests/test_sub_template.py +49 -0
- collective/documentgenerator/tests/test_templates_listing_view.py +91 -0
- collective/documentgenerator/tests/test_utils.py +244 -0
- collective/documentgenerator/tests/test_vocabulary.py +154 -0
- collective/documentgenerator/utils.py +396 -0
- collective/documentgenerator/viewlets/__init__.py +0 -0
- collective/documentgenerator/viewlets/batchactions.py +14 -0
- collective/documentgenerator/viewlets/configure.zcml +16 -0
- collective/documentgenerator/viewlets/generationlinks.pt +28 -0
- collective/documentgenerator/viewlets/generationlinks.py +58 -0
- collective.documentgenerator-3.46-py3.13-nspkg.pth +1 -0
- collective_documentgenerator-3.46.dist-info/METADATA +1142 -0
- collective_documentgenerator-3.46.dist-info/RECORD +176 -0
- collective_documentgenerator-3.46.dist-info/WHEEL +5 -0
- collective_documentgenerator-3.46.dist-info/entry_points.txt +5 -0
- collective_documentgenerator-3.46.dist-info/namespace_packages.txt +1 -0
- collective_documentgenerator-3.46.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from collective.documentgenerator.utils import convert_and_save_file
|
|
3
|
+
from collective.documentgenerator.utils import convert_file
|
|
4
|
+
from plone.namedfile.file import NamedBlobFile
|
|
5
|
+
from Products.Five import BrowserView
|
|
6
|
+
|
|
7
|
+
import mimetypes
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class DocumentConvertView(BrowserView):
|
|
11
|
+
"""A view to convert document from odt to pdf and download it"""
|
|
12
|
+
|
|
13
|
+
def get_params(self):
|
|
14
|
+
fmt = self.request.get('format', 'pdf')
|
|
15
|
+
output_name = self.request.get('output_name', "untitled.{}".format(fmt))
|
|
16
|
+
if not output_name and isinstance(self.context.file, NamedBlobFile):
|
|
17
|
+
output_name = self.context.file.filename.replace('.odt', '.{}'.format(fmt))
|
|
18
|
+
return output_name, fmt
|
|
19
|
+
|
|
20
|
+
def __call__(self):
|
|
21
|
+
output_name, fmt = self.get_params()
|
|
22
|
+
converted_filename, converted_file = convert_file(self.context.file, output_name, fmt=fmt)
|
|
23
|
+
|
|
24
|
+
# Set headers
|
|
25
|
+
response = self.request.RESPONSE
|
|
26
|
+
mimetype = mimetypes.guess_type(converted_filename)[0]
|
|
27
|
+
response.setHeader('Content-type', mimetype)
|
|
28
|
+
response.setHeader(
|
|
29
|
+
'Content-disposition',
|
|
30
|
+
u'inline;filename="{}"'.format(converted_filename).encode('utf-8')
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
return converted_file
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class PersistentDocumentConvertView(DocumentConvertView):
|
|
37
|
+
"""A view to convert document from odt to pdf and save it on the parent"""
|
|
38
|
+
|
|
39
|
+
def __call__(self):
|
|
40
|
+
output_name, fmt = self.get_params()
|
|
41
|
+
convert_and_save_file(self.context.file, self.context.aq_parent, self.context.portal_type, output_name, fmt=fmt)
|
|
42
|
+
|
|
43
|
+
self.request.response.redirect(self.context.aq_parent.absolute_url())
|
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .. import _
|
|
4
|
+
from AccessControl import Unauthorized
|
|
5
|
+
from appy.pod.renderer import CsvOptions
|
|
6
|
+
from appy.pod.renderer import Renderer
|
|
7
|
+
from appy.pod.styles_manager import TableProperties
|
|
8
|
+
from collective.documentgenerator import config
|
|
9
|
+
from collective.documentgenerator import utils
|
|
10
|
+
from collective.documentgenerator.content.pod_template import IPODTemplate
|
|
11
|
+
from collective.documentgenerator.interfaces import CyclicMergeTemplatesException
|
|
12
|
+
from collective.documentgenerator.interfaces import IDocumentFactory
|
|
13
|
+
from collective.documentgenerator.interfaces import PODTemplateNotFoundError
|
|
14
|
+
from collective.documentgenerator.utils import remove_tmp_file
|
|
15
|
+
from collective.documentgenerator.utils import temporary_file_name
|
|
16
|
+
from imio.helpers import HAS_PLONE_5_AND_MORE
|
|
17
|
+
from plone import api
|
|
18
|
+
from plone.app.uuid.utils import uuidToObject
|
|
19
|
+
from plone.i18n.normalizer.interfaces import IFileNameNormalizer
|
|
20
|
+
from Products.CMFPlone.utils import base_hasattr
|
|
21
|
+
from Products.CMFPlone.utils import safe_unicode
|
|
22
|
+
from Products.Five import BrowserView
|
|
23
|
+
from StringIO import StringIO
|
|
24
|
+
from zope.annotation.interfaces import IAnnotations
|
|
25
|
+
from zope.component import getMultiAdapter
|
|
26
|
+
from zope.component import queryAdapter
|
|
27
|
+
from zope.component import queryUtility
|
|
28
|
+
|
|
29
|
+
import mimetypes
|
|
30
|
+
import os
|
|
31
|
+
import pkg_resources
|
|
32
|
+
import unicodedata
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
HAS_FINGERPOINTING = None
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
pkg_resources.get_distribution('collective.fingerpointing')
|
|
39
|
+
except pkg_resources.DistributionNotFound:
|
|
40
|
+
HAS_FINGERPOINTING = False
|
|
41
|
+
else:
|
|
42
|
+
HAS_FINGERPOINTING = True
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class DocumentGenerationView(BrowserView):
|
|
46
|
+
"""
|
|
47
|
+
Document generation view.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def __init__(self, context, request):
|
|
51
|
+
self.context = context
|
|
52
|
+
self.request = request
|
|
53
|
+
self.pod_template = None
|
|
54
|
+
self.output_format = None
|
|
55
|
+
|
|
56
|
+
def __call__(self, template_uid='', output_format='', **kwargs):
|
|
57
|
+
self.pod_template, self.output_format = self._get_base_args(template_uid, output_format)
|
|
58
|
+
return self.generate_and_download_doc(self.pod_template, self.output_format, **kwargs)
|
|
59
|
+
|
|
60
|
+
def _get_base_args(self, template_uid, output_format):
|
|
61
|
+
template_uid = template_uid or self.get_pod_template_uid()
|
|
62
|
+
pod_template = self.get_pod_template(template_uid)
|
|
63
|
+
output_format = output_format or self.get_generation_format()
|
|
64
|
+
if not output_format:
|
|
65
|
+
raise Exception("No 'output_format' found to generate this document")
|
|
66
|
+
|
|
67
|
+
return pod_template, output_format
|
|
68
|
+
|
|
69
|
+
def generate_and_download_doc(self, pod_template, output_format, **kwargs):
|
|
70
|
+
"""
|
|
71
|
+
Generate a document of format 'output_format' from the template
|
|
72
|
+
'pod_template' and return it as a downloadable file.
|
|
73
|
+
"""
|
|
74
|
+
if HAS_FINGERPOINTING:
|
|
75
|
+
from collective.fingerpointing.config import AUDIT_MESSAGE
|
|
76
|
+
from collective.fingerpointing.logger import log_info
|
|
77
|
+
from collective.fingerpointing.utils import get_request_information
|
|
78
|
+
|
|
79
|
+
# add logging message to fingerpointing log
|
|
80
|
+
user, ip = get_request_information()
|
|
81
|
+
action = 'generate_document'
|
|
82
|
+
extras = 'context={0} pod_template={1} output_format={2}'.format(
|
|
83
|
+
'/'.join(self.context.getPhysicalPath()),
|
|
84
|
+
'/'.join(pod_template.getPhysicalPath()),
|
|
85
|
+
output_format)
|
|
86
|
+
allowed_parameters = filter(
|
|
87
|
+
None,
|
|
88
|
+
os.getenv("DOCUMENTGENERATOR_LOG_PARAMETERS", "").split(",")
|
|
89
|
+
)
|
|
90
|
+
if allowed_parameters:
|
|
91
|
+
for key, value in self.request.form.items():
|
|
92
|
+
if key in allowed_parameters:
|
|
93
|
+
extras = "{0} {1}={2}".format(extras, key, value)
|
|
94
|
+
log_info(AUDIT_MESSAGE.format(user, ip, action, extras))
|
|
95
|
+
|
|
96
|
+
doc, doc_name, gen_context = self._generate_doc(pod_template, output_format, **kwargs)
|
|
97
|
+
self._set_header_response(doc_name)
|
|
98
|
+
return doc
|
|
99
|
+
|
|
100
|
+
def _generate_doc(self, pod_template, output_format, **kwargs):
|
|
101
|
+
"""
|
|
102
|
+
Generate a document of format 'output_format' from the template
|
|
103
|
+
'pod_template'.
|
|
104
|
+
"""
|
|
105
|
+
if not pod_template.can_be_generated(self.context):
|
|
106
|
+
raise Unauthorized('You are not allowed to generate this document.')
|
|
107
|
+
|
|
108
|
+
if output_format not in pod_template.get_available_formats():
|
|
109
|
+
raise Exception(
|
|
110
|
+
"Asked output format '{0}' "
|
|
111
|
+
"is not available for template '{1}'!".format(
|
|
112
|
+
output_format,
|
|
113
|
+
pod_template.getId()
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
# subtemplates should not refer to each other in a cyclic way.
|
|
118
|
+
self._check_cyclic_merges(pod_template)
|
|
119
|
+
|
|
120
|
+
# Recursive generation of the document and all its subtemplates.
|
|
121
|
+
document_path, gen_context = self._recursive_generate_doc(pod_template, output_format, **kwargs)
|
|
122
|
+
|
|
123
|
+
rendered_document = open(document_path, 'rb')
|
|
124
|
+
rendered = rendered_document.read()
|
|
125
|
+
rendered_document.close()
|
|
126
|
+
remove_tmp_file(document_path)
|
|
127
|
+
filename = self._get_filename()
|
|
128
|
+
return rendered, filename, gen_context
|
|
129
|
+
|
|
130
|
+
def _get_filename(self):
|
|
131
|
+
""" """
|
|
132
|
+
# we limit filename to 120 characters
|
|
133
|
+
first_part = u'{0} {1}'.format(self.pod_template.title, safe_unicode(self.context.Title()))
|
|
134
|
+
# replace unicode special characters with ascii equivalent value
|
|
135
|
+
first_part = unicodedata.normalize('NFKD', first_part).encode('ascii', 'ignore')
|
|
136
|
+
util = queryUtility(IFileNameNormalizer)
|
|
137
|
+
# remove '-' from first_part because it is handled by cropName that manages max_length
|
|
138
|
+
# and it behaves weirdly if it encounters '-'
|
|
139
|
+
# moreover avoid more than one blank space at a time
|
|
140
|
+
first_part = u' '.join(util.normalize(first_part).replace(u'-', u' ').split()).strip()
|
|
141
|
+
filename = '{0}.{1}'.format(util.normalize(first_part, max_length=120), self.output_format)
|
|
142
|
+
return filename
|
|
143
|
+
|
|
144
|
+
def _recursive_generate_doc(self, pod_template, output_format, **kwargs):
|
|
145
|
+
"""
|
|
146
|
+
Generate a document recursively by starting to generate all its
|
|
147
|
+
subtemplates before merging them in the final document.
|
|
148
|
+
Return the file path of the generated document.
|
|
149
|
+
"""
|
|
150
|
+
sub_templates = pod_template.get_templates_to_merge()
|
|
151
|
+
sub_documents = {}
|
|
152
|
+
for context_name, (sub_pod, do_rendering) in sub_templates.iteritems():
|
|
153
|
+
# Force the subtemplate output_format to 'odt' because appy.pod
|
|
154
|
+
# can only merge documents in this format.
|
|
155
|
+
if do_rendering:
|
|
156
|
+
sub_documents[context_name] = self._recursive_generate_doc(
|
|
157
|
+
pod_template=sub_pod,
|
|
158
|
+
output_format='odt'
|
|
159
|
+
)[0]
|
|
160
|
+
else:
|
|
161
|
+
sub_documents[context_name] = sub_pod
|
|
162
|
+
|
|
163
|
+
document_path, gen_context = self._render_document(pod_template, output_format, sub_documents, **kwargs)
|
|
164
|
+
|
|
165
|
+
return document_path, gen_context
|
|
166
|
+
|
|
167
|
+
def get_pod_template(self, template_uid):
|
|
168
|
+
"""
|
|
169
|
+
Return the default PODTemplate that will be used when calling
|
|
170
|
+
this view.
|
|
171
|
+
"""
|
|
172
|
+
catalog = api.portal.get_tool('portal_catalog')
|
|
173
|
+
|
|
174
|
+
template_brains = catalog.unrestrictedSearchResults(
|
|
175
|
+
object_provides=IPODTemplate.__identifier__,
|
|
176
|
+
UID=template_uid
|
|
177
|
+
)
|
|
178
|
+
if not template_brains:
|
|
179
|
+
raise PODTemplateNotFoundError(
|
|
180
|
+
"Couldn't find POD template with UID '{0}'".format(template_uid)
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
template_path = template_brains[0].getPath()
|
|
184
|
+
pod_template = self.context.unrestrictedTraverse(template_path)
|
|
185
|
+
return pod_template
|
|
186
|
+
|
|
187
|
+
def get_pod_template_uid(self):
|
|
188
|
+
template_uid = self.request.get('template_uid', '')
|
|
189
|
+
return template_uid
|
|
190
|
+
|
|
191
|
+
def get_generation_format(self):
|
|
192
|
+
"""
|
|
193
|
+
Return the default document output format that will be used
|
|
194
|
+
when calling this view.
|
|
195
|
+
"""
|
|
196
|
+
output_format = self.request.get('output_format')
|
|
197
|
+
return output_format
|
|
198
|
+
|
|
199
|
+
def _render_document(self, pod_template, output_format, sub_documents, raiseOnError=False, **kwargs):
|
|
200
|
+
"""
|
|
201
|
+
Render a single document of type 'output_format' using the odt file
|
|
202
|
+
'document_template' as the generation template.
|
|
203
|
+
Subdocuments is a dictionnary of previously generated subtemplate
|
|
204
|
+
that will be merged into the current generated document.
|
|
205
|
+
"""
|
|
206
|
+
document_template = pod_template.get_file()
|
|
207
|
+
temp_filename = temporary_file_name('.{extension}'.format(extension=output_format))
|
|
208
|
+
|
|
209
|
+
# Prepare rendering context
|
|
210
|
+
helper_view = self.get_generation_context_helper()
|
|
211
|
+
generation_context = self._get_generation_context(helper_view, pod_template=pod_template)
|
|
212
|
+
|
|
213
|
+
# enrich the generation context with previously generated documents
|
|
214
|
+
utils.update_dict_with_validation(generation_context, sub_documents,
|
|
215
|
+
_("Error when merging merge_templates in generation context"))
|
|
216
|
+
|
|
217
|
+
# enable optimalColumnWidths if enabled in the config and/or on ConfigurablePodTemplate
|
|
218
|
+
stylesMapping = {}
|
|
219
|
+
optimalColumnWidths = "OCW_.*"
|
|
220
|
+
distributeColumns = "DC_.*"
|
|
221
|
+
|
|
222
|
+
column_modifier = pod_template.get_column_modifier()
|
|
223
|
+
if column_modifier == -1:
|
|
224
|
+
column_modifier = config.get_column_modifier()
|
|
225
|
+
|
|
226
|
+
if column_modifier == 'disabled':
|
|
227
|
+
optimalColumnWidths = False
|
|
228
|
+
distributeColumns = False
|
|
229
|
+
else:
|
|
230
|
+
stylesMapping = {
|
|
231
|
+
'table': TableProperties(columnModifier=column_modifier != 'nothing' and column_modifier or None)}
|
|
232
|
+
|
|
233
|
+
# if raiseOnError is not enabled, enabled it in the config excepted if user is a Manager
|
|
234
|
+
# and currently generated document use odt format
|
|
235
|
+
if not raiseOnError:
|
|
236
|
+
if config.get_raiseOnError_for_non_managers():
|
|
237
|
+
raiseOnError = True
|
|
238
|
+
if 'Manager' in api.user.get_roles() and output_format == 'odt':
|
|
239
|
+
raiseOnError = False
|
|
240
|
+
|
|
241
|
+
# stylesMapping.update({'para[class=None, parent != cell]': 'texte_delibe',
|
|
242
|
+
# 'para[class=xSmallText, parent != cell]': 'bodyXSmall',
|
|
243
|
+
# 'para[class=smallText, parent != cell]': 'bodySmall',
|
|
244
|
+
# 'para[class=largeText, parent != cell]': 'bodyLarge',
|
|
245
|
+
# 'para[class=xLargeText, parent != cell]': 'bodyXLarge',
|
|
246
|
+
# 'para[class=indentation, parent != cell]': 'bodyIndentation',
|
|
247
|
+
# 'para[class=None, parent = cell]': 'cell_delibe',
|
|
248
|
+
# 'table': TableProperties(cellContentStyle='cell_delibe'),
|
|
249
|
+
# 'para[class=xSmallText, parent = cell]': 'cellXSmall',
|
|
250
|
+
# 'para[class=smallText, parent = cell]': 'cellSmall',
|
|
251
|
+
# 'para[class=largeText, parent = cell]': 'cellLarge',
|
|
252
|
+
# 'para[class=xLargeText, parent = cell]': 'cellXLarge',
|
|
253
|
+
# 'para[class=indentation, parent = cell]': 'cellIndentation',
|
|
254
|
+
# })
|
|
255
|
+
# stylesMapping.update({'para[class=None,parent!=cell]': 'texte_delibe',
|
|
256
|
+
# 'para[class=xSmallText,parent!=cell]': 'bodyXSmall',
|
|
257
|
+
# 'para[class=smallText,parent!=cell]': 'bodySmall',
|
|
258
|
+
# 'para[class=largeText,parent!=cell]': 'bodyLarge',
|
|
259
|
+
# 'para[class=xLargeText,parent!=cell]': 'bodyXLarge',
|
|
260
|
+
# 'para[class=indentation,parent!=cell]': 'bodyIndentation',
|
|
261
|
+
# 'para[class=xSmallText,parent=cell]': 'cellXSmall',
|
|
262
|
+
# 'para[class=smallText,parent=cell]': 'cellSmall',
|
|
263
|
+
# 'para[class=largeText,parent=cell]': 'cellLarge',
|
|
264
|
+
# 'para[class=xLargeText,parent=cell]': 'cellXLarge',
|
|
265
|
+
# 'para[class=indentation,parent=cell]': 'cellIndentation',
|
|
266
|
+
# })
|
|
267
|
+
|
|
268
|
+
csvOptions = None
|
|
269
|
+
|
|
270
|
+
if output_format == "csv":
|
|
271
|
+
csvOptions = CsvOptions(fieldSeparator=pod_template.csv_field_delimiter,
|
|
272
|
+
textDelimiter=pod_template.csv_string_delimiter)
|
|
273
|
+
renderer = Renderer(
|
|
274
|
+
StringIO(document_template.data),
|
|
275
|
+
generation_context,
|
|
276
|
+
temp_filename,
|
|
277
|
+
pythonWithUnoPath=config.get_uno_path(),
|
|
278
|
+
ooServer=config.get_oo_server(),
|
|
279
|
+
ooPort=config.get_oo_port_list(),
|
|
280
|
+
raiseOnError=raiseOnError,
|
|
281
|
+
imageResolver=api.portal.get(),
|
|
282
|
+
forceOoCall=True,
|
|
283
|
+
html=True,
|
|
284
|
+
optimalColumnWidths=optimalColumnWidths,
|
|
285
|
+
distributeColumns=distributeColumns,
|
|
286
|
+
stylesMapping=stylesMapping,
|
|
287
|
+
stream=config.get_use_stream(),
|
|
288
|
+
csvOptions=csvOptions,
|
|
289
|
+
# deleteTempFolder=False,
|
|
290
|
+
**kwargs
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
# it is only now that we can initialize helper view's appy pod renderer
|
|
294
|
+
all_helper_views = self.get_views_for_appy_renderer(generation_context, helper_view)
|
|
295
|
+
for view in all_helper_views:
|
|
296
|
+
view._set_appy_renderer(renderer)
|
|
297
|
+
|
|
298
|
+
renderer.run()
|
|
299
|
+
|
|
300
|
+
# return also generation_context to test ist content in tests
|
|
301
|
+
return temp_filename, generation_context
|
|
302
|
+
|
|
303
|
+
def _get_context_variables(self, pod_template):
|
|
304
|
+
if base_hasattr(pod_template, 'get_context_variables'):
|
|
305
|
+
return pod_template.get_context_variables()
|
|
306
|
+
return {}
|
|
307
|
+
|
|
308
|
+
def _get_generation_context(self, helper_view, pod_template):
|
|
309
|
+
"""
|
|
310
|
+
Return the generation context for the current document.
|
|
311
|
+
"""
|
|
312
|
+
generation_context = self.get_base_generation_context(helper_view, pod_template)
|
|
313
|
+
utils.update_dict_with_validation(generation_context,
|
|
314
|
+
{'context': getattr(helper_view, 'context', None),
|
|
315
|
+
'portal': api.portal.get(),
|
|
316
|
+
'view': helper_view},
|
|
317
|
+
_("Error when merging helper_view in generation context"))
|
|
318
|
+
utils.update_dict_with_validation(generation_context, self._get_context_variables(pod_template),
|
|
319
|
+
_("Error when merging context_variables in generation context"))
|
|
320
|
+
return generation_context
|
|
321
|
+
|
|
322
|
+
def get_base_generation_context(self, helper_view, pod_template):
|
|
323
|
+
"""
|
|
324
|
+
Override this method to provide your own generation context.
|
|
325
|
+
"""
|
|
326
|
+
return {}
|
|
327
|
+
|
|
328
|
+
def get_generation_context_helper(self):
|
|
329
|
+
"""
|
|
330
|
+
Return the default helper view used for document generation.
|
|
331
|
+
"""
|
|
332
|
+
helper_view = getMultiAdapter((self.context, self.request), name='document_generation_helper_view')
|
|
333
|
+
helper_view.pod_template = self.pod_template
|
|
334
|
+
helper_view.output_format = self.output_format
|
|
335
|
+
return helper_view
|
|
336
|
+
|
|
337
|
+
def get_views_for_appy_renderer(self, generation_context, helper_view):
|
|
338
|
+
views = []
|
|
339
|
+
if 'view' in generation_context:
|
|
340
|
+
# helper_view has maybe changed in generation context getter
|
|
341
|
+
views.append(generation_context['view'])
|
|
342
|
+
else:
|
|
343
|
+
views.append(helper_view)
|
|
344
|
+
|
|
345
|
+
return views
|
|
346
|
+
|
|
347
|
+
def _set_header_response(self, filename):
|
|
348
|
+
"""
|
|
349
|
+
Tell the browser that the resulting page contains ODT.
|
|
350
|
+
"""
|
|
351
|
+
response = self.request.RESPONSE
|
|
352
|
+
mimetype = mimetypes.guess_type(filename)[0]
|
|
353
|
+
response.setHeader('Content-type', mimetype)
|
|
354
|
+
response.setHeader(
|
|
355
|
+
'Content-disposition',
|
|
356
|
+
u'inline;filename="{}"'.format(filename).encode('utf-8')
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
def _check_cyclic_merges(self, pod_template):
|
|
360
|
+
"""
|
|
361
|
+
Check if the template 'pod_template' has subtemplates referring to each
|
|
362
|
+
other in a cyclic way.
|
|
363
|
+
"""
|
|
364
|
+
|
|
365
|
+
def traverse_check(pod_template, path):
|
|
366
|
+
|
|
367
|
+
if pod_template in path:
|
|
368
|
+
path.append(pod_template)
|
|
369
|
+
start_cycle = path.index(pod_template)
|
|
370
|
+
start_msg = ' --> '.join(
|
|
371
|
+
['"{}" {}'.format(t.Title(), '/'.join(t.getPhysicalPath())) for t in path[:start_cycle]]
|
|
372
|
+
)
|
|
373
|
+
cycle_msg = ' <--> '.join(
|
|
374
|
+
['"{}" {}'.format(t.Title(), '/'.join(t.getPhysicalPath())) for t in path[start_cycle:]]
|
|
375
|
+
)
|
|
376
|
+
msg = '{} -> CYCLE:\n{}'.format(start_msg, cycle_msg)
|
|
377
|
+
raise CyclicMergeTemplatesException(msg)
|
|
378
|
+
|
|
379
|
+
new_path = list(path)
|
|
380
|
+
new_path.append(pod_template)
|
|
381
|
+
|
|
382
|
+
sub_templates = pod_template.get_templates_to_merge()
|
|
383
|
+
|
|
384
|
+
for name, (sub_template, do_rendering) in sub_templates.iteritems():
|
|
385
|
+
traverse_check(sub_template, new_path)
|
|
386
|
+
|
|
387
|
+
traverse_check(pod_template, [])
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
class PersistentDocumentGenerationView(DocumentGenerationView):
|
|
391
|
+
"""
|
|
392
|
+
Persistent document generation view.
|
|
393
|
+
"""
|
|
394
|
+
|
|
395
|
+
def __call__(self, template_uid='', output_format='', generated_doc_title=''):
|
|
396
|
+
self.generated_doc_title = generated_doc_title
|
|
397
|
+
self.pod_template, self.output_format = self._get_base_args(template_uid, output_format)
|
|
398
|
+
persisted_doc = self.generate_persistent_doc(self.pod_template, self.output_format)
|
|
399
|
+
self.redirects(persisted_doc)
|
|
400
|
+
|
|
401
|
+
def add_mailing_infos(self, doc, gen_context):
|
|
402
|
+
""" store mailing informations on generated doc """
|
|
403
|
+
annot = IAnnotations(doc)
|
|
404
|
+
if 'mailed_data' in gen_context or 'mailing_list' in gen_context:
|
|
405
|
+
annot['documentgenerator'] = {'need_mailing': False, 'template_uid': self.pod_template.UID()}
|
|
406
|
+
else:
|
|
407
|
+
annot['documentgenerator'] = {'need_mailing': True, 'template_uid': self.pod_template.UID(),
|
|
408
|
+
'output_format': self.output_format, 'context_uid': self.context.UID()}
|
|
409
|
+
|
|
410
|
+
def _get_title(self, doc_name, gen_context):
|
|
411
|
+
splitted_name = doc_name.split('.')
|
|
412
|
+
title = self.generated_doc_title or self.pod_template.title
|
|
413
|
+
extension = splitted_name[-1]
|
|
414
|
+
return safe_unicode(title), extension
|
|
415
|
+
|
|
416
|
+
def generate_persistent_doc(self, pod_template, output_format):
|
|
417
|
+
"""
|
|
418
|
+
Generate a document of format 'output_format' from the template
|
|
419
|
+
'pod_template' and persist it by creating a File containing the
|
|
420
|
+
generated document on the current context.
|
|
421
|
+
"""
|
|
422
|
+
|
|
423
|
+
doc, doc_name, gen_context = self._generate_doc(pod_template, output_format)
|
|
424
|
+
|
|
425
|
+
title, extension = self._get_title(doc_name, gen_context)
|
|
426
|
+
|
|
427
|
+
factory = queryAdapter(self.context, IDocumentFactory)
|
|
428
|
+
|
|
429
|
+
# Bypass any File creation permission of the user. If the user isnt
|
|
430
|
+
# supposed to save generated document on the site, then its the permission
|
|
431
|
+
# to call the generation view that should be changed.
|
|
432
|
+
with api.env.adopt_roles(['Manager']):
|
|
433
|
+
persisted_doc = factory.create(doc_file=doc, title=title, extension=extension)
|
|
434
|
+
|
|
435
|
+
# store informations on persisted doc
|
|
436
|
+
self.add_mailing_infos(persisted_doc, gen_context)
|
|
437
|
+
|
|
438
|
+
return persisted_doc
|
|
439
|
+
|
|
440
|
+
def redirects(self, persisted_doc):
|
|
441
|
+
"""
|
|
442
|
+
Redirects to the created document.
|
|
443
|
+
"""
|
|
444
|
+
if HAS_PLONE_5_AND_MORE:
|
|
445
|
+
filename = persisted_doc.file.filename
|
|
446
|
+
else:
|
|
447
|
+
filename = persisted_doc.getFile().filename
|
|
448
|
+
self._set_header_response(filename)
|
|
449
|
+
response = self.request.response
|
|
450
|
+
return response.redirect(persisted_doc.absolute_url() + '/external_edit')
|
|
451
|
+
|
|
452
|
+
def mailing_related_generation_context(self, helper_view, gen_context):
|
|
453
|
+
"""
|
|
454
|
+
Add mailing related information in generation context
|
|
455
|
+
"""
|
|
456
|
+
# We add mailed_data if we have only one element in mailing list
|
|
457
|
+
mailing_list = helper_view.mailing_list(gen_context)
|
|
458
|
+
if len(mailing_list) == 0:
|
|
459
|
+
utils.update_dict_with_validation(gen_context, {'mailed_data': None},
|
|
460
|
+
_("Error when merging mailed_data in generation context"))
|
|
461
|
+
elif len(mailing_list) == 1:
|
|
462
|
+
utils.update_dict_with_validation(gen_context, {'mailed_data': mailing_list[0]},
|
|
463
|
+
_("Error when merging mailed_data in generation context"))
|
|
464
|
+
|
|
465
|
+
def _get_generation_context(self, helper_view, pod_template):
|
|
466
|
+
""" """
|
|
467
|
+
gen_context = super(PersistentDocumentGenerationView, self)._get_generation_context(helper_view, pod_template)
|
|
468
|
+
self.mailing_related_generation_context(helper_view, gen_context)
|
|
469
|
+
return gen_context
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
class MailingLoopPersistentDocumentGenerationView(PersistentDocumentGenerationView):
|
|
473
|
+
"""
|
|
474
|
+
Mailing persistent document generation view.
|
|
475
|
+
This view use a MailingLoopTemplate to loop on a document when replacing some variables in.
|
|
476
|
+
"""
|
|
477
|
+
|
|
478
|
+
def __call__(self, document_uid='', document_url_path='', generated_doc_title=''):
|
|
479
|
+
self.generated_doc_title = generated_doc_title
|
|
480
|
+
document_uid = document_uid or self.request.get('document_uid', '')
|
|
481
|
+
document_url_path = document_url_path or self.request.get('document_url_path', '')
|
|
482
|
+
if not document_uid and not document_url_path:
|
|
483
|
+
raise Exception("No 'document_uid' or 'url_path' found to generate this document")
|
|
484
|
+
elif document_url_path:
|
|
485
|
+
site = api.portal.get()
|
|
486
|
+
self.document = site.restrictedTraverse(document_url_path)
|
|
487
|
+
else:
|
|
488
|
+
self.document = uuidToObject(document_uid)
|
|
489
|
+
if not self.document:
|
|
490
|
+
raise Exception("Cannot find document with UID '{0}'".format(document_uid))
|
|
491
|
+
self.pod_template, self.output_format = self._get_base_args('', '')
|
|
492
|
+
persisted_doc = self.generate_persistent_doc(self.pod_template, self.output_format)
|
|
493
|
+
self.redirects(persisted_doc)
|
|
494
|
+
|
|
495
|
+
def add_mailing_infos(self, doc, gen_context):
|
|
496
|
+
""" store mailing informations on generated doc """
|
|
497
|
+
annot = IAnnotations(doc)
|
|
498
|
+
annot['documentgenerator'] = {"need_mailing": False, "template_uid": self.pod_template.UID(), "mailed": True}
|
|
499
|
+
|
|
500
|
+
def _get_base_args(self, template_uid, output_format):
|
|
501
|
+
annot = IAnnotations(self.document).get('documentgenerator', '')
|
|
502
|
+
if not annot or 'template_uid' not in annot:
|
|
503
|
+
raise Exception("Cannot find 'template_uid' on document '{0}'".format(self.document.absolute_url()))
|
|
504
|
+
self.orig_template = self.get_pod_template(annot['template_uid'])
|
|
505
|
+
if (not base_hasattr(self.orig_template, 'mailing_loop_template')
|
|
506
|
+
or not self.orig_template.mailing_loop_template):
|
|
507
|
+
raise Exception("Cannot find 'mailing_loop_template' on template '{0}'".format(
|
|
508
|
+
self.orig_template.absolute_url()))
|
|
509
|
+
loop_template = self.get_pod_template(self.orig_template.mailing_loop_template)
|
|
510
|
+
|
|
511
|
+
if 'output_format' not in annot:
|
|
512
|
+
raise Exception("No 'output_format' found to generate this document")
|
|
513
|
+
return loop_template, annot['output_format']
|
|
514
|
+
|
|
515
|
+
def mailing_related_generation_context(self, helper_view, gen_context):
|
|
516
|
+
"""
|
|
517
|
+
Add mailing related information in generation context
|
|
518
|
+
"""
|
|
519
|
+
# We do nothing here because we have to call mailing_list after original template context variable inclusion
|
|
520
|
+
|
|
521
|
+
def _get_generation_context(self, helper_view, pod_template):
|
|
522
|
+
""" """
|
|
523
|
+
gen_context = super(MailingLoopPersistentDocumentGenerationView, self). \
|
|
524
|
+
_get_generation_context(helper_view, pod_template)
|
|
525
|
+
# add variable context from original template
|
|
526
|
+
utils.update_dict_with_validation(gen_context, self._get_context_variables(self.orig_template),
|
|
527
|
+
_("Error when merging context_variables in generation context"))
|
|
528
|
+
# add mailing list in generation context
|
|
529
|
+
dic = {'mailing_list': helper_view.mailing_list(gen_context), 'mailed_doc': self.document}
|
|
530
|
+
utils.update_dict_with_validation(gen_context, dic,
|
|
531
|
+
_("Error when merging mailing_list in generation context"))
|
|
532
|
+
return gen_context
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from collective.documentgenerator.browser.views import ViewConfigurablePodTemplate
|
|
4
|
+
from collective.documentviewer.views import DXDocumentViewerView
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class DGDXDocumentViewerView(ViewConfigurablePodTemplate, DXDocumentViewerView):
|
|
8
|
+
"""The documentviewer view from collective.documentviewer overrides the default DX
|
|
9
|
+
view, so override it again so our own override is taken into account."""
|