Refactored blog post add form
authorThierry Florac <thierry.florac@onf.fr>
Fri, 23 Mar 2018 16:21:51 +0100
changeset 498 a5fcbde961b9
parent 497 32f7f9950df3
child 499 d94366d76e08
Refactored blog post add form
src/pyams_content/shared/blog/zmi/__init__.py
--- a/src/pyams_content/shared/blog/zmi/__init__.py	Fri Mar 23 16:21:19 2018 +0100
+++ b/src/pyams_content/shared/blog/zmi/__init__.py	Fri Mar 23 16:21:51 2018 +0100
@@ -32,9 +32,12 @@
 from pyams_skin.interfaces import IContentTitle
 from pyams_skin.viewlet.toolbar import ToolbarAction
 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter, ContextRequestAdapter
+from pyams_utils.traversing import get_parent
 from pyams_utils.unicode import translate_string
 from pyams_utils.url import absolute_url
 from pyams_viewlet.viewlet import viewlet_config
+from pyramid.decorator import reify
+from pyramid.path import DottedNameResolver
 from pyramid.view import view_config
 from zope.interface import Interface
 from zope.lifecycleevent import ObjectCreatedEvent
@@ -78,10 +81,28 @@
     legend = _("Add blog post")
     content_url = None
 
-    def add(self, wf_content):
-        # create shared content
-        content = self.context.topic_content_factory()
-        self.request.registry.notify(ObjectCreatedEvent(content))
+    @reify
+    def content_factory(self):
+        registry = self.request.registry
+        factory = registry.settings.get('pyams_content.config.blogpost_factory')
+        if factory:
+            factory = DottedNameResolver().resolve(factory)
+        else:
+            manager = get_parent(self.context, IBlogManager)
+            factory = manager.topic_content_factory
+        return factory
+
+    def create(self, data):
+        return self.content_factory.content_class()
+
+    def update_content(self, content, data):
+        data = data.get(self, data)
+        # initialize content fields
+        content.title = data['title']
+        content.short_name = content.title.copy()
+        content.notepad = data.get('notepad')
+        content.creator = self.request.principal.id
+        content.owner = self.request.principal.id
         # check blog folders
         now = datetime.utcnow()
         year, month = now.strftime('%Y:%m').split(':')
@@ -91,24 +112,25 @@
         month_folder = year_folder.get(month)
         if month_folder is None:
             month_folder = year_folder[month] = self.context.folder_factory()
-        # check title language
-        added = False
-        i18n_manager = II18nManager(self.context)
-        for lang in i18n_manager.get_languages():
-            title = translate_string(wf_content.title.get(lang))
-            if title:
-                content_name = translate_string(title, force_lower=True, spaces='-')
-                month_folder[content_name] = content
-                added = True
-                break
-        # handle workflow
-        if added:
-            IWorkflowVersions(content).add_version(wf_content, None)
-            IWorkflowInfo(wf_content).fire_transition('init')
-            self.content_url = absolute_url(wf_content, self.request, 'admin')
+        # update languages
+        languages = II18nManager(self.context).languages
+        if languages:
+            II18nManager(content).languages = languages.copy()
+        # create content
+        wf_parent = self.content_factory()
+        self.request.registry.notify(ObjectCreatedEvent(wf_parent))
+        # add content to blog folder
+        title = II18n(content).query_attribute('title', request=self.request)
+        month_folder[translate_string(title, spaces='-')] = wf_parent
+        IWorkflowVersions(wf_parent).add_version(content, None)
+        IWorkflowInfo(content).fire_transition('init', comment=content.notepad)
+        self.__target = content
+
+    def add(self, content):
+        pass
 
     def nextURL(self):
-        return self.content_url
+        return absolute_url(self.__target, self.request, 'admin')
 
 
 @view_config(name='add-shared-content.json', context=IBlogManager, request_type=IPyAMSLayer,