collective.documentgenerator 4.0__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.
Files changed (167) hide show
  1. collective/documentgenerator/DX_renderer.py +51 -0
  2. collective/documentgenerator/__init__.py +11 -0
  3. collective/documentgenerator/adapters.py +30 -0
  4. collective/documentgenerator/browser/__init__.py +0 -0
  5. collective/documentgenerator/browser/actionspanel.py +19 -0
  6. collective/documentgenerator/browser/check_pod_templates.pt +86 -0
  7. collective/documentgenerator/browser/children_pod_template.pt +27 -0
  8. collective/documentgenerator/browser/configure.zcml +217 -0
  9. collective/documentgenerator/browser/controlpanel.py +165 -0
  10. collective/documentgenerator/browser/generation_view.py +613 -0
  11. collective/documentgenerator/browser/overrides.py +9 -0
  12. collective/documentgenerator/browser/resources/css/documentgenerator.css +133 -0
  13. collective/documentgenerator/browser/static/.gitkeep +0 -0
  14. collective/documentgenerator/browser/static/configurable.svg +1232 -0
  15. collective/documentgenerator/browser/static/csv.svg +521 -0
  16. collective/documentgenerator/browser/static/doc.svg +23 -0
  17. collective/documentgenerator/browser/static/docx.svg +20 -0
  18. collective/documentgenerator/browser/static/download_icon.svg +54 -0
  19. collective/documentgenerator/browser/static/libreoffice-blanck.svg +481 -0
  20. collective/documentgenerator/browser/static/mailinglooptemplate.svg +1207 -0
  21. collective/documentgenerator/browser/static/missing_use.svg +42 -0
  22. collective/documentgenerator/browser/static/nok.svg +38 -0
  23. collective/documentgenerator/browser/static/nok_use.svg +38 -0
  24. collective/documentgenerator/browser/static/nok_used.svg +38 -0
  25. collective/documentgenerator/browser/static/odp.svg +1264 -0
  26. collective/documentgenerator/browser/static/ods.svg +1354 -0
  27. collective/documentgenerator/browser/static/odt.svg +1347 -0
  28. collective/documentgenerator/browser/static/ok.svg +38 -0
  29. collective/documentgenerator/browser/static/ok_use.svg +38 -0
  30. collective/documentgenerator/browser/static/ok_used.svg +38 -0
  31. collective/documentgenerator/browser/static/pdf.svg +557 -0
  32. collective/documentgenerator/browser/static/podtemplate.svg +1197 -0
  33. collective/documentgenerator/browser/static/rtf.svg +521 -0
  34. collective/documentgenerator/browser/static/style.svg +1207 -0
  35. collective/documentgenerator/browser/static/subtemplate.svg +1232 -0
  36. collective/documentgenerator/browser/static/xls.svg +159 -0
  37. collective/documentgenerator/browser/static/xlsx.svg +48 -0
  38. collective/documentgenerator/browser/table.py +326 -0
  39. collective/documentgenerator/browser/templates_listing.pt +29 -0
  40. collective/documentgenerator/browser/views.py +366 -0
  41. collective/documentgenerator/config.py +174 -0
  42. collective/documentgenerator/configure.zcml +51 -0
  43. collective/documentgenerator/content/__init__.py +0 -0
  44. collective/documentgenerator/content/condition.py +66 -0
  45. collective/documentgenerator/content/conditions.zcml +30 -0
  46. collective/documentgenerator/content/configure.zcml +18 -0
  47. collective/documentgenerator/content/document_factory.py +73 -0
  48. collective/documentgenerator/content/file_representation.py +70 -0
  49. collective/documentgenerator/content/file_representation.zcml +29 -0
  50. collective/documentgenerator/content/merge_templates.py +15 -0
  51. collective/documentgenerator/content/merge_templates.zcml +11 -0
  52. collective/documentgenerator/content/pod_template.py +601 -0
  53. collective/documentgenerator/content/style_template.py +54 -0
  54. collective/documentgenerator/content/vocabulary.py +344 -0
  55. collective/documentgenerator/content/vocabulary.zcml +105 -0
  56. collective/documentgenerator/demo/__init__.py +0 -0
  57. collective/documentgenerator/demo/configure.zcml +18 -0
  58. collective/documentgenerator/demo/helper.py +170 -0
  59. collective/documentgenerator/events/__init__.py +0 -0
  60. collective/documentgenerator/events/configure.zcml +44 -0
  61. collective/documentgenerator/events/pod_templates_events.py +66 -0
  62. collective/documentgenerator/events/styles_events.py +151 -0
  63. collective/documentgenerator/helper/__init__.py +7 -0
  64. collective/documentgenerator/helper/base.py +334 -0
  65. collective/documentgenerator/helper/configure.zcml +34 -0
  66. collective/documentgenerator/helper/dexterity.py +178 -0
  67. collective/documentgenerator/interfaces.py +166 -0
  68. collective/documentgenerator/locales/collective.documentgenerator.pot +564 -0
  69. collective/documentgenerator/locales/es/LC_MESSAGES/collective.documentgenerator.po +572 -0
  70. collective/documentgenerator/locales/es/LC_MESSAGES/plone.po +32 -0
  71. collective/documentgenerator/locales/fr/LC_MESSAGES/collective.documentgenerator.po +566 -0
  72. collective/documentgenerator/locales/fr/LC_MESSAGES/plone.po +27 -0
  73. collective/documentgenerator/locales/manual.pot +30 -0
  74. collective/documentgenerator/locales/plone-manual.pot +22 -0
  75. collective/documentgenerator/locales/plone.pot +30 -0
  76. collective/documentgenerator/locales/update.sh +22 -0
  77. collective/documentgenerator/migrations/__init__.py +0 -0
  78. collective/documentgenerator/migrations/migrate_to_11.py +52 -0
  79. collective/documentgenerator/migrations/migrate_to_12.py +32 -0
  80. collective/documentgenerator/migrations/migrate_to_13.py +35 -0
  81. collective/documentgenerator/migrations/migrate_to_3.py +36 -0
  82. collective/documentgenerator/migrations/migrate_to_5.py +30 -0
  83. collective/documentgenerator/migrations/migrate_to_6.py +27 -0
  84. collective/documentgenerator/migrations/migrate_to_7.py +34 -0
  85. collective/documentgenerator/migrations/migrate_to_8.py +36 -0
  86. collective/documentgenerator/migrations/migrate_to_9.py +50 -0
  87. collective/documentgenerator/profiles/base/browserlayer.xml +6 -0
  88. collective/documentgenerator/profiles/base/controlpanel.xml +21 -0
  89. collective/documentgenerator/profiles/base/metadata.xml +8 -0
  90. collective/documentgenerator/profiles/base/registry.xml +48 -0
  91. collective/documentgenerator/profiles/base/types/ConfigurablePODTemplate.xml +69 -0
  92. collective/documentgenerator/profiles/base/types/MailingLoopTemplate.xml +68 -0
  93. collective/documentgenerator/profiles/base/types/PODTemplate.xml +68 -0
  94. collective/documentgenerator/profiles/base/types/StyleTemplate.xml +68 -0
  95. collective/documentgenerator/profiles/base/types/SubTemplate.xml +68 -0
  96. collective/documentgenerator/profiles/base/types.xml +22 -0
  97. collective/documentgenerator/profiles/base/viewlets.xml +10 -0
  98. collective/documentgenerator/profiles/demo/browserlayer.xml +6 -0
  99. collective/documentgenerator/profiles/demo/collectivedocumentgenerator_demo_marker.txt +0 -0
  100. collective/documentgenerator/profiles/demo/import_steps.xml +11 -0
  101. collective/documentgenerator/profiles/demo/metadata.xml +7 -0
  102. collective/documentgenerator/profiles/demo/registry.xml +14 -0
  103. collective/documentgenerator/profiles/demo/templates/mailing.odt +0 -0
  104. collective/documentgenerator/profiles/demo/templates/modele_general.ods +0 -0
  105. collective/documentgenerator/profiles/demo/templates/modele_general.odt +0 -0
  106. collective/documentgenerator/profiles/demo/templates/mod/303/250le_collection.odt +0 -0
  107. collective/documentgenerator/profiles/demo/templates/possibly_mailed_model.odt +0 -0
  108. collective/documentgenerator/profiles/demo/templates/styles.odt +0 -0
  109. collective/documentgenerator/profiles/demo/templates/styles_2.odt +0 -0
  110. collective/documentgenerator/profiles/demo/templates/sub_template.odt +0 -0
  111. collective/documentgenerator/profiles/demo/viewlets.xml +3 -0
  112. collective/documentgenerator/profiles/testing/metadata.xml +7 -0
  113. collective/documentgenerator/profiles/testing/types/member.xml +114 -0
  114. collective/documentgenerator/profiles/testing/types.xml +9 -0
  115. collective/documentgenerator/profiles/uninstall/browserlayer.xml +7 -0
  116. collective/documentgenerator/profiles/uninstall/controlpanel.xml +21 -0
  117. collective/documentgenerator/profiles/uninstall/metadata.xml +4 -0
  118. collective/documentgenerator/profiles/uninstall/registry.xml +14 -0
  119. collective/documentgenerator/profiles/uninstall/types.xml +27 -0
  120. collective/documentgenerator/profiles/uninstall/viewlets.xml +8 -0
  121. collective/documentgenerator/profiles.zcml +209 -0
  122. collective/documentgenerator/renderer.zcml +36 -0
  123. collective/documentgenerator/search_replace/__init__.py +0 -0
  124. collective/documentgenerator/search_replace/configure.zcml +21 -0
  125. collective/documentgenerator/search_replace/pod_template.py +161 -0
  126. collective/documentgenerator/search_replace/search_result_form.pt +76 -0
  127. collective/documentgenerator/search_replace/searchreplace_panel.pt +31 -0
  128. collective/documentgenerator/search_replace/searchreplace_panel.py +274 -0
  129. collective/documentgenerator/setuphandlers.py +297 -0
  130. collective/documentgenerator/testing.py +278 -0
  131. collective/documentgenerator/testing.zcml +17 -0
  132. collective/documentgenerator/tests/__init__.py +0 -0
  133. collective/documentgenerator/tests/failing_template.odt +0 -0
  134. collective/documentgenerator/tests/robot/.gitkeep +0 -0
  135. collective/documentgenerator/tests/template_to_clean.odt +0 -0
  136. collective/documentgenerator/tests/template_without_style_page.odt +0 -0
  137. collective/documentgenerator/tests/test_DX_helper_view.py +398 -0
  138. collective/documentgenerator/tests/test_base_helper_view.py +216 -0
  139. collective/documentgenerator/tests/test_check_podtemplates_view.py +138 -0
  140. collective/documentgenerator/tests/test_config.py +119 -0
  141. collective/documentgenerator/tests/test_configurable_pod_template.py +467 -0
  142. collective/documentgenerator/tests/test_demo_profile.py +121 -0
  143. collective/documentgenerator/tests/test_events.py +187 -0
  144. collective/documentgenerator/tests/test_generation_view.py +680 -0
  145. collective/documentgenerator/tests/test_mailing_loop_template.py +48 -0
  146. collective/documentgenerator/tests/test_pod_template.py +190 -0
  147. collective/documentgenerator/tests/test_robot.py +26 -0
  148. collective/documentgenerator/tests/test_search_replace.py +485 -0
  149. collective/documentgenerator/tests/test_setup.py +77 -0
  150. collective/documentgenerator/tests/test_style_template.py +19 -0
  151. collective/documentgenerator/tests/test_sub_template.py +49 -0
  152. collective/documentgenerator/tests/test_templates_listing_view.py +120 -0
  153. collective/documentgenerator/tests/test_utils.py +205 -0
  154. collective/documentgenerator/tests/test_vocabulary.py +169 -0
  155. collective/documentgenerator/utils.py +245 -0
  156. collective/documentgenerator/viewlets/__init__.py +0 -0
  157. collective/documentgenerator/viewlets/batchactions.py +14 -0
  158. collective/documentgenerator/viewlets/configure.zcml +17 -0
  159. collective/documentgenerator/viewlets/generationlinks.pt +46 -0
  160. collective/documentgenerator/viewlets/generationlinks.py +67 -0
  161. collective.documentgenerator-4.0-py3.12-nspkg.pth +1 -0
  162. collective_documentgenerator-4.0.dist-info/METADATA +1136 -0
  163. collective_documentgenerator-4.0.dist-info/RECORD +167 -0
  164. collective_documentgenerator-4.0.dist-info/WHEEL +5 -0
  165. collective_documentgenerator-4.0.dist-info/entry_points.txt +2 -0
  166. collective_documentgenerator-4.0.dist-info/namespace_packages.txt +1 -0
  167. collective_documentgenerator-4.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,601 @@
