src/pyams_content/shared/blog/zmi/__init__.py
changeset 42 bcb01961928e
child 57 23ee41f44161
equal deleted inserted replaced
41:48aa4de90de2 42:bcb01961928e
       
     1 #
       
     2 # Copyright (c) 2008-2016 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 from datetime import datetime
       
    18 
       
    19 # import interfaces
       
    20 from pyams_content.interfaces import CREATE_CONTENT_PERMISSION
       
    21 from pyams_content.shared.blog.interfaces import IWfBlogPost, IBlogManager
       
    22 from pyams_i18n.interfaces import II18n, II18nManager
       
    23 from pyams_skin.interfaces.viewlet import IMenuHeader, IWidgetTitleViewletManager
       
    24 from pyams_skin.layer import IPyAMSLayer
       
    25 from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowInfo
       
    26 from pyams_zmi.interfaces.menu import IContentManagementMenu
       
    27 from pyams_zmi.layer import IAdminLayer
       
    28 
       
    29 # import packages
       
    30 from pyams_content.shared.blog.manager import BlogFolder
       
    31 from pyams_content.shared.common.zmi import SharedContentAddForm, SharedContentAJAXAddForm
       
    32 from pyams_pagelet.pagelet import pagelet_config
       
    33 from pyams_skin.interfaces import IContentTitle
       
    34 from pyams_skin.viewlet.toolbar import ToolbarAction
       
    35 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter, ContextRequestAdapter
       
    36 from pyams_utils.unicode import translate_string
       
    37 from pyams_utils.url import absolute_url
       
    38 from pyams_viewlet.viewlet import viewlet_config
       
    39 from pyramid.view import view_config
       
    40 from zope.interface import Interface
       
    41 from zope.lifecycleevent import ObjectCreatedEvent
       
    42 
       
    43 from pyams_content import _
       
    44 
       
    45 
       
    46 @adapter_config(context=(IWfBlogPost, IContentManagementMenu), provides=IMenuHeader)
       
    47 class BlogPostContentMenuHeader(ContextRequestAdapter):
       
    48     """Blog post content menu header adapter"""
       
    49 
       
    50     header = _("This blog post")
       
    51 
       
    52 
       
    53 @adapter_config(context=(IWfBlogPost, IPyAMSLayer, Interface), provides=IContentTitle)
       
    54 class BlogPostTitleAdapter(ContextRequestViewAdapter):
       
    55     """Blog post title adapter"""
       
    56 
       
    57     @property
       
    58     def title(self):
       
    59         translate = self.request.localizer.translate
       
    60         return translate(_("Blog post « {title} »")).format(
       
    61             title=II18n(self.context).query_attribute('title', request=self.request))
       
    62 
       
    63 
       
    64 @viewlet_config(name='add-shared-content.action', context=IBlogManager, layer=IAdminLayer, view=Interface,
       
    65                 manager=IWidgetTitleViewletManager, permission=CREATE_CONTENT_PERMISSION, weight=1)
       
    66 class BlogPostAddAction(ToolbarAction):
       
    67     """Blog post adding action"""
       
    68 
       
    69     label = _("Add blog post")
       
    70     url = 'add-shared-content.html'
       
    71     modal_target = True
       
    72 
       
    73 
       
    74 @pagelet_config(name='add-shared-content.html', context=IBlogManager, layer=IPyAMSLayer,
       
    75                 permission=CREATE_CONTENT_PERMISSION)
       
    76 class BlogPostAddForm(SharedContentAddForm):
       
    77     """Blog post add form"""
       
    78 
       
    79     legend = _("Add blog post")
       
    80     content_url = None
       
    81 
       
    82     def add(self, wf_content):
       
    83         # create shared content
       
    84         content = self.context.shared_content_factory()
       
    85         self.request.registry.notify(ObjectCreatedEvent(content))
       
    86         # check blog folders
       
    87         now = datetime.utcnow()
       
    88         year, month = now.strftime('%Y:%m').split(':')
       
    89         year_folder = self.context.get(year)
       
    90         if year_folder is None:
       
    91             year_folder = self.context[year] = BlogFolder()
       
    92         month_folder = year_folder.get(month)
       
    93         if month_folder is None:
       
    94             month_folder = year_folder[month] = BlogFolder()
       
    95         # check title language
       
    96         added = False
       
    97         i18n_manager = II18nManager(self.context)
       
    98         for lang in i18n_manager.get_languages():
       
    99             title = translate_string(wf_content.title.get(lang))
       
   100             if title:
       
   101                 content_name = translate_string(title, force_lower=True, spaces='-')
       
   102                 month_folder[content_name] = content
       
   103                 added = True
       
   104                 break
       
   105         # handle workflow
       
   106         if added:
       
   107             IWorkflowVersions(content).add_version(wf_content, None)
       
   108             IWorkflowInfo(wf_content).fire_transition('init')
       
   109             self.content_url = absolute_url(wf_content, self.request, 'admin.html')
       
   110 
       
   111     def nextURL(self):
       
   112         return self.content_url
       
   113 
       
   114 
       
   115 @view_config(name='add-shared-content.json', context=IBlogManager, request_type=IPyAMSLayer,
       
   116              permission=CREATE_CONTENT_PERMISSION, renderer='json', xhr=True)
       
   117 class BlogPostAJAXAddForm(SharedContentAJAXAddForm, BlogPostAddForm):
       
   118     """Blog post add form, JSON renderer"""