Use object factory in annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Thu, 17 Jan 2019 10:12:34 +0100
changeset 155 326ee1e62dcb
parent 154 6d9f0d23415b
child 156 7aacb5557096
Use object factory in annotations adapters
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/notification.py	Fri Jan 11 13:17:36 2019 +0100
+++ b/src/pyams_security/notification.py	Thu Jan 17 10:12:34 2019 +0100
@@ -12,21 +12,16 @@
 
 __docformat__ = 'restructuredtext'
 
+from persistent import Persistent
+from zope.schema.fieldproperty import FieldProperty
 
-# import standard library
-
-# import interfaces
 from pyams_security.interfaces import ISecurityManager
 from pyams_security.interfaces.notification import INotificationSettings
-
-# import packages
-from persistent import Persistent
 from pyams_utils.adapter import adapter_config, get_annotation_adapter
-from zope.interface import implementer
-from zope.schema.fieldproperty import FieldProperty
+from pyams_utils.factory import factory_config
 
 
-@implementer(INotificationSettings)
+@factory_config(INotificationSettings)
 class NotificationSettings(Persistent):
     """Notification settings"""
 
@@ -46,4 +41,4 @@
 @adapter_config(context=ISecurityManager, provides=INotificationSettings)
 def security_notification_settings_factory(context):
     """Security manager notifications factory adapter"""
-    return get_annotation_adapter(context, NOTIFICATIONS_KEY, NotificationSettings, locate=False)
+    return get_annotation_adapter(context, NOTIFICATIONS_KEY, INotificationSettings, locate=False)
--- a/src/pyams_security/plugin/social.py	Fri Jan 11 13:17:36 2019 +0100
+++ b/src/pyams_security/plugin/social.py	Thu Jan 17 10:12:34 2019 +0100
@@ -12,30 +12,28 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 from datetime import datetime
 
-# import interfaces
-from pyams_security.interfaces import ISocialUser, IPrincipalInfo, ISocialUsersFolderPlugin, ISecurityManager, \
-    IAuthenticatedPrincipalEvent, ISocialLoginConfiguration, ISocialLoginProviderInfo, ISocialLoginProviderConnection
-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, get_annotation_adapter
-from pyams_utils.registry import query_utility
-from pyams_utils.request import check_request
-from pyams_utils.vocabulary import vocabulary_config
 from pyramid.events import subscriber
 from zope.container.contained import Contained
 from zope.container.folder import Folder
 from zope.interface import implementer
 from zope.lifecycleevent import ObjectCreatedEvent
 from zope.schema.fieldproperty import FieldProperty
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+from zope.traversing.interfaces import ITraversable
+
+from pyams_security.interfaces import IAuthenticatedPrincipalEvent, IPrincipalInfo, ISecurityManager, \
+    ISocialLoginConfiguration, ISocialLoginProviderConnection, ISocialLoginProviderInfo, ISocialUser, \
+    ISocialUsersFolderPlugin
+from pyams_security.principal import PrincipalInfo
+from pyams_utils.adapter import ContextAdapter, adapter_config, get_annotation_adapter
+from pyams_utils.factory import factory_config
+from pyams_utils.registry import query_utility
+from pyams_utils.request import check_request
+from pyams_utils.vocabulary import vocabulary_config
 
 
 @implementer(ISocialUser)
@@ -355,7 +353,7 @@
         super(OAuthProvidersVocabulary, self).__init__(terms)
 
 
-@implementer(ISocialLoginConfiguration)
+@factory_config(ISocialLoginConfiguration)
 class SocialLoginConfiguration(Folder):
     """Social login configuration"""
 
@@ -377,7 +375,7 @@
 @adapter_config(context=ISecurityManager, provides=ISocialLoginConfiguration)
 def social_login_configuration_adapter(context):
     """Social login configuration adapter"""
