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,146 @@
|
|
|
1
|
+
from collective.documentgenerator.browser.generation_view import HAS_FINGERPOINTING
|
|
2
|
+
from collective.documentgenerator.utils import get_site_root_relative_path
|
|
3
|
+
from collective.documentgenerator.utils import temporary_file_name
|
|
4
|
+
from plone.namedfile import NamedBlobFile
|
|
5
|
+
|
|
6
|
+
import collections
|
|
7
|
+
import mimetypes
|
|
8
|
+
import os
|
|
9
|
+
import shutil
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
SearchReplaceResult = collections.namedtuple(
|
|
13
|
+
"SearchReplaceResult",
|
|
14
|
+
["pod_expr", "match_start", "match_end", "match", "node_type", "new_pod_expr"],
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class SearchAndReplacePODTemplates:
|
|
19
|
+
"""
|
|
20
|
+
Search and replace POD expression among PODTemplates.
|
|
21
|
+
Use it like this:
|
|
22
|
+
with SearchAndReplacePODTemplates(podtemplate_obj) as search_replace:
|
|
23
|
+
search_results = search_replace.search('WhatISearch')
|
|
24
|
+
replace_results = search_replace.replace('^WillBeReplaced$', 'ByThis', is_regex=True)
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(self, podtemplates):
|
|
28
|
+
"""
|
|
29
|
+
:param podtemplates: List of PODTemplate objects
|
|
30
|
+
"""
|
|
31
|
+
self.podtemplates = podtemplates
|
|
32
|
+
self.templates_by_filename = {}
|
|
33
|
+
self.tmp_dir = temporary_file_name(suffix='search_replace')
|
|
34
|
+
self.changed_files = set()
|
|
35
|
+
|
|
36
|
+
# compute the (future) file system path of the plone pod templates
|
|
37
|
+
for podtemplate in podtemplates:
|
|
38
|
+
if not podtemplate.odt_file or not podtemplate.odt_file.filename:
|
|
39
|
+
continue # ignore templates referring another pod template.
|
|
40
|
+
file_extension = podtemplate.odt_file.filename.split(".")[-1].lower()
|
|
41
|
+
template_path = get_site_root_relative_path(podtemplate)
|
|
42
|
+
fs_filename = "{}/{}.{}".format(
|
|
43
|
+
self.tmp_dir, template_path.replace("/", "_"), file_extension
|
|
44
|
+
)
|
|
45
|
+
self.templates_by_filename[fs_filename] = {"obj": podtemplate, "path": template_path}
|
|
46
|
+
|
|
47
|
+
def __enter__(self):
|
|
48
|
+
"""
|
|
49
|
+
copy the plone pod template content on the file system
|
|
50
|
+
"""
|
|
51
|
+
os.mkdir(self.tmp_dir)
|
|
52
|
+
for filename in self.templates_by_filename.keys():
|
|
53
|
+
# clean old files
|
|
54
|
+
if os.path.isfile(filename):
|
|
55
|
+
os.remove(filename)
|
|
56
|
+
# copy the pod templates on the file system.
|
|
57
|
+
with open(filename, "w") as template_file:
|
|
58
|
+
plone_template = self.templates_by_filename[filename]["obj"]
|
|
59
|
+
template_file.write(plone_template.odt_file.data)
|
|
60
|
+
return self
|
|
61
|
+
|
|
62
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
|
63
|
+
# push the result back into the site pod templates
|
|
64
|
+
for filename in self.changed_files:
|
|
65
|
+
with open(filename, "rb") as replaced_file:
|
|
66
|
+
podtemplate = self.templates_by_filename[filename]["obj"]
|
|
67
|
+
result = NamedBlobFile(
|
|
68
|
+
data=replaced_file.read(),
|
|
69
|
+
contentType=podtemplate.odt_file.contentType
|
|
70
|
+
or mimetypes.guess_type(filename)[0],
|
|
71
|
+
filename=podtemplate.odt_file.filename,
|
|
72
|
+
)
|
|
73
|
+
podtemplate.odt_file = result
|
|
74
|
+
# delete tmp directory
|
|
75
|
+
shutil.rmtree(self.tmp_dir)
|
|
76
|
+
|
|
77
|
+
def search(self, find_expr, is_regex=False):
|
|
78
|
+
"""
|
|
79
|
+
Search find_expr in self.podtemplates
|
|
80
|
+
:param find_expr: A regex str or simple string
|
|
81
|
+
:param is_regex: use is_regex=False if find_expr is not a regex
|
|
82
|
+
:return: a dict with podtemplate uid as key and list of SearchReplaceResult as value
|
|
83
|
+
"""
|
|
84
|
+
from appy.bin.odfgrep import Grep
|
|
85
|
+
|
|
86
|
+
grepper = Grep(find_expr, self.tmp_dir, asString=not is_regex, verbose=0)
|
|
87
|
+
grepper.run()
|
|
88
|
+
results = self._prepare_results_output(grepper.matches, is_replacing=False)
|
|
89
|
+
return results
|
|
90
|
+
|
|
91
|
+
def replace(self, find_expr, replace_expr, is_regex=False, dry_run=False):
|
|
92
|
+
"""
|
|
93
|
+
Replace find_expr match with replace_expr in self.podtemplates
|
|
94
|
+
:param find_expr: A regex str or simple str
|
|
95
|
+
:param replace_expr: A str that will replace find_expr match
|
|
96
|
+
:param is_regex: Use is_regex=False if find_expr is not a regex
|
|
97
|
+
:param dry_run: Perform a dry run and not the actual replacement(s).
|
|
98
|
+
This will not modify the template(s) and can be used safely.
|
|
99
|
+
:return: a dict with podtemplate uid as key and list of SearchReplaceResult as value
|
|
100
|
+
"""
|
|
101
|
+
from appy.bin.odfgrep import Grep
|
|
102
|
+
|
|
103
|
+
grepper = Grep(
|
|
104
|
+
find_expr,
|
|
105
|
+
self.tmp_dir,
|
|
106
|
+
repl=replace_expr,
|
|
107
|
+
asString=not is_regex,
|
|
108
|
+
dryRun=dry_run,
|
|
109
|
+
verbose=0,
|
|
110
|
+
)
|
|
111
|
+
grepper.run()
|
|
112
|
+
results = self._prepare_results_output(grepper.matches, is_replacing=False)
|
|
113
|
+
|
|
114
|
+
for filename in grepper.matches.keys():
|
|
115
|
+
self.changed_files.add(filename)
|
|
116
|
+
|
|
117
|
+
return results
|
|
118
|
+
|
|
119
|
+
def _prepare_results_output(self, matches, replace_expr="", is_replacing=True):
|
|
120
|
+
""" Prepare results output so we have a nice feedback when searching and replacing """
|
|
121
|
+
results = {}
|
|
122
|
+
for file_path, file_results in matches.items():
|
|
123
|
+
template = self.templates_by_filename[file_path]["obj"]
|
|
124
|
+
template_uid = template.UID()
|
|
125
|
+
results[template_uid] = file_results
|
|
126
|
+
return results
|
|
127
|
+
|
|
128
|
+
@staticmethod
|
|
129
|
+
def _log_replace(template, match, replaced_by, old_pod_expr, new_pod_expr):
|
|
130
|
+
""" Log replacements if fingerpointing installed """
|
|
131
|
+
if HAS_FINGERPOINTING:
|
|
132
|
+
from collective.fingerpointing.config import AUDIT_MESSAGE
|
|
133
|
+
from collective.fingerpointing.logger import log_info
|
|
134
|
+
from collective.fingerpointing.utils import get_request_information
|
|
135
|
+
|
|
136
|
+
# add logging message to fingerpointing log
|
|
137
|
+
user, ip = get_request_information()
|
|
138
|
+
action = "replace_in_template"
|
|
139
|
+
extras = u"podtemplate={0} match={1} replaced_by={2} old_pod_expr={3} new_pod_expr={4}".format(
|
|
140
|
+
"/".join(template.getPhysicalPath()),
|
|
141
|
+
match,
|
|
142
|
+
replaced_by,
|
|
143
|
+
old_pod_expr,
|
|
144
|
+
new_pod_expr,
|
|
145
|
+
)
|
|
146
|
+
log_info(unicode(AUDIT_MESSAGE).format(user, ip, action, extras))
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
|
|
2
|
+
<div tal:condition="python: bool(view.get_results_table())"
|
|
3
|
+
i18n:domain="collective.documentgenerator">
|
|
4
|
+
<table class="datagridwidget-table-view search-replace-results" style="margin-top: 0.5em"
|
|
5
|
+
tal:define="templates python:view.get_results_table().items()">
|
|
6
|
+
<thead>
|
|
7
|
+
<tr>
|
|
8
|
+
<th class="header" i18n:translate="">
|
|
9
|
+
POD expression
|
|
10
|
+
</th>
|
|
11
|
+
<th tal:condition="view/is_previewing" class="header selected-header" i18n:translate="">
|
|
12
|
+
Selected
|
|
13
|
+
</th>
|
|
14
|
+
</tr>
|
|
15
|
+
</thead>
|
|
16
|
+
<tbody class="datagridwidget-tbody">
|
|
17
|
+
<tal:block repeat="template templates">
|
|
18
|
+
<tr class="header">
|
|
19
|
+
<td>
|
|
20
|
+
<div class="search-group"
|
|
21
|
+
tal:define="template_url python:view.get_template_link(template[0]);
|
|
22
|
+
template_title python:view.get_template_breadcrumb(template[0]);">
|
|
23
|
+
<a tal:attributes="href template_url" tal:content="template_title"/>
|
|
24
|
+
</div>
|
|
25
|
+
</td>
|
|
26
|
+
<td tal:condition="view/is_previewing" class="search-group">
|
|
27
|
+
<input type="checkbox" tal:attributes="value python: template[0]"
|
|
28
|
+
name="selected_templates" checked/>
|
|
29
|
+
</td>
|
|
30
|
+
</tr>
|
|
31
|
+
<tal:block repeat="result python:template[1]">
|
|
32
|
+
<tr>
|
|
33
|
+
<td colspan="2" tal:condition="python:view.is_previewing()">
|
|
34
|
+
<span class="search-cell"
|
|
35
|
+
tal:content="structure python:result.content"
|
|
36
|
+
/>
|
|
37
|
+
</td>
|
|
38
|
+
<td colspan="2" tal:condition="python:not view.is_previewing()">
|
|
39
|
+
<span class="search-cell"
|
|
40
|
+
tal:content="structure python:view.display_diff(result)"
|
|
41
|
+
/>
|
|
42
|
+
</td>
|
|
43
|
+
</tr>
|
|
44
|
+
</tal:block>
|
|
45
|
+
</tal:block>
|
|
46
|
+
</tbody>
|
|
47
|
+
</table>
|
|
48
|
+
</div>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
|
|
2
|
+
xmlns:tal="http://xml.zope.org/namespaces/tal"
|
|
3
|
+
xmlns:metal="http://xml.zope.org/namespaces/metal"
|
|
4
|
+
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
|
|
5
|
+
lang="en"
|
|
6
|
+
metal:use-macro="here/prefs_main_template/macros/master"
|
|
7
|
+
i18n:domain="collective.documentgenerator">
|
|
8
|
+
|
|
9
|
+
<metal:block fill-slot="top_slot"
|
|
10
|
+
tal:define="dummy python:request.set('disable_border',1)"/>
|
|
11
|
+
|
|
12
|
+
<body>
|
|
13
|
+
<div id="content" metal:fill-slot="prefs_configlet_content">
|
|
14
|
+
|
|
15
|
+
<h1 class="documentFirstHeading" tal:content="view/label">View Title</h1>
|
|
16
|
+
|
|
17
|
+
<div id="layout-contents">
|
|
18
|
+
<span tal:replace="structure view/contents"/>
|
|
19
|
+
</div>
|
|
20
|
+
</div>
|
|
21
|
+
</body>
|
|
22
|
+
</html>
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from collections import OrderedDict
|
|
3
|
+
from collective.documentgenerator import _
|
|
4
|
+
from collective.documentgenerator.content.vocabulary import AllPODTemplateWithFileVocabularyFactory
|
|
5
|
+
from collective.documentgenerator.search_replace.pod_template import SearchAndReplacePODTemplates
|
|
6
|
+
from imio.helpers import HAS_PLONE_5_AND_MORE
|
|
7
|
+
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper
|
|
8
|
+
from plone.app.uuid.utils import uuidToObject
|
|
9
|
+
from plone.autoform import directives
|
|
10
|
+
from plone.autoform.form import AutoExtensibleForm
|
|
11
|
+
from plone.z3cform.widget import SingleCheckBoxFieldWidget
|
|
12
|
+
from z3c.form import button
|
|
13
|
+
from z3c.form import form
|
|
14
|
+
from z3c.form.contentprovider import ContentProviders
|
|
15
|
+
from z3c.form.interfaces import IFieldsAndContentProvidersForm
|
|
16
|
+
from zope import component
|
|
17
|
+
from zope import interface
|
|
18
|
+
from zope import schema
|
|
19
|
+
from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
|
|
20
|
+
from zope.contentprovider.provider import ContentProviderBase
|
|
21
|
+
from zope.interface import implements
|
|
22
|
+
from zope.interface import Invalid
|
|
23
|
+
from zope.interface import invariant
|
|
24
|
+
|
|
25
|
+
import re
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if HAS_PLONE_5_AND_MORE:
|
|
29
|
+
from collective.z3cform.datagridfield.datagridfield import DataGridFieldFactory
|
|
30
|
+
from collective.z3cform.datagridfield.row import DictRow
|
|
31
|
+
else:
|
|
32
|
+
from collective.z3cform.datagridfield import DataGridFieldFactory
|
|
33
|
+
from collective.z3cform.datagridfield import DictRow
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class IReplacementRowSchema(interface.Interface):
|
|
37
|
+
"""
|
|
38
|
+
Schema for DataGridField widget's row of field 'replacements'
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
search_expr = schema.TextLine(title=_(u"Search"), required=True, default=u"")
|
|
42
|
+
replace_expr = schema.TextLine(title=_(u"Replace"), required=False, default=u"")
|
|
43
|
+
|
|
44
|
+
directives.widget("is_regex", SingleCheckBoxFieldWidget)
|
|
45
|
+
is_regex = schema.Bool(title=_(u"Regex?"),)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class IDocumentGeneratorSearchReplacePanelSchema(interface.Interface):
|
|
49
|
+
"""
|
|
50
|
+
Schema for DocumentGeneratorSearchReplacePanel
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
selected_templates = schema.List(
|
|
54
|
+
title=_(u"heading_selected_templates", default=u"Selected templates"),
|
|
55
|
+
description=_(u"description_selected_templates", default=u""),
|
|
56
|
+
required=False,
|
|
57
|
+
default=[],
|
|
58
|
+
missing_value=[],
|
|
59
|
+
value_type=schema.Choice(source="collective.documentgenerator.AllPODTemplateWithFile"),
|
|
60
|
+
)
|
|
61
|
+
directives.widget("replacements", DataGridFieldFactory)
|
|
62
|
+
replacements = schema.List(
|
|
63
|
+
title=_(u"Replacements"),
|
|
64
|
+
description=_("The replacements that will be made."),
|
|
65
|
+
required=False,
|
|
66
|
+
value_type=DictRow(schema=IReplacementRowSchema, required=True),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
@invariant
|
|
70
|
+
def has_valid_regexes(data):
|
|
71
|
+
if hasattr(data, 'replacements'):
|
|
72
|
+
for i, row in enumerate(data.replacements):
|
|
73
|
+
if row["is_regex"]:
|
|
74
|
+
try:
|
|
75
|
+
re.compile(row["search_expr"])
|
|
76
|
+
except re.error:
|
|
77
|
+
raise Invalid(_(u"Incorrect regex at row #{0} : \"{1}\"").format(
|
|
78
|
+
i + 1, row["search_expr"]))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class SearchResultProvider(ContentProviderBase):
|
|
82
|
+
"""
|
|
83
|
+
Search result and replace form is implemented through a content provider.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
template = ViewPageTemplateFile('search_result_form.pt')
|
|
87
|
+
|
|
88
|
+
def __init__(self, context, request, view):
|
|
89
|
+
super(SearchResultProvider, self).__init__(context, request, view)
|
|
90
|
+
self.view = view
|
|
91
|
+
self.results_table = OrderedDict()
|
|
92
|
+
|
|
93
|
+
def render(self):
|
|
94
|
+
return self.template()
|
|
95
|
+
|
|
96
|
+
def is_previewing(self):
|
|
97
|
+
return self.view.is_previewing
|
|
98
|
+
|
|
99
|
+
def get_results_table(self):
|
|
100
|
+
return self.view.results_table
|
|
101
|
+
|
|
102
|
+
def get_template_link(self, uid):
|
|
103
|
+
template = uuidToObject(uid)
|
|
104
|
+
return template.absolute_url()
|
|
105
|
+
|
|
106
|
+
def get_template_breadcrumb(self, uid):
|
|
107
|
+
template = uuidToObject(uid)
|
|
108
|
+
breadcrumb_view = template.restrictedTraverse('breadcrumbs_view')
|
|
109
|
+
title = ' / '.join([bc['Title'] for bc in breadcrumb_view.breadcrumbs()]) + ' ({})'.format(template.id)
|
|
110
|
+
return title
|
|
111
|
+
|
|
112
|
+
@staticmethod
|
|
113
|
+
def display_diff(result):
|
|
114
|
+
"""
|
|
115
|
+
Add a <strong> HTML tag with class highlight around start and end indices
|
|
116
|
+
"""
|
|
117
|
+
return result.getDiff(type="xhtml")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class DocumentGeneratorSearchReplacePanelAdapter(object):
|
|
121
|
+
interface.implements(IDocumentGeneratorSearchReplacePanelSchema)
|
|
122
|
+
component.adapts(interface.Interface)
|
|
123
|
+
|
|
124
|
+
def __init__(self, context):
|
|
125
|
+
self.context = context
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class DocumentGeneratorSearchReplacePanelForm(AutoExtensibleForm, form.Form):
|
|
129
|
+
"""
|
|
130
|
+
DocumentGenerator Search & Replace control panel form
|
|
131
|
+
"""
|
|
132
|
+
implements(IFieldsAndContentProvidersForm)
|
|
133
|
+
|
|
134
|
+
schema = IDocumentGeneratorSearchReplacePanelSchema
|
|
135
|
+
label = _(u"Search & Replace")
|
|
136
|
+
description = _(u"Search & replace among all template directives in this Plone site templates")
|
|
137
|
+
|
|
138
|
+
# display the search result and replace form as content provider
|
|
139
|
+
contentProviders = ContentProviders()
|
|
140
|
+
contentProviders['search_result_provider'] = SearchResultProvider
|
|
141
|
+
# defining a contentProvider position is mandatory...
|
|
142
|
+
contentProviders['search_result_provider'].position = 2
|
|
143
|
+
|
|
144
|
+
def __init__(self, context, request):
|
|
145
|
+
self.is_previewing = False
|
|
146
|
+
self.results_table = OrderedDict()
|
|
147
|
+
super(DocumentGeneratorSearchReplacePanelForm, self).__init__(context, request)
|
|
148
|
+
|
|
149
|
+
@button.buttonAndHandler(_("Replace"), name="replace", condition=lambda form: form.can_replace())
|
|
150
|
+
def handle_replace(self, action): # pragma: no cover
|
|
151
|
+
data, errors = self.extractData()
|
|
152
|
+
if errors:
|
|
153
|
+
self.status = self.formErrorsMessage
|
|
154
|
+
return
|
|
155
|
+
self.perform_replacements(data)
|
|
156
|
+
return self.render()
|
|
157
|
+
|
|
158
|
+
@button.buttonAndHandler(_("Search"), name="search")
|
|
159
|
+
def handle_search(self, action): # pragma: no cover
|
|
160
|
+
data, errors = self.extractData()
|
|
161
|
+
if errors:
|
|
162
|
+
self.status = self.formErrorsMessage
|
|
163
|
+
return
|
|
164
|
+
self.perform_preview(data)
|
|
165
|
+
return self.render()
|
|
166
|
+
|
|
167
|
+
@button.buttonAndHandler(_("Cancel"), name="cancel")
|
|
168
|
+
def handle_cancel(self, action): # pragma: no cover
|
|
169
|
+
self.request.response.redirect(
|
|
170
|
+
"{context_url}/{view}".format(
|
|
171
|
+
context_url=self.context.absolute_url(), view="@@collective.documentgenerator-controlpanel"
|
|
172
|
+
)
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def can_replace(self):
|
|
176
|
+
# allow to perform a replace only if we performed a search first or if we are performing the replace
|
|
177
|
+
search_done = self.request.get('form.buttons.search', False)
|
|
178
|
+
replacing = self.request.get('selected_templates', False)
|
|
179
|
+
return search_done or replacing
|
|
180
|
+
|
|
181
|
+
def updateActions(self): # pragma: no cover
|
|
182
|
+
super(DocumentGeneratorSearchReplacePanelForm, self).updateActions()
|
|
183
|
+
self.actions["search"].addClass("context") # Make "Search" button primary
|
|
184
|
+
if self.request.get('selected_templates', False): # Must do a new new search before replacing again
|
|
185
|
+
self.actions["replace"].addClass("hidden")
|
|
186
|
+
|
|
187
|
+
def updateWidgets(self, prefix=None): # pragma: no cover
|
|
188
|
+
super(DocumentGeneratorSearchReplacePanelForm, self).updateWidgets(prefix)
|
|
189
|
+
self.widgets["replacements"].auto_append = False
|
|
190
|
+
|
|
191
|
+
@staticmethod
|
|
192
|
+
def get_selected_templates(form_data):
|
|
193
|
+
"""
|
|
194
|
+
Get selected templates from form_data
|
|
195
|
+
"""
|
|
196
|
+
uids = form_data["selected_templates"]
|
|
197
|
+
if 'all' in uids:
|
|
198
|
+
voc = AllPODTemplateWithFileVocabularyFactory()
|
|
199
|
+
uids = [brain.UID for brain in voc._get_all_pod_templates_with_file()]
|
|
200
|
+
templates = [uuidToObject(template_uuid) for template_uuid in uids]
|
|
201
|
+
return templates
|
|
202
|
+
|
|
203
|
+
def perform_replacements(self, form_data):
|
|
204
|
+
"""
|
|
205
|
+
Execute replacements action
|
|
206
|
+
"""
|
|
207
|
+
if len(form_data["replacements"]) == 0:
|
|
208
|
+
self.status = _("Nothing to replace.")
|
|
209
|
+
return
|
|
210
|
+
|
|
211
|
+
templates = self.get_selected_templates(form_data)
|
|
212
|
+
self.results_table = {}
|
|
213
|
+
|
|
214
|
+
with SearchAndReplacePODTemplates(templates) as replace:
|
|
215
|
+
for row in form_data["replacements"]:
|
|
216
|
+
row["replace_expr"] = row["replace_expr"] or ""
|
|
217
|
+
search_expr = row["search_expr"]
|
|
218
|
+
replace_expr = row["replace_expr"]
|
|
219
|
+
replace_results = replace.replace(search_expr, replace_expr, is_regex=row["is_regex"])
|
|
220
|
+
|
|
221
|
+
for template_uid, template_result in replace_results.items():
|
|
222
|
+
if not self.results_table.get(template_uid):
|
|
223
|
+
self.results_table[template_uid] = template_result
|
|
224
|
+
else:
|
|
225
|
+
self.results_table[template_uid].extend(template_result)
|
|
226
|
+
|
|
227
|
+
if len(self.results_table) == 0:
|
|
228
|
+
self.status = _("Nothing found.")
|
|
229
|
+
|
|
230
|
+
def perform_preview(self, form_data):
|
|
231
|
+
"""
|
|
232
|
+
Execute preview action
|
|
233
|
+
"""
|
|
234
|
+
if len(form_data["replacements"]) == 0:
|
|
235
|
+
self.status = _("Nothing to preview.")
|
|
236
|
+
return
|
|
237
|
+
templates = self.get_selected_templates(form_data)
|
|
238
|
+
|
|
239
|
+
self.results_table = {}
|
|
240
|
+
|
|
241
|
+
with SearchAndReplacePODTemplates(templates) as search_replace:
|
|
242
|
+
for row in form_data["replacements"]:
|
|
243
|
+
search_expr = row["search_expr"]
|
|
244
|
+
search_results = search_replace.search(search_expr, is_regex=row["is_regex"])
|
|
245
|
+
for template_uid, template_result in search_results.items():
|
|
246
|
+
if not self.results_table.get(template_uid):
|
|
247
|
+
self.results_table[template_uid] = template_result
|
|
248
|
+
else:
|
|
249
|
+
self.results_table[template_uid].extend(template_result)
|
|
250
|
+
self.is_previewing = True
|
|
251
|
+
|
|
252
|
+
if len(self.results_table) == 0:
|
|
253
|
+
self.status = _("Nothing found.")
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class DocumentGeneratorSearchReplace(ControlPanelFormWrapper):
|
|
257
|
+
"""
|
|
258
|
+
DocumentGenerator Search & Replace control panel
|
|
259
|
+
"""
|
|
260
|
+
|
|
261
|
+
form = DocumentGeneratorSearchReplacePanelForm
|