Use new thesaurus terms tree widget to display themes and tags
authorThierry Florac <thierry.florac@onf.fr>
Tue, 10 Jul 2018 15:47:30 +0200
changeset 825 3f5684390091
parent 824 5a56ac57d94e
child 826 eb16b1127e06
Use new thesaurus terms tree widget to display themes and tags
src/pyams_content/component/theme/zmi/__init__.py
src/pyams_content/component/theme/zmi/templates/themes-info.pt
--- a/src/pyams_content/component/theme/zmi/__init__.py	Tue Jul 10 14:19:56 2018 +0200
+++ b/src/pyams_content/component/theme/zmi/__init__.py	Tue Jul 10 15:47:30 2018 +0200
@@ -9,7 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_content.component.theme import ITagsTarget, ITagsInfo, ITagsManagerTarget, ITagsManager
 
 __docformat__ = 'restructuredtext'
 
@@ -17,10 +16,11 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.theme.interfaces import IThemesTarget, IThemesInfo, IThemesManagerTarget, IThemesManager
+from pyams_content.component.theme import ITagsTarget, ITagsInfo, ITagsManager, IThemesTarget, IThemesInfo, \
+    IThemesManagerTarget, IThemesManager
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
 from pyams_form.interfaces.form import IWidgetForm
-from pyams_skin.interfaces import IInnerPage, IPageHeader
+from pyams_skin.interfaces import IInnerPage, IPageHeader, IDialog
 from pyams_skin.layer import IPyAMSLayer
 from pyams_thesaurus.interfaces.thesaurus import IThesaurus
 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
@@ -29,15 +29,15 @@
 
 # import packages
 from pyams_content.shared.common.zmi import WfSharedContentHeaderAdapter
-from pyams_form.form import ajax_config
+from pyams_form.form import ajax_config, AJAXEditForm, EditForm
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.viewlet.menu import MenuItem
-from pyams_template.template import template_config
+from pyams_thesaurus.widget import ThesaurusTermsTreeFieldWidget
 from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import query_utility
 from pyams_utils.traversing import get_parent
 from pyams_viewlet.viewlet import viewlet_config
-from pyams_zmi.form import AdminEditForm
+from pyams_zmi.form import AdminEditForm, AdminDialogEditForm
 from z3c.form import field
 from zope.interface import implementer
 