1
+ # -*- coding: utf-8 -*-
2
+ from collective.documentgenerator import _
3
+ from collective.documentgenerator.browser.controlpanel import COLUMN_MODIFIER_DESCR
4
+ from collective.documentgenerator.config import NEUTRAL_FORMATS
5
+ from collective.documentgenerator.config import ODS_FORMATS
6
+ from collective.documentgenerator.config import ODT_FORMATS
7
+ from collective.documentgenerator.config import POD_FORMATS
8
+ from collective.documentgenerator.content.style_template import StyleTemplate
9
+ from collective.documentgenerator.interfaces import IPODTemplateCondition
10
+ from collective.documentgenerator.interfaces import ITemplatesToMerge
11
+ from collective.documentgenerator.utils import compute_md5
12
+ from collective.z3cform.datagridfield.datagridfield import DataGridFieldFactory
13
+ from collective.z3cform.datagridfield.row import DictRow
14
+ from imio.helpers.content import add_to_annotation
15
+ from imio.helpers.content import del_from_annotation
16
+ from imio.helpers.content import get_from_annotation
17
+ from plone import api
18
+ from plone.autoform import directives as form
19
+ from plone.autoform.interfaces import IFormFieldProvider
20
+ from plone.dexterity.content import Item
21
+ from plone.formwidget.namedfile import NamedFileWidget
22
+ from plone.namedfile.field import NamedBlobFile
23
+ from plone.supermodel import model
24
+ from Products.CMFPlone.utils import safe_unicode
25
+ from z3c.form import validator
26
+ from z3c.form.browser.checkbox import CheckBoxFieldWidget
27
+ from z3c.form.browser.orderedselect import OrderedSelectFieldWidget
28
+ from z3c.form.browser.radio import RadioFieldWidget
29
+ from z3c.form.browser.select import SelectWidget
30
+ from zope import schema
31
+ from zope.component import provideAdapter
32
+ from zope.component import queryAdapter
33
+ from zope.component import queryMultiAdapter
34
+ from zope.interface import implementer
35
+ from zope.interface import Interface
36
+ from zope.interface import Invalid
37
+ from zope.interface import invariant
38
+ from zope.interface import provider
39
+
40
+ import copy
41
+ import logging
42
+
43
+
44
+ logger = logging.getLogger("collective.documentgenerator: PODTemplate")
45
+
46
+
47
+ class IPODTemplate(model.Schema):
48
+ """
49
+ PODTemplate dexterity schema.
50
+ """
51
+
52
+ model.primary("odt_file")
53
+ form.widget("odt_file", NamedFileWidget)
54
+ odt_file = NamedBlobFile(
55
+ title=_(u"ODT File"),
56
+ required=False,
57
+ )
58
+
59
+ form.omitted("initial_md5")
60
+ initial_md5 = schema.TextLine(
61
+ description=u"Initially loaded file md5. Will be compared with os file md5."
62
+ )
63
+
64
+ form.omitted("style_modification_md5")
65
+ style_modification_md5 = schema.TextLine(
66
+ description=u"Working md5, stored when styles only update."
67
+ )
68
+
69
+ form.widget("enabled", RadioFieldWidget)
70
+ enabled = schema.Bool(
71
+ title=_(u"Enabled"),
72
+ default=True,
73
+ required=False,
74
+ )
75
+
76
+ form.widget("style_template", SelectWidget)
77
+ style_template = schema.List(
78
+ title=_(u"Style template"),
79
+ description=_(u"Choose the style template to apply for this template."),
80
+ value_type=schema.Choice(source="collective.documentgenerator.StyleTemplates"),
81
+ required=False,
82
+ )
83
+
84
+
85
+ @implementer(IPODTemplate)
86
+ class PODTemplate(Item):
87
+ """
88
+ PODTemplate dexterity class.
89
+ """
90
+
91
+ def get_file(self):
92
+ """
93
+ Method used in renderer in order to retrieve the odt_file to be used.
94
+ :return: an odt_file field.
95
+ """
96
+ return self.odt_file
97
+
98
+ def can_be_generated(self, context):
99
+ """
100
+ Evaluate if the template can be generated on a given context.
101
+ """
102
+ condition_obj = queryMultiAdapter((self, context), IPODTemplateCondition)
103
+ if condition_obj:
104
+ can_be_generated = condition_obj.evaluate()
105
+ return can_be_generated
106
+
107
+ def get_style_template(self):
108
+ """
109
+ Return associated StylesTemplate from which styles will be imported
110
+ to the current PODTemplate.
111
+ """
112
+ if not self.style_template:
113
+ return
114
+
115
+ catalog = api.portal.get_tool("portal_catalog")
116
+ style_template_brain = catalog(UID=self.style_template)
117
+
118
+ if style_template_brain:
119
+ return style_template_brain[0].getObject()
120
+
121
+ return None
122
+
123
+ def get_templates_to_merge(self):
124
+ """
125
+ Return associated PODTemplates merged into the current PODTemplate
126
+ when it is rendered.
127
+ """
128
+ templates_to_merge = queryAdapter(self, ITemplatesToMerge)
129
+ if templates_to_merge:
130
+ return templates_to_merge.get()
131
+ return {}
132
+
133
+ def get_available_formats(self):
134
+ """
135
+ Returns formats in which current template may be generated.
136
+ """
137
+ return [
138
+ "odt",
139
+ ]
140
+
141
+ @property
142
+ def current_md5(self):
143
+ md5 = u""
144
+ if self.odt_file:
145
+ md5 = safe_unicode(compute_md5(self.odt_file.data))
146
+ return md5
147
+
148
+ def has_been_modified(self):
149
+ """
150
+ Current md5 will also be different from initial_md5 without user modification but if style modification.
151
+ Current_md5 will be equal to style_modification_md5 if style only update.
152
+ This last one will be updated after style modification only. See events
153
+ """
154
+ return self.current_md5 != self.style_modification_md5
155
+
156
+ def get_column_modifier(self):
157
+ """By default, return -1 meaning use global config value."""
158
+ return -1
159
+
160
+ def add_parent_pod_annotation(self):
161
+ return None
162
+
163
+ def del_parent_pod_annotation(self):
164
+ return None
165
+
166
+ def has_linked_template(self):
167
+ return False
168
+
169
+
170
+ class IMergeTemplatesRowSchema(Interface):
171
+ """
172
+ Schema for DataGridField widget's row of field 'merge_templates'
173
+ """
174
+
175
+ template = schema.Choice(
176
+ title=_(u"Template"),
177
+ vocabulary="collective.documentgenerator.MergeTemplates",
178
+ required=True,
179
+ )
180
+
181
+ pod_context_name = schema.TextLine(
182
+ title=_(u"POD context name"),
183
+ required=True,
184
+ )
185
+
186
+ form.widget("do_rendering", RadioFieldWidget)
187
+ do_rendering = schema.Bool(
188
+ title=_(u"Do rendering"),
189
+ )
190
+
191
+
192
+ class IContextVariablesRowSchema(Interface):
193
+ """
194
+ Schema for DataGridField widget's row of field 'context_variables'
195
+ """
196
+
197
+ name = schema.TextLine(
198
+ title=_(u"Variable name"),
199
+ required=True,
200
+ )
201
+
202
+ value = schema.TextLine(
203
+ title=_(u"Value"),
204
+ required=False,
205
+ )
206
+
207
+
208
+ class PodFormatsValidator(validator.SimpleFieldValidator):
209
+ """z3c.form validator class for Pod formats"""
210
+
211
+ def validate(self, value, force=False):
212
+ # thanks to widget, we get just-loaded file.filename.
213
+ widgets = self.view.widgets
214
+ current_filename = u""
215
+ for element in widgets.items():
216
+ if element[0] == "odt_file":
217
+ current_filename = safe_unicode(element[1].filename)
218
+ if current_filename:
219
+ FORMATS_DICT = {
220
+ "ods": ODS_FORMATS + NEUTRAL_FORMATS,
221
+ "odt": ODT_FORMATS + NEUTRAL_FORMATS,
222
+ }
223
+ extension = current_filename.split(".")[-1]
224
+ if extension not in FORMATS_DICT.keys():
225
+ extension = "odt"
226
+ authorise_element_list = FORMATS_DICT[extension]
227
+ authorise_extension_list = [elem[0] for elem in authorise_element_list]
228
+ if not value:
229
+ raise Invalid(_(u"No format selected"))
230
+ for element in value:
231
+ if element not in authorise_extension_list:
232
+ elem = self.get_invalid_error(element)
233
+ error_message = _(
234
+ u"element_not_valid",
235
+ default=u'Element ${elem} is not valid for .${extension} template : "${template}"',
236
+ mapping={
237
+ u"elem": elem,
238
+ u"extension": extension,
239
+ u"template": current_filename,
240
+ },
241
+ )
242
+ raise Invalid(error_message)
243
+
244
+ def get_invalid_error(self, extension):
245
+ for element in POD_FORMATS:
246
+ if element[0] == extension:
247
+ return element[1]
248
+
249
+
250
+ class IRenamePageStylesSchema(model.Schema):
251
+ """This interface must be removed after migration to 8 is done !"""
252
+
253
+
254
+ @provider(IFormFieldProvider)
255
+ class IConfigurablePODTemplate(IPODTemplate):
256
+ """
257
+ ConfigurablePODTemplate dexterity schema.
258
+ """
259
+
260
+ form.order_before(is_reusable="enabled")
261
+ form.widget("is_reusable", RadioFieldWidget)
262
+ is_reusable = schema.Bool(
263
+ title=_(u"Reusable"),
264
+ description=_(
265
+ u"Check if this POD Template can be reused by other POD Template"
266
+ ),
267
+ required=False,
268
+ default=False,
269
+ )
270
+
271
+ form.order_before(pod_template_to_use="enabled")
272
+ pod_template_to_use = schema.Choice(
273
+ title=_(u"Select Existing POD Template to reuse"),
274
+ vocabulary="collective.documentgenerator.ExistingPODTemplate",
275
+ required=False,
276
+ default=None,
277
+ )
278
+
279
+ form.widget("pod_formats", OrderedSelectFieldWidget, multiple="multiple", size=5)
280
+ pod_formats = schema.List(
281
+ title=_(u"Available formats"),
282
+ description=_(u"Select format in which the template will be generable."),
283
+ value_type=schema.Choice(source="collective.documentgenerator.Formats"),
284
+ required=True,
285
+ default=[
286
+ "odt",
287
+ ],
288
+ )
289
+
290
+ csv_field_delimiter = schema.Choice(
291
+ title=_(u"Field Delimiter for CSV format"),
292
+ description=_(
293
+ u"Select the character to be used to separate fields into CSV files"
294
+ ),
295
+ vocabulary="collective.documentgenerator.CSVFieldDelimiter",
296
+ required=True,
297
+ default=u",",
298
+ )
299
+ csv_string_delimiter = schema.Choice(
300
+ title=_(u"String Delimiter for CSV format"),
301
+ description=_(
302
+ u"Select the character to be used to wrap around string values in CSV files"
303
+ ),
304
+ vocabulary="collective.documentgenerator.CSVStringDelimiter",
305
+ required=True,
306
+ default=u'"',
307
+ )
308
+
309
+ form.widget("pod_portal_types", CheckBoxFieldWidget, multiple="multiple", size=15)
310
+ pod_portal_types = schema.List(
311
+ title=_(u"Allowed portal types"),
312
+ description=_(
313
+ u"Select for which content types the template will be available."
314
+ ),
315
+ value_type=schema.Choice(source="collective.documentgenerator.PortalTypes"),
316
+ required=False,
317
+ )
318
+
319
+ form.widget("merge_templates", DataGridFieldFactory)
320
+ merge_templates = schema.List(
321
+ title=_(u"Templates to merge."),
322
+ required=False,
323
+ value_type=DictRow(schema=IMergeTemplatesRowSchema, required=False),
324
+ default=[],
325
+ )
326
+
327
+ form.widget("context_variables", DataGridFieldFactory)
328
+ context_variables = schema.List(
329
+ title=_(u"Context variables."),
330
+ description=_("These context variables are added to the odt_file context."),
331
+ required=False,
332
+ value_type=DictRow(schema=IContextVariablesRowSchema, required=False),
333
+ )
334
+
335
+ mailing_loop_template = schema.Choice(
336
+ title=_(u"Mailing loop template"),
337
+ description=_(u"Choose the mailing loop template to apply for this template."),
338
+ vocabulary="collective.documentgenerator.EnabledMailingLoopTemplates",
339
+ required=False,
340
+ )
341
+
342
+ column_modifier = schema.Choice(
343
+ title=_(u"Table column modifier"),
344
+ description=_(COLUMN_MODIFIER_DESCR),
345
+ vocabulary="collective.documentgenerator.PodColumnModifier",
346
+ required=True,
347
+ default=-1,
348
+ )
349
+
350
+ @invariant
351
+ def validate_context_variables(data):
352
+ keys = []
353
+ forbidden = ["context", "view", "uids", "brains", "self"]
354
+ to_check = copy.deepcopy(data.context_variables or [])
355
+ to_check.extend(copy.deepcopy(data.merge_templates or []))
356
+
357
+ for line in to_check:
358
+ value = ("name" in line and line["name"]) or (
359
+ "pod_context_name" in line and line["pod_context_name"]
360
+ )
361
+
362
+ if value in forbidden:
363
+ raise Invalid(
364
+ _(
365
+ "You can't use one of these words: ${list}",
366
+ mapping={"list": ", ".join(forbidden)},
367
+ )
368
+ )
369
+
370
+ if value in keys:
371
+ raise Invalid(
372
+ _(
373
+ "You have twice used the same name '${name}'",
374
+ mapping={"name": value},
375
+ )
376
+ )
377
+ else:
378
+ keys.append(value)
379
+
380
+ @invariant
381
+ def validate_pod_file(data):
382
+ if not (data.odt_file or data.pod_template_to_use):
383
+ raise Invalid(
384
+ _("You must select an odt file or an existing pod template"), schema
385
+ )
386
+
387
+ @invariant
388
+ def validate_pod_template_to_use(data):
389
+ if data.pod_template_to_use and (data.is_reusable or data.odt_file):
390
+ raise Invalid(
391
+ _(
392
+ "You can't select a POD Template or set this template reusable "
393
+ "if you have chosen an existing POD Template."
394
+ ),
395
+ schema,
396
+ )
397
+
398
+
399
+ # Set conditions for which fields the validator class applies
400
+ validator.WidgetValidatorDiscriminators(
401
+ PodFormatsValidator, field=IConfigurablePODTemplate["pod_formats"]
402
+ )
403
+
404
+ # Register the validator so it will be looked up by z3c.form machinery
405
+ provideAdapter(PodFormatsValidator)
406
+
407
+
408
+ @implementer(IConfigurablePODTemplate)
409
+ class ConfigurablePODTemplate(PODTemplate):
410
+ """
411
+ ConfigurablePODTemplate dexterity class.
412
+ """
413
+
414
+ parent_pod_annotation_key = "linked_child_templates"
415
+
416
+ def get_pod_template_to_use(self):
417
+ """
418
+ Method used in renderer in order to retrieve the pod_template to be used.
419
+ :return: the used object.
420
+ """
421
+ if self.pod_template_to_use:
422
+ # make sure we get the POD template holding the odt_file
423
+ # by searching it unrestrictedly
424
+ catalog = api.portal.get_tool("portal_catalog")
425
+ brains = catalog.unrestrictedSearchResults(UID=self.pod_template_to_use)
426
+ if brains:
427
+ return brains[0]._unrestrictedGetObject()
428
+ return None
429
+
430
+ def get_file(self):
431
+ """
432
+ Method used in renderer in order to retrieve the odt_file to be used.
433
+ :return: an odt_file field.
434
+ """
435
+ pttu = self.get_pod_template_to_use()
436
+ if pttu:
437
+ return pttu.odt_file
438
+ return self.odt_file
439
+
440
+ def get_filename(self):
441
+ """Add accessor for file.filename as it is private by default in plone.namedfile."""
442
+ return self.get_file().filename
443
+
444
+ def __setattr__(self, key, value):
445
+ if key == "pod_template_to_use":
446
+ if (
447
+ hasattr(self, "pod_template_to_use")
448
+ and self.pod_template_to_use != value
449
+ ):
450
+ if self.pod_template_to_use:
451
+ self.del_parent_pod_annotation()
452
+ super(ConfigurablePODTemplate, self).__setattr__(key, value)
453
+ self.add_parent_pod_annotation()
454
+ else:
455
+ super(ConfigurablePODTemplate, self).__setattr__(key, value)
456
+
457
+ def has_linked_template(self):
458
+ return self.pod_template_to_use is not None
459
+
460
+ def set_merge_templates(
461
+ self, pod_template, pod_context_name, do_rendering=True, position=-1
462
+ ):
463
+ old_value = list(self.merge_templates)
464
+ newline = {
465
+ "template": pod_template.UID(),
466
+ "pod_context_name": pod_context_name,
467
+ "do_rendering": do_rendering,
468
+ }
469
+ if position >= 0:
470
+ old_value.insert(position, newline)
471
+ else:
472
+ old_value.append(newline)
473
+ self.merge_templates = old_value
474
+
475
+ def get_templates_to_merge(self):
476
+ """
477
+ Return associated PODTemplates merged into the current PODTemplate
478
+ when it is rendered.
479
+ """
480
+ catalog = api.portal.get_tool("portal_catalog")
481
+ pod_context = {}
482
+
483
+ if self.merge_templates:
484
+ for line in self.merge_templates:
485
+ pod_template = catalog(UID=line["template"])[0].getObject()
486
+ pod_context[line["pod_context_name"]] = (
487
+ pod_template,
488
+ line["do_rendering"],
489
+ )
490
+
491
+ return pod_context
492
+
493
+ def get_available_formats(self):
494
+ """
495
+ Returns formats in which current template may be generated.
496
+ """
497
+ return self.pod_formats
498
+
499
+ def get_context_variables(self):
500
+ """
501
+ Returns context_variables as dict
502
+ """
503
+ ret = {}
504
+ for line in self.context_variables or []:
505
+ # Simple boolean conversion actually implemented
506
+ # May be improved with ZPublisher.Converters, managing boolean, int, date, string, text, ...
507
+ # Those last values in a list box with string as default value
508
+ val = line["value"]
509
+ if val == "True":
510
+ val = True
511
+ elif val == "False":
512
+ val = False
513
+ ret[line["name"]] = val
514
+ return ret
515
+
516
+ def get_column_modifier(self):
517
+ return self.column_modifier
518
+
519
+ def add_parent_pod_annotation(self):
520
+ if self.pod_template_to_use:
521
+ try:
522
+ add_to_annotation(
523
+ ConfigurablePODTemplate.parent_pod_annotation_key,
524
+ self.UID(),
525
+ uid=self.pod_template_to_use,
526
+ )
527
+ except TypeError:
528
+ pass
529
+ # object has no UID yet. It will be retried when IObjectCreatedEvent will be triggered
530
+ # for some reasons, isinstance(self, IUUID) always returns False
531
+
532
+ def del_parent_pod_annotation(self):
533
+ if hasattr(self, "pod_template_to_use") and self.pod_template_to_use:
534
+ del_from_annotation(
535
+ ConfigurablePODTemplate.parent_pod_annotation_key,
536
+ self.UID(),
537
+ uid=self.pod_template_to_use,
538
+ )
539
+
540
+ def get_children_pod_template(self):
541
+ """
542
+ @see plone.api.content.get
543
+ @see imio.helpers.content.get_from_annotation
544
+ @see imio.helpers.content.del_from_annotation
545
+ :return: a list containing the children templates.
546
+
547
+ Handles the case when the uid doesn't match any object by deleting the uid from anotation.
548
+ """
549
+ res = set()
550
+ to_delete = set()
551
+ annotated = get_from_annotation(
552
+ ConfigurablePODTemplate.parent_pod_annotation_key, self
553
+ )
554
+ if annotated:
555
+ for uid in annotated:
556
+ child = api.content.get(UID=uid)
557
+ if child:
558
+ res.add(child)
559
+ else:
560
+ # child doesn't exist anymore. api.content.get returned None
561
+ to_delete.add(uid)
562
+ for uid in to_delete:
563
+ del_from_annotation(
564
+ ConfigurablePODTemplate.parent_pod_annotation_key, uid, obj=self
565
+ )
566
+ return res
567
+
568
+
569
+ class ISubTemplate(IPODTemplate):
570
+ """
571
+ PODTemplate used only a sub template to merge
572
+ """
573
+
574
+
575
+ @implementer(ISubTemplate)
576
+ class SubTemplate(PODTemplate):
577
+ """
578
+ PODTemplate used only a sub template to merge
579
+ """
580
+
581
+
582
+ class IMailingLoopTemplate(IPODTemplate):
583
+ """
584
+ PODTemplate used only to loop for mailing
585
+ """
586
+
587
+
588
+ @implementer(IMailingLoopTemplate)
589
+ class MailingLoopTemplate(PODTemplate):
590
+ """
591
+ PODTemplate used only to loop for mailing
592
+ """
593
+
594
+
595
+ POD_TEMPLATE_TYPES = {
596
+ PODTemplate.__name__: PODTemplate,
597
+ ConfigurablePODTemplate.__name__: ConfigurablePODTemplate,
598
+ SubTemplate.__name__: SubTemplate,
599
+ MailingLoopTemplate.__name__: MailingLoopTemplate,
600
+ StyleTemplate.__name__: StyleTemplate,
601
+ }
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from collective.documentgenerator import _
4
+ from collective.documentgenerator.utils import compute_md5
5
+ from plone.autoform import directives as form
6
+ from plone.dexterity.content import Item
7
+ from plone.formwidget.namedfile import NamedFileWidget
8
+ from plone.namedfile.field import NamedBlobFile
9
+ from plone.supermodel import model
10
+ from Products.CMFPlone.utils import safe_unicode
11
+ from zope import schema
12
+ from zope.interface import implementer
13
+
14
+ import logging
15
+
16
+
17
+ logger = logging.getLogger("collective.documentgenerator: StyleTemplate")
18
+
19
+
20
+ class IStyleTemplate(model.Schema):
21
+ """
22
+ StyleTemplate dexterity schema.
23
+ """
24
+
25
+ model.primary("odt_file")
26
+ form.widget("odt_file", NamedFileWidget)
27
+ odt_file = NamedBlobFile(
28
+ title=_(u"ODT File"),
29
+ )
30
+
31
+ form.omitted("initial_md5")
32
+ initial_md5 = schema.TextLine(
33
+ description=u"Initially loaded file md5. Will be compared with os file md5."
34
+ )
35
+
36
+
37
+ @implementer(IStyleTemplate)
38
+ class StyleTemplate(Item):
39
+ """
40
+ StyleTemplate dexterity class.
41
+ """
42
+
43
+ @property
44
+ def current_md5(self):
45
+ md5 = u""
46
+ if self.odt_file:
47
+ md5 = safe_unicode(compute_md5(self.odt_file.data))
48
+ return md5
49
+
50
+ def has_been_modified(self):
51
+ """
52
+ Current md5 will be different from initial_md5 if user modification.
53
+ """
54
+ return self.current_md5 != self.initial_md5