src/pyams_content/profile/admin.py
changeset 0 7c0001cacf8e
child 8 4a2b5dd3d69b
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.profile.interfaces import IAdminProfile, ADMIN_PROFILE_KEY
       
    20 from pyams_security.interfaces import IPrincipalInfo
       
    21 from zope.annotation.interfaces import IAnnotations, IAttributeAnnotatable
       
    22 
       
    23 # import packages
       
    24 from persistent import Persistent
       
    25 from pyams_utils.adapter import adapter_config
       
    26 from pyams_utils.request import check_request
       
    27 from pyramid.threadlocal import get_current_registry
       
    28 from zope.lifecycleevent import ObjectCreatedEvent
       
    29 from zope.interface import implementer, Interface
       
    30 from zope.schema.fieldproperty import FieldProperty
       
    31 
       
    32 
       
    33 @implementer(IAdminProfile, IAttributeAnnotatable)
       
    34 class AdminProfile(Persistent):
       
    35     """Admin profile persistent class"""
       
    36 
       
    37     table_page_length = FieldProperty(IAdminProfile['table_page_length'])
       
    38 
       
    39 
       
    40 @adapter_config(context=Interface, provides=IAdminProfile)
       
    41 def AdminProfileFactory(context):
       
    42     request = check_request()
       
    43     return IAdminProfile(request.principal)
       
    44 
       
    45 
       
    46 @adapter_config(context=IPrincipalInfo, provides=IAdminProfile)
       
    47 def PrincipalAdminProfileFactory(principal):
       
    48     """Principal admin profile factory adapter"""
       
    49     annotations = IAnnotations(principal)
       
    50     profile = annotations.get(ADMIN_PROFILE_KEY)
       
    51     if profile is None:
       
    52         profile = annotations[ADMIN_PROFILE_KEY] = AdminProfile()
       
    53         get_current_registry().notify(ObjectCreatedEvent(profile))
       
    54     return profile