-    return get_annotation_adapter(context, SOCIAL_LOGIN_CONFIGURATION_KEY, SocialLoginConfiguration,
+    return get_annotation_adapter(context, SOCIAL_LOGIN_CONFIGURATION_KEY, ISocialLoginConfiguration,
                                   name='++social-configuration++')
 
 
--- a/src/pyams_security/profile.py	Fri Jan 11 13:17:36 2019 +0100
+++ b/src/pyams_security/profile.py	Thu Jan 17 10:12:34 2019 +0100
@@ -12,31 +12,27 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_security.interfaces import IPrincipalInfo, ADMIN_USER_ID
-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 persistent import Persistent
+from pyramid.security import ALL_PERMISSIONS, Allow, Everyone
+from pyramid.threadlocal import get_current_request
+from zope.container.contained import Contained
+from zope.interface import Interface
 from zope.intid.interfaces import IIntIds
+from zope.location import locate
 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, get_annotation_adapter
+from pyams_security.interfaces import ADMIN_USER_ID, IPrincipalInfo
+from pyams_security.interfaces.profile import IPublicProfile, PUBLIC_PROFILE_KEY
+from pyams_utils.adapter import ContextRequestAdapter, adapter_config, get_annotation_adapter
+from pyams_utils.factory import factory_config
+from pyams_utils.interfaces import PUBLIC_PERMISSION
+from pyams_utils.interfaces.tales import ITALESExtension
 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_request
-from zope.container.contained import Contained
-from zope.interface import implementer, Interface
-from zope.location import locate
 
 
-@implementer(IPublicProfile)
+@factory_config(IPublicProfile)
 class PublicProfile(Persistent, Contained):
     """Public profile persistent class"""
 
@@ -69,7 +65,7 @@
             locate(profile, root)  # avoid NotYet exception
             locate(profile, root, '++profile++{0}'.format(intids.register(profile)))
 
-    return get_annotation_adapter(principal, PUBLIC_PROFILE_KEY, PublicProfile,
+    return get_annotation_adapter(principal, PUBLIC_PROFILE_KEY, IPublicProfile,
                                   locate=False, callback=public_profile_callback)
 
 
--- a/src/pyams_security/security.py	Fri Jan 11 13:17:36 2019 +0100
+++ b/src/pyams_security/security.py	Thu Jan 17 10:12:34 2019 +0100
@@ -12,30 +12,29 @@
 
 __docformat__ = 'restructuredtext'
 
-# import standard library
 import logging
-logger = logging.getLogger('PyAMS (security)')
 
-# import interfaces
-from pyams_security.interfaces import IProtectedObject, IRole, IPrincipalInfo, GrantedRoleEvent, RevokedRoleEvent, \
-    IDefaultProtectionPolicy, IRoleProtectedObject, ADMIN_USER_ID
-from pyams_utils.interfaces import PUBLIC_PERMISSION
-
-# import packages
 from persistent import Persistent
 from persistent.dict import PersistentDict
+from pyramid.location import lineage
+from pyramid.security import ALL_PERMISSIONS, Allow, Authenticated, DENY_ALL, Deny, Everyone
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_security.interfaces import ADMIN_USER_ID, GrantedRoleEvent, IDefaultProtectionPolicy, IPrincipalInfo, \
+    IProtectedObject, IRole, IRoleProtectedObject, RevokedRoleEvent
 from pyams_utils.adapter import adapter_config, get_annotation_adapter
+from pyams_utils.factory import factory_config
+from pyams_utils.interfaces import PUBLIC_PERMISSION
 from pyams_utils.registry import query_utility
 from pyams_utils.request import request_property
-from pyramid.location import lineage
-from pyramid.security import DENY_ALL, Everyone, Allow, Deny, ALL_PERMISSIONS, Authenticated
-from pyramid.threadlocal import get_current_registry
-from zope.container.contained import Contained
-from zope.interface import implementer
-from zope.schema.fieldproperty import FieldProperty
 
 
-@implementer(IRoleProtectedObject)
+logger = logging.getLogger('PyAMS (security)')
+
+
+@factory_config(IRoleProtectedObject)
 class RoleProtectedObject(Persistent, Contained):
     """Base class for object protected by roles"""
 
@@ -202,7 +201,7 @@
 @adapter_config(context=IDefaultProtectionPolicy, provides=IRoleProtectedObject)
 def protected_object_factory(context):
     """Default protected object factory"""
-    return get_annotation_adapter(context, ROLES_ANNOTATIONS_KEY, RoleProtectedObject)
+    return get_annotation_adapter(context, ROLES_ANNOTATIONS_KEY, IRoleProtectedObject)
 
 
 class ProtectedObject(object):