Updated annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Mon, 28 May 2018 14:05:40 +0200
changeset 126 18711a1f3140
parent 125 44639c19ec9b
child 127 b111497f093d
Updated annotations adapters
src/pyams_security/interfaces/profile.py
src/pyams_security/notification.py
src/pyams_security/plugin/social.py
src/pyams_security/profile.py
src/pyams_security/security.py
--- a/src/pyams_security/interfaces/profile.py	Wed May 23 15:09:56 2018 +0200
+++ b/src/pyams_security/interfaces/profile.py	Mon May 28 14:05:40 2018 +0200
@@ -19,7 +19,7 @@
 
 # import packages
 from pyams_file.schema import ThumbnailImageField
-from zope.interface import Interface
+from zope.annotation.interfaces import IAttributeAnnotatable
 
 from pyams_security import _
 
@@ -27,7 +27,7 @@
 PUBLIC_PROFILE_KEY = 'pyams_security.public_profile'
 
 
-class IPublicProfile(Interface):
+class IPublicProfile(IAttributeAnnotatable):
     """User public profile preferences"""
 
     avatar = ThumbnailImageField(title=_("Profile's avatar"),
--- a/src/pyams_security/notification.py	Wed May 23 15:09:56 2018 +0200
+++ b/src/pyams_security/notification.py	Mon May 28 14:05:40 2018 +0200
@@ -18,14 +18,11 @@
 # import interfaces
 from pyams_security.interfaces import ISecurityManager
 from pyams_security.interfaces.notification import INotificationSettings
-from zope.annotation.interfaces import IAnnotations
 
 # import packages
 from persistent import Persistent
-from pyams_utils.adapter import adapter_config
-from pyramid.threadlocal import get_current_registry
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from zope.interface import implementer
-from zope.lifecycleevent import ObjectCreatedEvent
 from zope.schema.fieldproperty import FieldProperty
 
 
@@ -49,9 +46,4 @@
 @adapter_config(context=ISecurityManager, provides=INotificationSettings)
 def security_notification_settings_factory(context):
     """Security manager notifications factory adapter"""
-    annotations = IAnnotations(context)
-    settings = annotations.get(NOTIFICATIONS_KEY)
-    if settings is None:
-        settings = annotations[NOTIFICATIONS_KEY] = NotificationSettings()
-        get_current_registry().notify(ObjectCreatedEvent(settings))
-    return settings
+    return get_annotation_adapter(context, NOTIFICATIONS_KEY, NotificationSettings, locate=False)
--- a/src/pyams_security/plugin/social.py	Wed May 23 15:09:56 2018 +0200
+++ b/src/pyams_security/plugin/social.py	Mon May 28 14:05:40 2018 +0200
@@ -19,14 +19,13 @@
 # import interfaces
 from pyams_security.interfaces import ISocialUser, IPrincipalInfo, ISocialUsersFolderPlugin, ISecurityManager, \
     IAuthenticatedPrincipalEvent, ISocialLoginConfiguration, ISocialLoginProviderInfo, ISocialLoginProviderConnection
-from zope.annotation.interfaces import IAnnotations
 from zope.traversing.interfaces import ITraversable
 
 # import packages
 from authomatic.providers import oauth1, oauth2
 from persistent import Persistent
 from pyams_security.principal import PrincipalInfo
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
 from pyams_utils.registry import query_utility
 from pyams_utils.request import check_request
 from pyams_utils.vocabulary import vocabulary_config
@@ -35,7 +34,6 @@
 from zope.container.folder import Folder
 from zope.interface import implementer
 from zope.lifecycleevent import ObjectCreatedEvent
-from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
@@ -379,13 +377,8 @@
 @adapter_config(context=ISecurityManager, provides=ISocialLoginConfiguration)
 def social_login_configuration_adapter(context):
     """Social login configuration adapter"""
-    annotations = IAnnotations(context)
-    configuration = annotations.get(SOCIAL_LOGIN_CONFIGURATION_KEY)
-    if configuration is None:
-        configuration = annotations[SOCIAL_LOGIN_CONFIGURATION_KEY] = SocialLoginConfiguration()
-        check_request().registry.notify(ObjectCreatedEvent(configuration))
-        locate(configuration, context, '++social-configuration++')
-    return configuration
+    return get_annotation_adapter(context, SOCIAL_LOGIN_CONFIGURATION_KEY, SocialLoginConfiguration,
+                                  name='++social-configuration++')
 
 
 @adapter_config(name='social-configuration', context=ISecurityManager, provides=ITraversable)
--- a/src/pyams_security/profile.py	Wed May 23 15:09:56 2018 +0200
+++ b/src/pyams_security/profile.py	Mon May 28 14:05:40 2018 +0200
@@ -20,25 +20,23 @@
 from pyams_security.interfaces.profile import PUBLIC_PROFILE_KEY, IPublicProfile
 from pyams_utils.interfaces import PUBLIC_PERMISSION
 from pyams_utils.interfaces.tales import ITALESExtension
-from zope.annotation.interfaces import IAnnotations, IAttributeAnnotatable
 from zope.intid.interfaces import IIntIds
 from zope.traversing.interfaces import ITraversable
 
 # import packages
 from persistent import Persistent
 from pyams_file.property import FileProperty
-from pyams_utils.adapter import adapter_config, ContextRequestAdapter
+from pyams_utils.adapter import adapter_config, ContextRequestAdapter, get_annotation_adapter
 from pyams_utils.registry import get_utility
 from pyams_utils.request import check_request, query_request
 from pyramid.security import Allow, ALL_PERMISSIONS, Everyone
-from pyramid.threadlocal import get_current_registry, get_current_request
+from pyramid.threadlocal import get_current_request
 from zope.container.contained import Contained
-from zope.lifecycleevent import ObjectCreatedEvent
 from zope.interface import implementer, Interface
 from zope.location import locate
 
 
-@implementer(IPublicProfile, IAttributeAnnotatable)
+@implementer(IPublicProfile)
 class PublicProfile(Persistent, Contained):
     """Public profile persistent class"""
 
@@ -62,18 +60,17 @@
 @adapter_config(context=IPrincipalInfo, provides=IPublicProfile)
 def principal_public_profile_factory(principal):
     """Principal public profile factory adapter"""
-    annotations = IAnnotations(principal)
-    profile = annotations.get(PUBLIC_PROFILE_KEY)
-    if profile is None:
-        profile = annotations[PUBLIC_PROFILE_KEY] = PublicProfile()
-        get_current_registry().notify(ObjectCreatedEvent(profile))
+
+    def public_profile_callback(profile):
         request = get_current_request()
         if request is not None:
             root = request.root
             intids = get_utility(IIntIds)
             locate(profile, root)  # avoid NotYet exception
             locate(profile, root, '++profile++{0}'.format(intids.register(profile)))
-    return profile
+
+    return get_annotation_adapter(principal, PUBLIC_PROFILE_KEY, PublicProfile,
+                                  locate=False, callback=public_profile_callback)
 
 
 @adapter_config(name='profile', context=(Interface, Interface), provides=ITraversable)
--- a/src/pyams_security/security.py	Wed May 23 15:09:56 2018 +0200
+++ b/src/pyams_security/security.py	Mon May 28 14:05:40 2018 +0200
@@ -20,12 +20,11 @@
 from pyams_security.interfaces import IProtectedObject, IRole, IPrincipalInfo, GrantedRoleEvent, RevokedRoleEvent, \
     IDefaultProtectionPolicy, IRoleProtectedObject, ADMIN_USER_ID
 from pyams_utils.interfaces import PUBLIC_PERMISSION
-from zope.annotation.interfaces import IAnnotations
 
 # import packages
 from persistent import Persistent
 from persistent.dict import PersistentDict
-from pyams_utils.adapter import adapter_config
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from pyams_utils.registry import query_utility
 from pyams_utils.request import request_property
 from pyramid.location import lineage
@@ -33,8 +32,6 @@
 from pyramid.threadlocal import get_current_registry
 from zope.container.contained import Contained
 from zope.interface import implementer
-from zope.lifecycleevent import ObjectCreatedEvent
-from zope.location.location import locate
 from zope.schema.fieldproperty import FieldProperty
 
 
@@ -205,13 +202,7 @@
 @adapter_config(context=IDefaultProtectionPolicy, provides=IRoleProtectedObject)
 def protected_object_factory(context):
     """Default protected object factory"""
-    annotations = IAnnotations(context)
-    protection = annotations.get(ROLES_ANNOTATIONS_KEY)
-    if protection is None:
-        protection = annotations[ROLES_ANNOTATIONS_KEY] = RoleProtectedObject()
-        get_current_registry().notify(ObjectCreatedEvent(protection))
-        locate(protection, context)
-    return protection
+    return get_annotation_adapter(context, ROLES_ANNOTATIONS_KEY, RoleProtectedObject)
 
 
 class ProtectedObject(object):