@@ -59,7 +59,6 @@
 
 
 @pagelet_config(name='tags.html', context=ITagsTarget, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
-@template_config(template='templates/themes-info.pt', layer=IPyAMSLayer)
 @ajax_config(name='tags.json', context=ITagsTarget, layer=IPyAMSLayer, permission=MANAGE_CONTENT_PERMISSION)
 @implementer(IWidgetForm, IInnerPage)
 class TagsEditForm(AdminEditForm):
@@ -67,8 +66,11 @@
 
     legend = _("Content tags")
 
+    label_css_class = 'control-label hidden'
+    input_css_class = 'col-md-12'
+
     fields = field.Fields(ITagsInfo)
-    fieldname = 'tags'
+    fields['tags'].widgetFactory = ThesaurusTermsTreeFieldWidget
 
     def __init__(self, context, request):
         super(TagsEditForm, self).__init__(context, request)
@@ -120,50 +122,47 @@
     url = '#themes.html'
 
 
-@pagelet_config(name='themes.html', context=IThemesTarget, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
-@template_config(template='templates/themes-info.pt', layer=IPyAMSLayer)
-@ajax_config(name='themes.json', context=IThemesTarget, layer=IPyAMSLayer, permission=MANAGE_CONTENT_PERMISSION)
-@implementer(IWidgetForm, IInnerPage)
-class ThemesEditForm(AdminEditForm):
-    """Themes edit form"""
+class BaseThemesEditForm(EditForm):
+    """Base themes edit form"""
 
     legend = _("Content themes")
 
-    fields = field.Fields(IThemesInfo)
-    fieldname = 'themes'
+    label_css_class = 'control-label hidden'
+    input_css_class = 'col-md-12'
 
-    def __init__(self, context, request):
-        super(ThemesEditForm, self).__init__(context, request)
-        target = get_parent(self.context, IThemesManagerTarget)
-        manager = IThemesManager(target)
-        self.thesaurus_name = manager.thesaurus_name
-        self.extract_name = manager.extract_name
+    fields = field.Fields(IThemesInfo)
+    fields['themes'].widgetFactory = ThesaurusTermsTreeFieldWidget
+
+    edit_permission = MANAGE_CONTENT_PERMISSION
 
     def updateWidgets(self, prefix=None):
-        super(ThemesEditForm, self).updateWidgets(prefix)
-        widget = self.widgets['themes']
-        widget.thesaurus_name = self.thesaurus_name
-        widget.extract_name = self.extract_name
-
-    @property
-    def top_terms(self):
-        thesaurus = query_utility(IThesaurus, name=self.thesaurus_name)
-        if thesaurus is not None:
-            return sorted(thesaurus.get_top_terms(extract=self.extract_name),
-                          key=lambda x: x.label)
-        else:
-            return ()
-
-    def get_subterms(self, term):
-        for subterm in term.specifics:
-            if (not self.extract_name) or (self.extract_name in subterm.extracts):
-                yield subterm
-                for another in self.get_subterms(subterm):
-                    yield another
+        super(BaseThemesEditForm, self).updateWidgets(prefix)
+        if 'themes' in self.widgets:
+            widget = self.widgets['themes']
+            target = get_parent(self.context, IThemesManagerTarget)
+            manager = IThemesManager(target)
+            widget.thesaurus_name = manager.thesaurus_name
+            widget.extract_name = manager.extract_name
 
 
-@adapter_config(context=(IThemesTarget, IAdminLayer, ThemesEditForm), provides=IPageHeader)
+@adapter_config(context=(IThemesTarget, IAdminLayer, BaseThemesEditForm), provides=IPageHeader)
 class ThemesHeaderAdapter(WfSharedContentHeaderAdapter):
     """Shared content themes header adapter"""
 
     icon_class = 'fa fa-fw fa-tags'
+
+
+@pagelet_config(name='themes.html', context=IThemesTarget, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
+@ajax_config(name='themes.json', context=IThemesTarget, layer=IPyAMSLayer, base=AJAXEditForm)
+@implementer(IWidgetForm, IInnerPage)
+class ThemesEditForm(BaseThemesEditForm, AdminEditForm):
+    """Themes edit form"""
+
+
+@pagelet_config(name='themes-dialog.html', context=IThemesTarget, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
+@ajax_config(name='themes-dialog.json', context=IThemesTarget, layer=IPyAMSLayer, base=AJAXEditForm)
+@implementer(IDialog)
+class DialogThemesEditForm(BaseThemesEditForm, AdminDialogEditForm):
+    """Dialog themes edit form"""
+
+    dialog_class = 'modal-large'
--- a/src/pyams_content/component/theme/zmi/templates/themes-info.pt	Tue Jul 10 14:19:56 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,109 +0,0 @@
-<div class="ams-widget">
-	<header>
-		<span tal:condition="view.widget_icon_class | nothing"
-			  class="widget-icon"><i tal:attributes="class view.widget_icon_class"></i>
-		</span>
-		<h2 tal:content="view.legend"></h2>
-		<tal:var content="structure provider:pyams.widget_title" />
-		<tal:var content="structure provider:pyams.toolbar" />
-	</header>
-	<div class="widget-body no-padding">
-		<div tal:define="prefix provider:form_prefix"
-			 tal:replace="structure prefix">Form prefix</div>
-		<tal:var content="structure provider:content_help" />
-		<form method="post"
-			  data-async
-			  tal:attributes="id view.id;
-							  name view.name;
-							  action view.get_form_action();
-							  method view.method;
-							  enctype view.enctype;
-							  acceptCharset view.acceptCharset;
-							  accept view.accept;
-							  autocomplete view.autocomplete;
-							  class view.css_class;
-							  data-ams-data tales:object_data(view);
-							  data-ams-form-handler view.get_ajax_handler() | nothing;
-							  data-ams-form-options view.get_form_options() | nothing;
-							  data-ams-form-submit-target view.form_target | nothing;
-							  data-ams-form-download-target view.download_target | nothing;
-							  data-ams-warn-on-change view.warn_on_change;">
-			<div class="modal-viewport">
-				<fieldset>
-					<div class="widgets-prefix"
-						 tal:define="prefix provider:widgets_prefix"
-						 tal:condition="prefix"
-						 tal:content="structure prefix">Widgets prefix</div>
-					<tal:loop repeat="term view.top_terms">
-						<div class="col col-xs-6 col-sm-4 col-md-4 col-lg-3">
-							<div class="ams-widget" data-ams-widget-toggle-button="false"
-								 tal:attributes="id string:them_${repeat['term'].index()}">
-								<header class="no-margin"><h2 tal:content="term.label"></h2></header>
-								<div class="widget-body no-padding viewport-y viewport-200 viewport-x-none"
-									 style="height: 200px; width: calc(100% - 2px);">
-									<tal:loop repeat="subterm view.get_subterms(term)">
-										<div tal:define="padding (subterm.level - 1) * 20"
-											 tal:attributes="style string:padding-left: ${padding}px;; line-height: 1em;;">
-											<label class="checkbox"
-												   tal:define="published subterm.status == 'published'"
-												   tal:omit-tag="not:published">
-												<input type="checkbox" name="form.widgets.themes:list"
-													   tal:condition="published"
-													   tal:attributes="id string:term_${subterm.label};
-																	   name string:${view.widgets[view.fieldname].name}:list;
-																	   value subterm.label;
-																	   checked subterm.label in view.widgets[view.fieldname].value" />
-												<i></i>
-												<div tal:attributes="class '' if published else 'bold margin-top-5'"
-													 tal:content="subterm.label"></div>
-											</label>
-										</div>
-									</tal:loop>
-								</div>
-							</div>
-						</div>
-					</tal:loop>
-					<div class="widgets-suffix"
-						 tal:define="suffix provider:widgets_suffix"
-						 tal:condition="suffix"
-						 tal:content="structure suffix">Widgets suffix</div>
-					<div class="subforms"
-						 tal:condition="view.subforms">
-						<fieldset tal:define="title view.subforms_legend"
-								  tal:omit-tag="not:title">
-							<legend tal:condition="title" tal:content="title" i18n:translate="">Title</legend>
-							<tal:loop repeat="subform view.subforms">
-								<tal:var replace="structure subform.render()" />
-							</tal:loop>
-						</fieldset>
-					</div>
-					<div class="tabforms"
-						 tal:condition="view.tabforms">
-						<ul class="nav nav-tabs">
-							<li tal:repeat="tabform view.tabforms"
-								tal:attributes="class 'small {active} {errors}'.format(active='active' if repeat['tabform'].start() else '',
-																					   errors='state-error' if tabform.widgets.errors else '')">
-								<a data-toggle="tab"
-								   tal:attributes="href string:#${tabform.id}"
-								   tal:content="tabform.tab_label" i18n:translate="">Tab label</a>
-							</li>
-						</ul>
-						<div class="tab-content">
-							<div class="tab-pane fade in"
-								 tal:repeat="tabform view.tabforms"
-								 tal:attributes="id tabform.id;
-												 class 'tab-pane {active} fade in'.format(active='active' if repeat['tabform'].start() else '');"
-								 tal:content="structure tabform.render()"></div>
-						</div>
-					</div>
-				</fieldset>
-			</div>
-			<footer tal:condition="view.actions and (view.is_dialog or (view.mode == 'input'))">
-				<button tal:repeat="action view.actions.values()"
-						tal:replace="structure action.render()">Action</button>
-			</footer>
-		</form>
-		<div tal:define="prefix provider:form_suffix"
-			 tal:replace="structure prefix">Form suffix</div>
-	</div>
-</div>