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