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,396 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from appy.bin.odfclean import Cleaner
|
|
3
|
+
from appy.pod.lo_pool import LoPool
|
|
4
|
+
from appy.pod.renderer import Renderer
|
|
5
|
+
from collective.documentgenerator import _
|
|
6
|
+
from collective.documentgenerator import BLDT_DIR
|
|
7
|
+
from collective.documentgenerator import config
|
|
8
|
+
from collective.documentgenerator.config import DEFAULT_OO_PORT
|
|
9
|
+
from collective.documentgenerator.config import get_oo_port_list
|
|
10
|
+
from collective.documentgenerator.config import get_oo_server
|
|
11
|
+
from collective.documentgenerator.config import get_uno_path
|
|
12
|
+
from imio.helpers.content import uuidToObject
|
|
13
|
+
from imio.helpers.security import fplog
|
|
14
|
+
from imio.pyutils.system import runCommand
|
|
15
|
+
from plone import api
|
|
16
|
+
from plone.dexterity.utils import createContentInContainer
|
|
17
|
+
from plone.namedfile.file import NamedBlobFile
|
|
18
|
+
from Products.CMFCore.utils import getToolByName
|
|
19
|
+
from Products.CMFPlone.utils import safe_unicode
|
|
20
|
+
from zope import i18n
|
|
21
|
+
from zope.annotation import IAnnotations
|
|
22
|
+
from zope.component import getMultiAdapter
|
|
23
|
+
from zope.component.hooks import getSite
|
|
24
|
+
from zope.component.hooks import setSite
|
|
25
|
+
from zope.interface import Interface
|
|
26
|
+
from zope.interface import Invalid
|
|
27
|
+
from zope.lifecycleevent import Attributes
|
|
28
|
+
from zope.lifecycleevent import modified
|
|
29
|
+
|
|
30
|
+
import hashlib
|
|
31
|
+
import logging
|
|
32
|
+
import os
|
|
33
|
+
import re
|
|
34
|
+
import tempfile
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
logger = logging.getLogger('collective.documentgenerator')
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def translate(msgid, domain='collective.documentgenerator'):
|
|
41
|
+
portal = api.portal.get()
|
|
42
|
+
translation = i18n.translate(
|
|
43
|
+
msgid,
|
|
44
|
+
domain=domain,
|
|
45
|
+
context=portal.REQUEST
|
|
46
|
+
)
|
|
47
|
+
return translation
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def compute_md5(data):
|
|
51
|
+
md5 = hashlib.md5(data).hexdigest()
|
|
52
|
+
return md5
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def update_templates(templates, profile='', force=False):
|
|
56
|
+
"""
|
|
57
|
+
function to manage templates update.
|
|
58
|
+
# see http://trac.imio.be/trac/ticket/9383 for full implementation
|
|
59
|
+
:param list templates: list of tuples containing ('plone-template-path', 'os-template-path')
|
|
60
|
+
:param str profile: profile path stored on template (or various identification)
|
|
61
|
+
:param bool force: force overrides of templates
|
|
62
|
+
"""
|
|
63
|
+
# Don't use profile now !
|
|
64
|
+
portal = api.portal.getSite()
|
|
65
|
+
ret = []
|
|
66
|
+
for (ppath, ospath) in templates:
|
|
67
|
+
ppath = ppath.strip('/ ')
|
|
68
|
+
obj = portal.unrestrictedTraverse(ppath, default=None)
|
|
69
|
+
if not obj:
|
|
70
|
+
logger.warn("The plone template '%s' was not found" % ppath)
|
|
71
|
+
ret.append((ppath, ospath, 'plone path error'))
|
|
72
|
+
continue
|
|
73
|
+
if not obj.odt_file:
|
|
74
|
+
logger.warn("The plone template '%s' doesn't have odt file" % ppath)
|
|
75
|
+
ret.append((ppath, ospath, 'no odt file'))
|
|
76
|
+
continue
|
|
77
|
+
if not os.path.exists(ospath):
|
|
78
|
+
logger.warn("The template file '%s' doesn't exist" % ospath)
|
|
79
|
+
ret.append((ppath, ospath, 'os path error'))
|
|
80
|
+
continue
|
|
81
|
+
with open(ospath, 'rb') as f:
|
|
82
|
+
data = f.read()
|
|
83
|
+
new_md5 = compute_md5(data)
|
|
84
|
+
if obj.initial_md5 == new_md5:
|
|
85
|
+
ret.append((ppath, ospath, 'unchanged'))
|
|
86
|
+
continue
|
|
87
|
+
elif obj.has_been_modified() and not force:
|
|
88
|
+
ret.append((ppath, ospath, 'kept'))
|
|
89
|
+
continue
|
|
90
|
+
obj.initial_md5 = new_md5
|
|
91
|
+
obj.style_modification_md5 = new_md5
|
|
92
|
+
obj.odt_file.data = data
|
|
93
|
+
modified(obj, Attributes(Interface, 'odt_file'))
|
|
94
|
+
ret.append((ppath, ospath, 'replaced'))
|
|
95
|
+
return ret
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def update_dict_with_validation(original_dict, update_dict, error_message=_("Dict update collision on key")):
|
|
99
|
+
for key in update_dict:
|
|
100
|
+
if key in original_dict:
|
|
101
|
+
raise Invalid(_("${error_message} for key = '${key}'",
|
|
102
|
+
mapping={'error_message': error_message, 'key': key}))
|
|
103
|
+
|
|
104
|
+
original_dict[key] = update_dict[key]
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def ulocalized_time(date, long_format=None, time_only=None, custom_format=None,
|
|
108
|
+
domain='plonelocales', target_language=None, context=None,
|
|
109
|
+
request=None, month_lc=True, day_lc=True):
|
|
110
|
+
"""
|
|
111
|
+
Return for a datetime the string value with week and mont translated.
|
|
112
|
+
Take into account %a, %A, %b, %B
|
|
113
|
+
"""
|
|
114
|
+
if not custom_format:
|
|
115
|
+
# use toLocalizedTime
|
|
116
|
+
plone = getMultiAdapter((context, request), name=u'plone')
|
|
117
|
+
formatted_date = plone.toLocalizedTime(date, long_format, time_only)
|
|
118
|
+
else:
|
|
119
|
+
from Products.CMFPlone.i18nl10n import monthname_msgid
|
|
120
|
+
from Products.CMFPlone.i18nl10n import monthname_msgid_abbr
|
|
121
|
+
from Products.CMFPlone.i18nl10n import weekdayname_msgid
|
|
122
|
+
from Products.CMFPlone.i18nl10n import weekdayname_msgid_abbr
|
|
123
|
+
if request is None:
|
|
124
|
+
portal = api.portal.get()
|
|
125
|
+
request = portal.REQUEST
|
|
126
|
+
# first replace parts to translate
|
|
127
|
+
custom_format = custom_format.replace('%%', '_p_c_')
|
|
128
|
+
|
|
129
|
+
conf = {
|
|
130
|
+
'a': {'fct': weekdayname_msgid_abbr, 'fmt': '%w', 'low': day_lc},
|
|
131
|
+
'A': {'fct': weekdayname_msgid, 'fmt': '%w', 'low': day_lc},
|
|
132
|
+
'b': {'fct': monthname_msgid_abbr, 'fmt': '%m', 'low': month_lc},
|
|
133
|
+
'B': {'fct': monthname_msgid, 'fmt': '%m', 'low': month_lc},
|
|
134
|
+
}
|
|
135
|
+
matches = re.findall(r'%([aAbB])', custom_format)
|
|
136
|
+
for match in sorted(set(matches)):
|
|
137
|
+
# function( int(date.strftime(format) )
|
|
138
|
+
msgid = conf[match]['fct'](int(date.strftime(conf[match]['fmt'])))
|
|
139
|
+
repl = i18n.translate(msgid, domain, context=request, target_language=target_language)
|
|
140
|
+
if conf[match]['low']:
|
|
141
|
+
repl = repl.lower()
|
|
142
|
+
custom_format = re.sub('%{}'.format(match), repl, custom_format)
|
|
143
|
+
|
|
144
|
+
# then format date
|
|
145
|
+
custom_format = custom_format.replace('_p_c_', '%%')
|
|
146
|
+
formatted_date = date.strftime(custom_format.encode('utf8'))
|
|
147
|
+
return safe_unicode(formatted_date)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def remove_tmp_file(filename):
|
|
151
|
+
"""Do not break if unable to remove temporary file, but log error if any."""
|
|
152
|
+
try:
|
|
153
|
+
os.remove(filename)
|
|
154
|
+
except OSError:
|
|
155
|
+
logger.warn("Could not remove temporary file at {0}".format(filename))
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def update_oo_config():
|
|
159
|
+
""" Update config following buildout var """
|
|
160
|
+
key_template = 'collective.documentgenerator.browser.controlpanel.IDocumentGeneratorControlPanelSchema.{}'
|
|
161
|
+
var = {'oo_server': 'OO_SERVER', 'oo_port_list': 'OO_PORT', 'uno_path': 'PYTHON_UNO'}
|
|
162
|
+
for key in var.keys():
|
|
163
|
+
full_key = key_template.format(key)
|
|
164
|
+
configured_oo_option = api.portal.get_registry_record(full_key)
|
|
165
|
+
env_value = os.getenv(var.get(key, 'NO_ONE'), None)
|
|
166
|
+
if env_value:
|
|
167
|
+
new_oo_option = type(configured_oo_option)(os.getenv(var.get(key, 'NO_ONE'), ''))
|
|
168
|
+
if new_oo_option and new_oo_option != configured_oo_option:
|
|
169
|
+
api.portal.set_registry_record(full_key, new_oo_option)
|
|
170
|
+
logger.info("LibreOffice configuration updated for " + getSite().getId())
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def update_oo_config_after_bigbang(event):
|
|
174
|
+
setSite(event.object)
|
|
175
|
+
try:
|
|
176
|
+
update_oo_config()
|
|
177
|
+
except Exception:
|
|
178
|
+
logger.error("Update LibreOffice configuration failed", exc_info=1)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def get_site_root_relative_path(obj):
|
|
182
|
+
return "/" + '/'.join(
|
|
183
|
+
getToolByName(obj, 'portal_url').getRelativeContentPath(obj)
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def temporary_file_name(suffix=''):
|
|
188
|
+
tmp_dir = os.getenv('CUSTOM_TMP', None)
|
|
189
|
+
if tmp_dir and not os.path.exists(tmp_dir):
|
|
190
|
+
os.mkdir(tmp_dir)
|
|
191
|
+
return tempfile.mktemp(suffix=suffix, dir=tmp_dir)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def create_temporary_file(initial_file=None, base_name=''):
|
|
195
|
+
tmp_filename = temporary_file_name(suffix=base_name)
|
|
196
|
+
# create the file in any case
|
|
197
|
+
with open(tmp_filename, 'w+') as tmp_file:
|
|
198
|
+
if initial_file:
|
|
199
|
+
tmp_file.write(initial_file.data)
|
|
200
|
+
return tmp_file
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def clean_notes(pod_template):
|
|
204
|
+
""" Use appy.pod Cleaner to clean notes (comments). """
|
|
205
|
+
cleaned = 0
|
|
206
|
+
odt_file = pod_template.odt_file
|
|
207
|
+
if odt_file:
|
|
208
|
+
# write file to /tmp to be able to use appy.pod Cleaner
|
|
209
|
+
tmp_file = create_temporary_file(odt_file, '-to-clean.odt')
|
|
210
|
+
cleaner = Cleaner(path=tmp_file.name, verbose=1)
|
|
211
|
+
cleaned = cleaner.run()
|
|
212
|
+
if cleaned:
|
|
213
|
+
manually_modified = pod_template.has_been_modified()
|
|
214
|
+
with open(tmp_file.name, 'rb') as res_file:
|
|
215
|
+
# update template
|
|
216
|
+
result = NamedBlobFile(
|
|
217
|
+
data=res_file.read(),
|
|
218
|
+
contentType=odt_file.contentType,
|
|
219
|
+
filename=pod_template.odt_file.filename)
|
|
220
|
+
pod_template.odt_file = result
|
|
221
|
+
if not manually_modified:
|
|
222
|
+
pod_template.style_modification_md5 = pod_template.current_md5
|
|
223
|
+
extras = 'pod_template={0} cleaned_parts={1}'.format(
|
|
224
|
+
repr(pod_template), cleaned)
|
|
225
|
+
fplog('clean_notes', extras=extras)
|
|
226
|
+
remove_tmp_file(tmp_file.name)
|
|
227
|
+
|
|
228
|
+
return bool(cleaned)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def convert_odt(afile, output_name, fmt='pdf', **kwargs):
|
|
232
|
+
"""
|
|
233
|
+
Convert an odt file to another format using appy.pod.
|
|
234
|
+
|
|
235
|
+
:param afile: file field content like NamedBlobFile
|
|
236
|
+
:param output_name: output name
|
|
237
|
+
:param fmt: output format, default to 'pdf'
|
|
238
|
+
:param kwargs: other parameters passed to Renderer, i.e pdfOptions='ExportNotes=True;SelectPdfVersion=1'
|
|
239
|
+
"""
|
|
240
|
+
lo_pool = LoPool.get(
|
|
241
|
+
python=config.get_uno_path(),
|
|
242
|
+
server=config.get_oo_server(),
|
|
243
|
+
port=config.get_oo_port_list(),
|
|
244
|
+
)
|
|
245
|
+
if not lo_pool:
|
|
246
|
+
raise Exception("Could not find LibreOffice, check your configuration")
|
|
247
|
+
|
|
248
|
+
temp_file = create_temporary_file(afile, '.odt')
|
|
249
|
+
converted_filename = None
|
|
250
|
+
try:
|
|
251
|
+
renderer = Renderer(
|
|
252
|
+
temp_file.name,
|
|
253
|
+
afile,
|
|
254
|
+
temporary_file_name(suffix=".{extension}".format(extension=fmt)),
|
|
255
|
+
**kwargs
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
lo_pool(renderer, temp_file.name, fmt)
|
|
259
|
+
converted_filename = temp_file.name.replace('.odt', '.{}'.format(fmt))
|
|
260
|
+
if not os.path.exists(converted_filename):
|
|
261
|
+
api.portal.show_message(
|
|
262
|
+
message=_(u"Conversion failed, no converted file '{}'".format(safe_unicode(output_name))),
|
|
263
|
+
request=getSite().REQUEST,
|
|
264
|
+
type="error",
|
|
265
|
+
)
|
|
266
|
+
raise Invalid(u"Conversion failed, no converted file '{}'".format(safe_unicode(output_name)))
|
|
267
|
+
with open(converted_filename, 'rb') as f:
|
|
268
|
+
converted_file = f.read()
|
|
269
|
+
finally:
|
|
270
|
+
remove_tmp_file(temp_file.name)
|
|
271
|
+
if converted_filename:
|
|
272
|
+
remove_tmp_file(converted_filename)
|
|
273
|
+
|
|
274
|
+
return output_name, converted_file
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def convert_file(afile, output_name, fmt="pdf", renderer=False):
|
|
278
|
+
"""
|
|
279
|
+
Convert a file to another libreoffice readable format using appy.pod
|
|
280
|
+
|
|
281
|
+
:param afile: file field content like NamedBlobFile
|
|
282
|
+
:param output_name: output name
|
|
283
|
+
:param fmt: output format, default to "pdf"
|
|
284
|
+
:param renderer: whether to use appy.pod Renderer or converter script. Default to False.
|
|
285
|
+
"""
|
|
286
|
+
if renderer:
|
|
287
|
+
if not afile.filename.endswith('.odt'):
|
|
288
|
+
message = _(u"Conversion with renderer only works from odt files.")
|
|
289
|
+
raise Invalid(message)
|
|
290
|
+
return convert_odt(afile, output_name, fmt=fmt)
|
|
291
|
+
from appy.pod import converter
|
|
292
|
+
converter_path = converter.__file__.endswith(".pyc") and converter.__file__[:-1] or converter.__file__
|
|
293
|
+
file_ext = afile.filename.split('.')[-1].lower()
|
|
294
|
+
temp_file = create_temporary_file(afile, base_name=".{}".format(file_ext))
|
|
295
|
+
converted_filename = temp_file.name.replace(".{}".format(file_ext), ".{}".format(fmt))
|
|
296
|
+
converted_file = ""
|
|
297
|
+
try:
|
|
298
|
+
ports = get_oo_port_list()
|
|
299
|
+
port = ports[0] if ports else DEFAULT_OO_PORT
|
|
300
|
+
command = "{python_uno_path} {converter_path} {temp_file} {fmt} -p {port} -e {server}".format(
|
|
301
|
+
python_uno_path=get_uno_path(), converter_path=converter_path, temp_file=temp_file.name, fmt=fmt,
|
|
302
|
+
port=port, server=get_oo_server())
|
|
303
|
+
out, err, code = runCommand(command)
|
|
304
|
+
# This command has no output on success
|
|
305
|
+
if code != 0 or err or not os.path.exists(converted_filename):
|
|
306
|
+
message = _(u"Conversion failed, no converted file '{}'".format(safe_unicode(output_name)))
|
|
307
|
+
raise Invalid(message)
|
|
308
|
+
with open(converted_filename, 'rb') as f:
|
|
309
|
+
converted_file = f.read()
|
|
310
|
+
except Exception as e:
|
|
311
|
+
api.portal.show_message(message=str(e), request=getSite().REQUEST, type="error")
|
|
312
|
+
finally:
|
|
313
|
+
remove_tmp_file(temp_file.name)
|
|
314
|
+
if os.path.exists(converted_filename):
|
|
315
|
+
remove_tmp_file(converted_filename)
|
|
316
|
+
return output_name, converted_file
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def convert_and_save_file(afile, container, portal_type, output_name, fmt='pdf', from_uid=None, attributes=None, renderer=False):
|
|
320
|
+
"""
|
|
321
|
+
Convert a file to another libreoffice readable format using appy.pod and save it in a NamedBlobFile.
|
|
322
|
+
|
|
323
|
+
:param afile: file field content like NamedBlobFile
|
|
324
|
+
:param container: container object to create new file
|
|
325
|
+
:param portal_type: portal type
|
|
326
|
+
:param output_name: output name
|
|
327
|
+
:param fmt: output format, default to 'pdf'
|
|
328
|
+
:param from_uid: uid from original file object
|
|
329
|
+
:param attributes: dict of other attributes to set on created content
|
|
330
|
+
:param renderer: whether to use appy.pod Renderer or converter script. Default to False.
|
|
331
|
+
"""
|
|
332
|
+
converted_filename, converted_file = convert_file(afile, output_name, fmt=fmt, renderer=renderer)
|
|
333
|
+
file_object = NamedBlobFile(converted_file, filename=safe_unicode(converted_filename))
|
|
334
|
+
if attributes is None:
|
|
335
|
+
attributes = {}
|
|
336
|
+
attributes["conv_from_uid"] = from_uid
|
|
337
|
+
new_file = createContentInContainer(
|
|
338
|
+
container,
|
|
339
|
+
portal_type,
|
|
340
|
+
title=converted_filename,
|
|
341
|
+
file=file_object,
|
|
342
|
+
**attributes)
|
|
343
|
+
if from_uid:
|
|
344
|
+
annot = IAnnotations(new_file)
|
|
345
|
+
annot["documentgenerator"] = {"conv_from_uid": from_uid}
|
|
346
|
+
return new_file
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
@api.env.mutually_exclusive_parameters("document", "document_uid")
|
|
350
|
+
def need_mailing_value(document=None, document_uid=None):
|
|
351
|
+
if not document:
|
|
352
|
+
document = uuidToObject(document_uid, unrestricted=True)
|
|
353
|
+
annot = IAnnotations(document)
|
|
354
|
+
if "documentgenerator" in annot and annot["documentgenerator"].get("need_mailing", False):
|
|
355
|
+
return True
|
|
356
|
+
return False
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def odfsplit(content):
|
|
360
|
+
"""Splits an ODT document into a series of sub-documents. The split is based on page breaks.
|
|
361
|
+
|
|
362
|
+
:param content: The binary content of the ODT file to be split.
|
|
363
|
+
:return: A tuple containing the exit code, a generator yielding the binary content of each subfile and
|
|
364
|
+
the number of files
|
|
365
|
+
"""
|
|
366
|
+
|
|
367
|
+
def get_subfiles(temp_file, nb_files):
|
|
368
|
+
if nb_files == 1:
|
|
369
|
+
with open(temp_file, "rb") as f:
|
|
370
|
+
yield f.read()
|
|
371
|
+
remove_tmp_file(temp_file)
|
|
372
|
+
return
|
|
373
|
+
for i in range(1, nb_files + 1):
|
|
374
|
+
subfile = temp_file.replace(".odt", ".{}.odt".format(i))
|
|
375
|
+
with open(subfile, "rb") as sf:
|
|
376
|
+
yield sf.read()
|
|
377
|
+
remove_tmp_file(subfile)
|
|
378
|
+
remove_tmp_file(temp_file)
|
|
379
|
+
|
|
380
|
+
temp_file = temporary_file_name(suffix=".odt")
|
|
381
|
+
with open(temp_file, "wb") as f:
|
|
382
|
+
f.write(content)
|
|
383
|
+
command = "{pwd}/bin/odfsplit {temp_file}".format(temp_file=temp_file, pwd=BLDT_DIR)
|
|
384
|
+
out, err, code = runCommand(command)
|
|
385
|
+
nb_files = 0
|
|
386
|
+
if out and code == 0:
|
|
387
|
+
part0 = out[0].split(" ")[0] # Ex: "2 files were generated."
|
|
388
|
+
if part0.isdigit():
|
|
389
|
+
nb_files = int(part0)
|
|
390
|
+
value = get_subfiles(temp_file, nb_files)
|
|
391
|
+
else:
|
|
392
|
+
nb_files = 1
|
|
393
|
+
value = get_subfiles(temp_file, 1)
|
|
394
|
+
else:
|
|
395
|
+
value = "".join(err)
|
|
396
|
+
return code, value, nb_files
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
from collective.eeafaceted.batchactions.browser.viewlets import BatchActionsViewlet
|
|
5
|
+
except ImportError:
|
|
6
|
+
from plone.app.layout.viewlets import ViewletBase as BatchActionsViewlet
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class DGBatchActionsViewlet(BatchActionsViewlet):
|
|
10
|
+
''' '''
|
|
11
|
+
|
|
12
|
+
def available(self):
|
|
13
|
+
"""Global availability of the viewlet."""
|
|
14
|
+
return self.view.__name__ == 'dg-templates-listing'
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<configure
|
|
2
|
+
xmlns="http://namespaces.zope.org/zope"
|
|
3
|
+
xmlns:browser="http://namespaces.zope.org/browser"
|
|
4
|
+
xmlns:plone="http://namespaces.plone.org/plone"
|
|
5
|
+
i18n_domain="collective.documentgenerator">
|
|
6
|
+
|
|
7
|
+
<browser:viewlet
|
|
8
|
+
for="*"
|
|
9
|
+
name="document-generation-link"
|
|
10
|
+
manager="plone.app.layout.viewlets.interfaces.IBelowContentTitle"
|
|
11
|
+
class="collective.documentgenerator.viewlets.generationlinks.DocumentGeneratorLinksViewlet"
|
|
12
|
+
template="generationlinks.pt"
|
|
13
|
+
permission="zope2.View"
|
|
14
|
+
/>
|
|
15
|
+
|
|
16
|
+
</configure>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<div id="doc-generation-view"
|
|
2
|
+
tal:condition="view/available"
|
|
3
|
+
i18n:domain="collective.documentgenerator">
|
|
4
|
+
|
|
5
|
+
<ul class="pod-template"
|
|
6
|
+
tal:define="infos view/get_links_info;
|
|
7
|
+
titles python:infos.keys();">
|
|
8
|
+
<li tal:repeat="title titles" class="template-link">
|
|
9
|
+
<span class="template-link-title" tal:content="title">Template title</span>
|
|
10
|
+
<span class="template-link-after-title"></span>
|
|
11
|
+
<span class="template-link-before-link"
|
|
12
|
+
tal:repeat="link python: infos[title]">
|
|
13
|
+
<a target="_blank"
|
|
14
|
+
class="generation-link"
|
|
15
|
+
tal:attributes="href link/link;
|
|
16
|
+
title string:${link/output_format_title};">
|
|
17
|
+
<img src="#" class="svg-icon"
|
|
18
|
+
tal:define="alt python: '{} {}'.format(link['title'], link['output_format'].upper());"
|
|
19
|
+
tal:attributes="src string:${view/portal_url}/++resource++collective.documentgenerator/${link/output_format}.svg;
|
|
20
|
+
alt alt;
|
|
21
|
+
title link/output_format_title;" />
|
|
22
|
+
</a>
|
|
23
|
+
</span>
|
|
24
|
+
<span class="link-separator"></span>
|
|
25
|
+
</li>
|
|
26
|
+
</ul>
|
|
27
|
+
|
|
28
|
+
</div>
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from collections import OrderedDict
|
|
4
|
+
from collective.documentgenerator.config import POD_FORMATS
|
|
5
|
+
from collective.documentgenerator.interfaces import IGenerablePODTemplates
|
|
6
|
+
from plone.app.layout.viewlets import ViewletBase
|
|
7
|
+
from plone.memoize.view import memoize
|
|
8
|
+
from zope.component import getAdapter
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DocumentGeneratorLinksViewlet(ViewletBase):
|
|
12
|
+
"""This viewlet displays available documents to generate."""
|
|
13
|
+
|
|
14
|
+
def available(self):
|
|
15
|
+
return bool(self.get_generable_templates())
|
|
16
|
+
|
|
17
|
+
@memoize
|
|
18
|
+
def get_generable_templates(self):
|
|
19
|
+
adapter = getAdapter(self.context, IGenerablePODTemplates)
|
|
20
|
+
generable_templates = adapter.get_generable_templates()
|
|
21
|
+
return generable_templates
|
|
22
|
+
|
|
23
|
+
def get_generation_view_name(self, template, output_format):
|
|
24
|
+
return 'document-generation'
|
|
25
|
+
|
|
26
|
+
def get_links_info(self):
|
|
27
|
+
base_url = self.context.absolute_url()
|
|
28
|
+
links = OrderedDict()
|
|
29
|
+
pod_formats = {podf[0]: podf[1] for podf in POD_FORMATS}
|
|
30
|
+
for template in self.get_generable_templates():
|
|
31
|
+
for output_format in template.get_available_formats():
|
|
32
|
+
title = template.Title().strip()
|
|
33
|
+
if title not in links:
|
|
34
|
+
links[title] = []
|
|
35
|
+
description = template.Description()
|
|
36
|
+
uid = template.UID()
|
|
37
|
+
link = '{base_url}/{gen_view_name}?template_uid={uid}&output_format={output_format}'.format(
|
|
38
|
+
base_url=base_url,
|
|
39
|
+
uid=uid,
|
|
40
|
+
output_format=output_format,
|
|
41
|
+
gen_view_name=self.get_generation_view_name(template, output_format)
|
|
42
|
+
)
|
|
43
|
+
infos = {'link': link,
|
|
44
|
+
'title': title,
|
|
45
|
+
'description': description,
|
|
46
|
+
'output_format': output_format,
|
|
47
|
+
'output_format_title': pod_formats[output_format],
|
|
48
|
+
'template_uid': uid,
|
|
49
|
+
'template': template}
|
|
50
|
+
infos.update(self.add_extra_links_info(template, infos))
|
|
51
|
+
links[title].append(infos)
|
|
52
|
+
return links
|
|
53
|
+
|
|
54
|
+
def add_extra_links_info(self, template, infos):
|
|
55
|
+
"""This method is made to be overrided and ease adding extra infos
|
|
56
|
+
to the list of dicts returned by self.get_links_info.
|
|
57
|
+
It needs to returns a dict that will update data stored in infos."""
|
|
58
|
+
return {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import sys, types, os;p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('collective',));importlib = __import__('importlib.util');__import__('importlib.machinery');m = sys.modules.setdefault('collective', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('collective', [os.path.dirname(p)])));m = m or sys.modules.setdefault('collective', types.ModuleType('collective'));mp = (m or []) and m.__dict__.setdefault('__path__',[]);(p not in mp) and mp.append(p)
|