src/pyams_content/component/gallery/zmi/container.py
changeset 0 7c0001cacf8e
child 7 cbc55162b64e
equal deleted inserted replaced
-1:000000000000 0:7c0001cacf8e
       
     1 #
       
     2 # Copyright (c) 2008-2015 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 
       
    18 # import interfaces
       
    19 from pyams_content.component.gallery.interfaces import IGalleryContainerTarget, IGalleryContainer, \
       
    20     IGalleryLinksContainerTarget, IGalleryLinksContainer
       
    21 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
       
    22 from pyams_i18n.interfaces import II18n
       
    23 from pyams_skin.interfaces import IInnerPage, IPageHeader
       
    24 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
       
    25 from pyams_utils.interfaces.data import IObjectData
       
    26 from pyams_zmi.interfaces.menu import IPropertiesMenu
       
    27 from pyams_zmi.layer import IAdminLayer
       
    28 from z3c.table.interfaces import IColumn, IValues
       
    29 
       
    30 # import packages
       
    31 from pyams_content.component.gallery.zmi.widget import GalleryLinkSelectFieldWidget
       
    32 from pyams_content.shared.common.zmi import WfModifiedContentColumnMixin
       
    33 from pyams_form.form import AJAXEditForm
       
    34 from pyams_form.security import ProtectedFormObjectMixin
       
    35 from pyams_pagelet.pagelet import pagelet_config
       
    36 from pyams_skin.layer import IPyAMSLayer
       
    37 from pyams_skin.page import DefaultPageHeaderAdapter
       
    38 from pyams_skin.table import BaseTable, I18nColumn, TrashColumn, ActionColumn
       
    39 from pyams_skin.viewlet.menu import MenuItem
       
    40 from pyams_template.template import template_config
       
    41 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
       
    42 from pyams_utils.url import absolute_url
       
    43 from pyams_viewlet.viewlet import viewlet_config
       
    44 from pyams_zmi.form import AdminDialogEditForm
       
    45 from pyams_zmi.view import AdminView
       
    46 from pyramid.decorator import reify
       
    47 from pyramid.view import view_config
       
    48 from z3c.table.column import GetAttrColumn
       
    49 from z3c.form import field
       
    50 from zope.interface import implementer, alsoProvides
       
    51 
       
    52 from pyams_content import _
       
    53 
       
    54 
       
    55 @viewlet_config(name='galleries.menu', context=IGalleryContainerTarget, layer=IAdminLayer,
       
    56                 manager=IPropertiesMenu, permission=VIEW_SYSTEM_PERMISSION, weight=220)
       
    57 class GalleryContainerMenu(MenuItem):
       
    58     """Galleries container menu"""
       
    59 
       
    60     label = _("Images galleries...")
       
    61     icon_class = 'fa-picture-o'
       
    62     url = '#galleries.html'
       
    63 
       
    64 
       
    65 #
       
    66 # Galleries container views
       
    67 #
       
    68 
       
    69 @pagelet_config(name='galleries.html', context=IGalleryContainerTarget, layer=IPyAMSLayer,
       
    70                 permission=VIEW_SYSTEM_PERMISSION)
       
    71 @template_config(template='templates/container.pt', layer=IPyAMSLayer)
       
    72 @implementer(IInnerPage)
       
    73 class GalleryContainerView(AdminView):
       
    74     """Galleries container view"""
       
    75 
       
    76     title = _("Galleries list")
       
    77 
       
    78     def __init__(self, context, request):
       
    79         super(GalleryContainerView, self).__init__(context, request)
       
    80         self.galleries_table = GalleryContainerTable(context, request)
       
    81 
       
    82     def update(self):
       
    83         super(GalleryContainerView, self).update()
       
    84         self.galleries_table.update()
       
    85 
       
    86 
       
    87 class GalleryContainerTable(BaseTable):
       
    88     """Galleries container table"""
       
    89 
       
    90     hide_header = True
       
    91     cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight'}
       
    92 
       
    93     def __init__(self, context, request):
       
    94         super(GalleryContainerTable, self).__init__(context, request)
       
    95         self.object_data = {'ams-widget-toggle-button': 'false'}
       
    96         alsoProvides(self, IObjectData)
       
    97 
       
    98     @property
       
    99     def data_attributes(self):
       
   100         attributes = super(GalleryContainerTable, self).data_attributes
       
   101         attributes['table'] = {'data-ams-location': absolute_url(IGalleryContainer(self.context), self.request),
       
   102                                'data-ams-datatable-sort': 'false',
       
   103                                'data-ams-datatable-pagination': 'false'}
       
   104         return attributes
       
   105 
       
   106     @reify
       
   107     def values(self):
       
   108         return list(super(GalleryContainerTable, self).values)
       
   109 
       
   110     def render(self):
       
   111         if not self.values:
       
   112             translate = self.request.localizer.translate
       
   113             return translate(_("No currently defined gallery."))
       
   114         return super(GalleryContainerTable, self).render()
       
   115 
       
   116 
       
   117 @adapter_config(name='name', context=(IGalleryContainerTarget, IPyAMSLayer, GalleryContainerTable), provides=IColumn)
       
   118 class GalleryContainerNameColumn(I18nColumn, WfModifiedContentColumnMixin, GetAttrColumn):
       
   119     """Galleries container name column"""
       
   120 
       
   121     _header = _("Title")
       
   122 
       
   123     weight = 10
       
   124 
       
   125     def getValue(self, obj):
       
   126         return II18n(obj).query_attribute('title', request=self.request)
       
   127 
       
   128 
       
   129 @adapter_config(name='count', context=(IGalleryContainerTarget, IPyAMSLayer, GalleryContainerTable), provides=IColumn)
       
   130 class GalleryContainerCountColumn(I18nColumn, GetAttrColumn):
       
   131     """Gallery container images counter column"""
       
   132 
       
   133     _header = _("Images")
       
   134 
       
   135     weight = 20
       
   136 
       
   137     def getValue(self, obj):
       
   138         return len(obj)
       
   139 
       
   140 
       
   141 @adapter_config(name='manage', context=(IGalleryContainerTarget, IPyAMSLayer, GalleryContainerTable), provides=IColumn)
       
   142 class GalleryContainerManageColumn(ActionColumn):
       
   143     """Gallery container manage column"""
       
   144 
       
   145     icon_class = 'fa fa-fw fa-camera'
       
   146     icon_hint = _("Display gallery contents")
       
   147 
       
   148     url = 'contents.html'
       
   149     target = None
       
   150     modal_target = True
       
   151 
       
   152     weight = 30
       
   153 
       
   154 
       
   155 @adapter_config(name='trash', context=(IGalleryContainerTarget, IPyAMSLayer, GalleryContainerTable), provides=IColumn)
       
   156 class GalleryContainerTrashColumn(ProtectedFormObjectMixin, TrashColumn):
       
   157     """Galleries container trash column"""
       
   158 
       
   159 
       
   160 @adapter_config(context=(IGalleryContainerTarget, IPyAMSLayer, GalleryContainerTable), provides=IValues)
       
   161 class GalleryContainerValues(ContextRequestViewAdapter):
       
   162     """Galleries container values"""
       
   163 
       
   164     @property
       
   165     def values(self):
       
   166         return IGalleryContainer(self.context).values()
       
   167 
       
   168 
       
   169 @adapter_config(context=(IGalleryContainerTarget, IPyAMSLayer, GalleryContainerView), provides=IPageHeader)
       
   170 class GalleryHeaderAdapter(DefaultPageHeaderAdapter):
       
   171     """Galleries container header adapter"""
       
   172 
       
   173     back_url = '#properties.html'
       
   174     icon_class = 'fa fa-fw fa-picture-o'
       
   175 
       
   176 
       
   177 #
       
   178 # Galleries links edit form
       
   179 #
       
   180 
       
   181 @pagelet_config(name='gallery-links.html', context=IGalleryLinksContainerTarget, layer=IPyAMSLayer,
       
   182                 permission=VIEW_SYSTEM_PERMISSION)
       
   183 class GalleryLinksContainerLinksEditForm(AdminDialogEditForm):
       
   184     """Galleries links container edit form"""
       
   185 
       
   186     legend = _("Edit galleries links")
       
   187 
       
   188     fields = field.Fields(IGalleryLinksContainer)
       
   189     fields['galleries'].widgetFactory = GalleryLinkSelectFieldWidget
       
   190 
       
   191     ajax_handler = 'gallery-links.json'
       
   192     edit_permission = MANAGE_CONTENT_PERMISSION
       
   193 
       
   194 
       
   195 @view_config(name='gallery-links.json', context=IGalleryLinksContainerTarget, request_type=IPyAMSLayer,
       
   196              permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
       
   197 class GalleryLinksContainerAJAXEditForm(AJAXEditForm, GalleryLinksContainerLinksEditForm):
       
   198     """Galleries links container edit form, JSON renderer"""