diff -r 000000000000 -r 7c0001cacf8e src/pyams_content/profile/admin.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_content/profile/admin.py Thu Oct 08 13:37:29 2015 +0200 @@ -0,0 +1,54 @@ +# +# Copyright (c) 2008-2015 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# + +__docformat__ = 'restructuredtext' + + +# import standard library + +# import interfaces +from pyams_content.profile.interfaces import IAdminProfile, ADMIN_PROFILE_KEY +from pyams_security.interfaces import IPrincipalInfo +from zope.annotation.interfaces import IAnnotations, IAttributeAnnotatable + +# import packages +from persistent import Persistent +from pyams_utils.adapter import adapter_config +from pyams_utils.request import check_request +from pyramid.threadlocal import get_current_registry +from zope.lifecycleevent import ObjectCreatedEvent +from zope.interface import implementer, Interface +from zope.schema.fieldproperty import FieldProperty + + +@implementer(IAdminProfile, IAttributeAnnotatable) +class AdminProfile(Persistent): + """Admin profile persistent class""" + + table_page_length = FieldProperty(IAdminProfile['table_page_length']) + + +@adapter_config(context=Interface, provides=IAdminProfile) +def AdminProfileFactory(context): + request = check_request() + return IAdminProfile(request.principal) + + +@adapter_config(context=IPrincipalInfo, provides=IAdminProfile) +def PrincipalAdminProfileFactory(principal): + """Principal admin profile factory adapter""" + annotations = IAnnotations(principal) + profile = annotations.get(ADMIN_PROFILE_KEY) + if profile is None: + profile = annotations[ADMIN_PROFILE_KEY] = AdminProfile() + get_current_registry().notify(ObjectCreatedEvent(profile)) + return profile