src/pyams_content/shared/common/zmi/header.py
changeset 14 234db8f05928
parent 0 7c0001cacf8e
child 30 dd345161e0b6
equal deleted inserted replaced
13:35afd52c902b 14:234db8f05928
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     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
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
       
    12 from pyams_content.profile.interfaces import IAdminProfile
    12 
    13 
    13 __docformat__ = 'restructuredtext'
    14 __docformat__ = 'restructuredtext'
    14 
    15 
    15 
    16 
    16 # import standard library
    17 # import standard library
    29 from pyams_utils.date import format_datetime
    30 from pyams_utils.date import format_datetime
    30 from pyams_utils.registry import get_utility
    31 from pyams_utils.registry import get_utility
    31 from pyams_utils.traversing import get_parent
    32 from pyams_utils.traversing import get_parent
    32 from pyams_utils.url import absolute_url
    33 from pyams_utils.url import absolute_url
    33 from pyams_viewlet.viewlet import contentprovider_config
    34 from pyams_viewlet.viewlet import contentprovider_config
       
    35 from pyramid.decorator import reify
    34 from zope.interface import Interface
    36 from zope.interface import Interface
    35 
    37 
    36 from pyams_content import _
    38 from pyams_content import _
    37 
    39 
    38 
    40 
    59         translate = request.localizer.translate
    61         translate = request.localizer.translate
    60         workflow = IWorkflow(context)
    62         workflow = IWorkflow(context)
    61         versions = IWorkflowVersions(context)
    63         versions = IWorkflowVersions(context)
    62         state = IWorkflowState(context)
    64         state = IWorkflowState(context)
    63         self.version_id = state.version_id
    65         self.version_id = state.version_id
       
    66         # init state format
       
    67         state_format = translate(_("{state} by {principal}"))
       
    68         if state.state_urgency:
       
    69             state_format = state_format.replace('{state}',
       
    70                                                 '{state} <i class="fa fa-fw fa-exclamation-triangle txt-color-red"></i>');
       
    71         state_class = 'text-danger'  # if state.state in workflow.update_states else 'txt-color-text'
       
    72         state_format = state_format.replace('{state}',
       
    73                                             '<span class="{0}">{{state}}</span>'.format(state_class))
       
    74         # init principal format
       
    75         # principal_class = 'text-danger' if state.state_principal != owner else 'txt-color-text'
       
    76         principal_class = 'text-danger' \
       
    77             if state.state in (workflow.update_states | workflow.waiting_states) \
       
    78             else 'txt-color-text'
       
    79         state_format = state_format.replace('{principal}',
       
    80                                             '<span class="{0}">{{principal}}</span>'.format(principal_class))
       
    81         # get state label
    64         state_label = request.registry.queryAdapter(workflow, IWorkflowStateLabel, name=state.state)
    82         state_label = request.registry.queryAdapter(workflow, IWorkflowStateLabel, name=state.state)
    65         if state_label is None:
    83         if state_label is None:
    66             state_label = request.registry.queryAdapter(workflow, IWorkflowStateLabel)
    84             state_label = request.registry.queryAdapter(workflow, IWorkflowStateLabel)
    67         if state_label is None:
    85         if state_label is None:
    68             self.state = translate(_("{state} by {{principal}}")).format(
    86             self.state = state_format.format(state=translate(workflow.get_state_label(state.state)),
    69                 state=translate(workflow.get_state_label(state.state)))
    87                                              principal=security.get_principal(state.state_principal).title)
    70         else:
    88         else:
    71             self.state = state_label.get_label(context, request, format=False)
    89             self.state = state_format.format(state=state_label.get_label(context, request, format=False),
    72         principal_class = 'text-danger' if state.state_principal != owner else 'txt-color-text'
    90                                              principal=security.get_principal(state.state_principal).title)
    73         self.state = self.state.replace('{principal}',
       
    74                                         '<span class="{0}">{{principal}}</span>'.format(principal_class))
       
    75         state_class = 'text-danger' if state.state in workflow.update_states else None
       
    76         if state_class:
       
    77             self.state = '<span class="{state_class}">{state}</span>'.format(state_class=state_class,
       
    78                                                                              state=self.state)
       
    79         self.state = self.state.format(principal=security.get_principal(state.state_principal).title)
       
    80         self.state_date = translate(_("since {date}")).format(date=format_datetime(state.state_date, request=request))
    91         self.state_date = translate(_("since {date}")).format(date=format_datetime(state.state_date, request=request))
    81         if state.state not in workflow.update_states and versions.has_version(workflow.update_states):
    92         has_update_state = versions.has_version(workflow.update_states)
       
    93         if (state.state not in workflow.update_states and has_update_state) or \
       
    94            (state.state in workflow.retired_states and has_update_state):
    82             target = sorted(versions.get_versions(workflow.update_states),
    95             target = sorted(versions.get_versions(workflow.update_states),
    83                             key=lambda x: IWorkflowState(x).version_id,
    96                             key=lambda x: IWorkflowState(x).version_id)[-1]
    84                             reverse=True)[-1]
       
    85             self.version_link = {
    97             self.version_link = {
    86                 'css_class': 'text-danger',
    98                 'css_class': 'text-danger',
    87                 'href': absolute_url(target, request, 'admin.html'),
    99                 'href': absolute_url(target, request, 'admin.html'),
    88                 'title': translate(_("access new version"))
   100                 'title': translate(_("access new version"))
    89             }
   101             }
    90         elif state.state not in workflow.published_states and versions.has_version(workflow.published_states):
   102         elif state.state not in workflow.published_states and versions.has_version(workflow.published_states):
    91             target = sorted(versions.get_versions(workflow.published_states),
   103             target = sorted(versions.get_versions(workflow.published_states),
    92                             key=lambda x: IWorkflowState(x).version_id,
   104                             key=lambda x: IWorkflowState(x).version_id,
    93                             reverse=True)[-1]
   105                             reverse=True)[-1]
    94             self.version_link = {
   106             self.version_link = {
    95                 'css_class': 'txt-color-text',
   107                 'css_class': 'text-danger',
    96                 'href': absolute_url(target, request, 'admin.html'),
   108                 'href': absolute_url(target, request, 'admin.html'),
    97                 'title': translate(_("access published version"))
   109                 'title': translate(_("access published version"))
    98             }
   110             }
    99         else:
   111         else:
   100             self.version_link = None
   112             self.version_link = None
   102     @property
   114     @property
   103     def title(self):
   115     def title(self):
   104         tool = get_parent(self.context, ISharedTool)
   116         tool = get_parent(self.context, ISharedTool)
   105         return II18n(tool).query_attribute('title', request=self.request)
   117         return II18n(tool).query_attribute('title', request=self.request)
   106 
   118 
       
   119     @reify
       
   120     def hex_oid(self):
       
   121         return ISequentialIdInfo(self.context).hex_oid
       
   122 
       
   123     @property
       
   124     def favorite_content(self):
       
   125         profile = IAdminProfile(self.request.principal)
       
   126         return self.hex_oid in (profile.favorites or '')
       
   127 
   107 
   128 
   108 @contentprovider_config(name='content_header', context=IWfSharedContent, view=IInnerTabForm, layer=IPyAMSLayer)
   129 @contentprovider_config(name='content_header', context=IWfSharedContent, view=IInnerTabForm, layer=IPyAMSLayer)
   109 class SharedContentInnerPageHeaderContentProvider(object):
   130 class SharedContentInnerPageHeaderContentProvider(object):
   110     """Inner page header content provider"""
   131     """Inner page header content provider"""
   111 
   132