Added "content_url" property
authorThierry Florac <thierry.florac@onf.fr>
Fri, 13 Apr 2018 12:23:58 +0200
changeset 534 09b34ca38bf1
parent 533 e0708738c26e
child 535 d01efcb4d332
Added "content_url" property
src/pyams_content/shared/common/__init__.py
src/pyams_content/shared/common/interfaces/__init__.py
src/pyams_content/shared/common/zmi/__init__.py
src/pyams_content/shared/common/zmi/properties.py
--- a/src/pyams_content/shared/common/__init__.py	Fri Apr 13 10:28:38 2018 +0200
+++ b/src/pyams_content/shared/common/__init__.py	Fri Apr 13 12:23:58 2018 +0200
@@ -123,9 +123,11 @@
     guests = RolePrincipalsFieldProperty(IWfSharedContentRoles['guests'])
 
     content_type = None
+    content_name = None
 
     title = FieldProperty(IWfSharedContent['title'])
     short_name = FieldProperty(IWfSharedContent['short_name'])
+    content_url = FieldProperty(IWfSharedContent['content_url'])
     creator = FieldProperty(IWfSharedContent['creator'])
     modifiers = FieldProperty(IWfSharedContent['modifiers'])
     last_modifier = FieldProperty(IWfSharedContent['last_modifier'])
--- a/src/pyams_content/shared/common/interfaces/__init__.py	Fri Apr 13 10:28:38 2018 +0200
+++ b/src/pyams_content/shared/common/interfaces/__init__.py	Fri Apr 13 12:23:58 2018 +0200
@@ -112,6 +112,12 @@
     content_type = Attribute("Content data type")
     content_name = Attribute("Content name")
 
+    content_url = TextLine(title=_("Content URL"),
+                           description=_("URL used to access this content; this is important for SEO and "
+                                         "should include most important words describing content; spaces and "
+                                         "underscores will be automatically replaced by hyphens"),
+                           required=True)
+
     creator = Principal(title=_("Version creator"),
                         description=_("Name of content's version creator. "
                                       "The creator of the first version is also it's owner."),
--- a/src/pyams_content/shared/common/zmi/__init__.py	Fri Apr 13 10:28:38 2018 +0200
+++ b/src/pyams_content/shared/common/zmi/__init__.py	Fri Apr 13 12:23:58 2018 +0200
@@ -24,7 +24,7 @@
 from pyams_content.shared.common.interfaces import IWfSharedContent, ISharedContent, IBaseSharedTool, \
     IManagerRestrictions
 from pyams_form.interfaces.form import IFormContextPermissionChecker, IWidgetsPrefixViewletsManager
-from pyams_i18n.interfaces import II18n, II18nManager
+from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
 from pyams_sequence.interfaces import ISequentialIdInfo
 from pyams_skin.interfaces import IContentTitle
 from pyams_skin.interfaces.container import ITable, ITableElementEditor
@@ -50,7 +50,7 @@
 from pyams_utils.registry import get_utility
 from pyams_utils.request import check_request
 from pyams_utils.traversing import get_parent
-from pyams_utils.url import absolute_url
+from pyams_utils.url import absolute_url, generate_url
 from pyams_viewlet.viewlet import viewlet_config, Viewlet
 from pyams_workflow.versions import WorkflowHistoryItem
 from pyams_zmi.form import AdminDialogAddForm
@@ -95,6 +95,8 @@
         content.creator = self.request.principal.id
         content.owner = self.request.principal.id
         content.short_name = content.title.copy()
+        lang = get_utility(INegotiator).server_language
+        content.content_url = generate_url(content.title.get(lang, ''))
         # init content languages
         languages = II18nManager(self.context).languages
         if languages:
--- a/src/pyams_content/shared/common/zmi/properties.py	Fri Apr 13 10:28:38 2018 +0200
+++ b/src/pyams_content/shared/common/zmi/properties.py	Fri Apr 13 12:23:58 2018 +0200
@@ -12,6 +12,9 @@
 from pyramid.events import subscriber
 from z3c.form.interfaces import IDataExtractedEvent, HIDDEN_MODE
 
+from pyams_skin.event import get_json_widget_refresh_event
+from pyams_utils.url import generate_url
+
 __docformat__ = 'restructuredtext'
 
 
@@ -79,7 +82,8 @@
 
     legend = _("Content properties")
 
-    fields = field.Fields(IWfSharedContent).select('title', 'short_name', 'description', 'notepad')
+    fields = field.Fields(IWfSharedContent).select('title', 'short_name', 'content_url',
+                                                   'description', 'notepad')
     fields['title'].widgetFactory = I18nSEOTextLineFieldWidget
 
     ajax_handler = 'properties.json'
@@ -97,7 +101,9 @@
 @subscriber(IDataExtractedEvent, form_selector=SharedContentPropertiesEditForm)
 def handle_content_properties_data_extraction(event):
     """Automatically set short_name as title"""
-    event.data['short_name'] = event.data['title']
+    data = event.data
+    data['short_name'] = data['title'].copy()
+    data['content_url'] = generate_url(data['content_url'])
 
 
 @view_config(name='properties.json', context=IWfSharedContent, request_type=IPyAMSLayer,
@@ -106,14 +112,20 @@
     """Shared content properties edit form, JSON renderer"""
 
     def get_ajax_output(self, changes):
-        wf_changes = changes.get(IBaseContent, ())
-        if 'title' in wf_changes:
+        updated = changes.get(IBaseContent, ())
+        if 'title' in updated:
             return {
                 'status': 'reload',
                 'message': self.request.localizer.translate(self.successMessage)
             }
         else:
-            return super(SharedContentPropertiesAJAXEditForm, self).get_ajax_output(changes)
+            output = super(SharedContentPropertiesAJAXEditForm, self).get_ajax_output(changes)
+            updated = changes.get(IWfSharedContent, ())
+            if 'content_url' in updated:
+                output.setdefault('events', []).append(
+                    get_json_widget_refresh_event(self.context, self.request,
+                                                  SharedContentPropertiesEditForm, 'content_url'))
+            return output
 
 
 @adapter_config(context=(IWfSharedContent, IAdminLayer, SharedContentPropertiesEditForm), provides=IPageHeader)