src/pyams_content/shared/view/theme.py
changeset 829 f933926ed0a1
parent 800 2b5460ecb254
child 1090 d6d041577ae0
equal deleted inserted replaced
828:bf12603398b2 829:f933926ed0a1
     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.component.theme import ICollectionsInfo
    12 
    13 
    13 __docformat__ = 'restructuredtext'
    14 __docformat__ = 'restructuredtext'
    14 
    15 
    15 
    16 
    16 # import standard library
    17 # import standard library
    17 
    18 
    18 # import interfaces
    19 # import interfaces
    19 from hypatia.interfaces import ICatalog
    20 from hypatia.interfaces import ICatalog
    20 from pyams_content.component.theme.interfaces import ITagsInfo, IThemesInfo
    21 from pyams_content.component.theme.interfaces import ITagsInfo, IThemesInfo
    21 from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewThemesSettings, \
    22 from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewThemesSettings, \
    22     IViewQueryParamsExtension, VIEW_THEMES_SETTINGS_KEY, IViewTagsSettings, VIEW_TAGS_SETTINGS_KEY
    23     IViewQueryParamsExtension, VIEW_THEMES_SETTINGS_KEY, IViewTagsSettings, VIEW_TAGS_SETTINGS_KEY, \
       
    24     IViewCollectionsSettings, VIEW_COLLECTIONS_SETTINGS_KEY
    23 from zope.intid.interfaces import IIntIds
    25 from zope.intid.interfaces import IIntIds
    24 
    26 
    25 # import packages
    27 # import packages
    26 from hypatia.query import Any
    28 from hypatia.query import Any
    27 from persistent import Persistent
    29 from persistent import Persistent
   139         # check themes
   141         # check themes
   140         themes = settings.get_themes_index(context)
   142         themes = settings.get_themes_index(context)
   141         if themes:
   143         if themes:
   142             params = and_(params, Any(catalog['themes'], themes))
   144             params = and_(params, Any(catalog['themes'], themes))
   143         return params
   145         return params
       
   146 
       
   147 
       
   148 #
       
   149 # Collections management
       
   150 #
       
   151 
       
   152 @implementer(IViewCollectionsSettings)
       
   153 class ViewCollectionsSettings(Persistent, Contained):
       
   154     """View collections settings"""
       
   155 
       
   156     select_context_collections = FieldProperty(IViewCollectionsSettings['select_context_collections'])
       
   157     collections = FieldProperty(IViewCollectionsSettings['collections'])
       
   158 
       
   159     @property
       
   160     def is_using_context(self):
       
   161         return self.select_context_collections
       
   162 
       
   163     def get_collections(self, context):
       
   164         collections = set()
       
   165         if self.select_context_collections:
       
   166             collections_info = ICollectionsInfo(context, None)
       
   167             if collections_info is not None:
       
   168                 collections |= set(collections_info.collections or ())
       
   169         if self.collections:
       
   170             collections |= set(self.collections)
       
   171         return collections
       
   172 
       
   173     def get_collections_index(self, context):
       
   174         intids = get_utility(IIntIds)
       
   175         return [intids.register(term) for term in self.get_collections(context)]
       
   176 
       
   177 
       
   178 @adapter_config(context=IWfView, provides=IViewCollectionsSettings)
       
   179 @adapter_config(name='collections', context=IWfView, provides=IViewSettings)
       
   180 def view_collections_settings_factory(view):
       
   181     """View collections settings factory"""
       
   182     return get_annotation_adapter(view, VIEW_COLLECTIONS_SETTINGS_KEY, ViewCollectionsSettings,
       
   183                                   name='++view:collections++')
       
   184 
       
   185 
       
   186 @adapter_config(name='collections', context=IWfView, provides=IViewQueryParamsExtension)
       
   187 class ViewCollectionsQueryParamsExtension(ContextAdapter):
       
   188     """View collections query params extension"""
       
   189 
       
   190     weight = 54
       
   191 
       
   192     def get_params(self, context):
       
   193         catalog = get_utility(ICatalog)
       
   194         settings = IViewCollectionsSettings(self.context)
       
   195         params = None
       
   196         # check collections
       
   197         collections = settings.get_collections_index(context)
       
   198         if collections:
       
   199             params = and_(params, Any(catalog['collections'], collections))
       
   200         return params