src/pyams_security/profile.py
changeset 50 a6d994c60822
child 58 5085503b72c2
equal deleted inserted replaced
49:e4fc19ce77fa 50:a6d994c60822
       
     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_security.interfaces import IPrincipalInfo
       
    20 from pyams_security.interfaces.profile import PUBLIC_PROFILE_KEY, IPublicProfile
       
    21 from pyams_utils.interfaces.site import ISiteRoot
       
    22 from pyams_utils.interfaces.tales import ITALESExtension
       
    23 from zope.annotation.interfaces import IAnnotations, IAttributeAnnotatable
       
    24 from zope.traversing.interfaces import ITraversable
       
    25 
       
    26 # import packages
       
    27 from persistent import Persistent
       
    28 from pyams_file.property import FileProperty
       
    29 from pyams_utils.adapter import adapter_config, ContextRequestAdapter
       
    30 from pyams_utils.request import check_request
       
    31 from pyams_utils.traversing import get_parent
       
    32 from pyramid.threadlocal import get_current_registry, get_current_request
       
    33 from zope.container.contained import Contained
       
    34 from zope.lifecycleevent import ObjectCreatedEvent
       
    35 from zope.interface import implementer, Interface
       
    36 from zope.location import locate
       
    37 
       
    38 
       
    39 @implementer(IPublicProfile, IAttributeAnnotatable)
       
    40 class PublicProfile(Persistent, Contained):
       
    41     """Public profile persistent class"""
       
    42 
       
    43     avatar = FileProperty(IPublicProfile['avatar'])
       
    44 
       
    45 
       
    46 @adapter_config(context=Interface, provides=IPublicProfile)
       
    47 def PublicProfileFactory(context):
       
    48     request = check_request()
       
    49     return IPublicProfile(request.principal)
       
    50 
       
    51 
       
    52 @adapter_config(context=IPrincipalInfo, provides=IPublicProfile)
       
    53 def PrincipalPublicProfileFactory(principal):
       
    54     """Principal public profile factory adapter"""
       
    55     annotations = IAnnotations(principal)
       
    56     profile = annotations.get(PUBLIC_PROFILE_KEY)
       
    57     if profile is None:
       
    58         profile = annotations[PUBLIC_PROFILE_KEY] = PublicProfile()
       
    59         get_current_registry().notify(ObjectCreatedEvent(profile))
       
    60         request = get_current_request()
       
    61         if request is not None:
       
    62             root = get_parent(request.context, ISiteRoot)
       
    63             locate(profile, root, '++profile++')
       
    64     return profile
       
    65 
       
    66 
       
    67 @adapter_config(name='profile', context=(Interface, Interface), provides=ITraversable)
       
    68 class ProfileTraverser(ContextRequestAdapter):
       
    69     """++profile++ namespace traverser"""
       
    70 
       
    71     def traverse(self, name, furtherpath=None):
       
    72         return IPublicProfile(self.request)
       
    73 
       
    74 
       
    75 @adapter_config(name='public_profile', context=(Interface, Interface), provides=ITALESExtension)
       
    76 class PublicProfileExtension(ContextRequestAdapter):
       
    77     """public_profile TALES extension"""
       
    78 
       
    79     def render(self, request=None):
       
    80         if request is None:
       
    81             request = self.request
       
    82         return IPublicProfile(request)