[Zope-CVS] SVN: book/trunk/principalinfo/ A small sample package demonstrating principal annotations.

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Aug 25 14:19:38 EDT 2004


Log message for revision 27265:
  A small sample package demonstrating principal annotations.
  


Changed:
  A   book/trunk/principalinfo/
  A   book/trunk/principalinfo/__init__.py
  A   book/trunk/principalinfo/configure.zcml
  A   book/trunk/principalinfo/info.py
  A   book/trunk/principalinfo/interfaces.py
  A   book/trunk/principalinfo/tests.py


-=-
Added: book/trunk/principalinfo/__init__.py
===================================================================
--- book/trunk/principalinfo/__init__.py	2004-08-25 17:23:38 UTC (rev 27264)
+++ book/trunk/principalinfo/__init__.py	2004-08-25 18:19:37 UTC (rev 27265)
@@ -0,0 +1 @@
+# Make directory a package.

Added: book/trunk/principalinfo/configure.zcml
===================================================================
--- book/trunk/principalinfo/configure.zcml	2004-08-25 17:23:38 UTC (rev 27264)
+++ book/trunk/principalinfo/configure.zcml	2004-08-25 18:19:37 UTC (rev 27265)
@@ -0,0 +1,21 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:browser="http://namespaces.zope.org/browser"
+    i18n_domain="principalinfo">
+
+  <adapter
+      factory=".info.PrincipalInformation"
+      provides=".interfaces.IPrincipalInformation"
+      for="zope.app.security.interfaces.IPrincipal"
+      permission="zope.ManageServices"
+      />
+
+  <browser:editform
+      name="userInfo.html"
+      schema=".interfaces.IPrincipalInformation"
+      for="zope.app.security.interfaces.IPrincipal"
+      label="Change User Information"
+      permission="zope.ManageServices"
+      menu="zmi_views" title="User Info" />
+
+</configure>

Added: book/trunk/principalinfo/info.py
===================================================================
--- book/trunk/principalinfo/info.py	2004-08-25 17:23:38 UTC (rev 27264)
+++ book/trunk/principalinfo/info.py	2004-08-25 18:19:37 UTC (rev 27265)
@@ -0,0 +1,102 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# 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.
+#
+##############################################################################
+"""Principal Information Implementation
+
+$Id$
+"""
+from persistent.dict import PersistentDict
+from zope.interface import implements
+from zope.app import zapi
+
+from interfaces import IPrincipalInformation
+
+key = 'book.principalinfo.Information'
+
+class PrincipalInformation(object):
+    r"""Principal Information Adapter
+
+    Here is a small demonstration on how it works.
+
+    Do the tedious setup first.
+
+    >>> from zope.app.tests import setup
+    >>> from zope.app.principalannotation.interfaces import \
+    ...      IPrincipalAnnotationService
+    >>> from zope.app.principalannotation import PrincipalAnnotationService
+
+    >>> site = setup.placefulSetUp(site=True)
+    >>> root_sm = zapi.getGlobalServices()
+    >>> root_sm.defineService('PrincipalAnnotation',
+    ...                       IPrincipalAnnotationService)
+    >>> svc = setup.addService(site.getSiteManager(), 'PrincipalAnnotation',
+    ...                        PrincipalAnnotationService())
+
+    Let's now test the principal annotation. To do this, we need a
+    minimalistic principal.
+
+    >>> class Principal(object):
+    ...     id = 'user1'
+    >>> principal = Principal()
+
+    Now create the adapter using the principal and make sure that the values
+    are initially unset.
+
+    >>> info = PrincipalInformation(principal)
+    >>> info.email is None
+    True
+    >>> info.ircNickname is None
+    True
+    >>> info.phone
+    Traceback (most recent call last):
+    ...
+    AttributeError: 'phone' not in interface.
+    
+    Now set the `email` field and make sure it is still there, even if the
+    adapter is regenerated.
+
+    >>> info.email = 'foo at bar.com'
+    >>> info.email
+    'foo at bar.com'
+
+    >>> info = PrincipalInformation(principal)
+    >>> info.email
+    'foo at bar.com'
+
+    Now make really sure that the service stores the data.
+
+    >>> svc.annotations['user1']['book.principalinfo.Information']['email']
+    'foo at bar.com'
+
+    >>> setup.placefulTearDown()
+    """
+    implements(IPrincipalInformation)
+
+    def __init__(self, principal):
+        annotationsvc = zapi.getService('PrincipalAnnotation')
+        annotations = annotationsvc.getAnnotations(principal)
+        if annotations.get(key) is None:
+            annotations[key] = PersistentDict()
+        self.info = annotations[key]
+
+    def __getattr__(self, name):
+        if name in IPrincipalInformation:
+            return self.info.get(name, None)
+        raise AttributeError, "'%s' not in interface." %name
+
+    def __setattr__(self, name, value):
+        if name in IPrincipalInformation:
+            self.info[name] = value
+        else:
+            super(PrincipalInformation, self).__setattr__(name, value)
+    

Added: book/trunk/principalinfo/interfaces.py
===================================================================
--- book/trunk/principalinfo/interfaces.py	2004-08-25 17:23:38 UTC (rev 27264)
+++ book/trunk/principalinfo/interfaces.py	2004-08-25 18:19:37 UTC (rev 27265)
@@ -0,0 +1,38 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# 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.
+#
+##############################################################################
+"""Principal Information Interfaces
+
+$Id$
+"""
+from zope.i18n import MessageIDFactory
+from zope.interface import Interface
+from zope.schema import TextLine
+
+_ = MessageIDFactory('messageboard')
+
+
+class IPrincipalInformation(Interface):
+    """This interface additional information about a principal."""
+
+    email = TextLine(
+        title=_("E-mail"),
+        description=_("E-mail Address"),
+        default=u"",
+        required=False)
+
+    ircNickname = TextLine(
+        title=_("IRC Nickname"),
+        description=_("IRC Nickname"),
+        default=u"",
+        required=False)

Added: book/trunk/principalinfo/tests.py
===================================================================
--- book/trunk/principalinfo/tests.py	2004-08-25 17:23:38 UTC (rev 27264)
+++ book/trunk/principalinfo/tests.py	2004-08-25 18:19:37 UTC (rev 27265)
@@ -0,0 +1,25 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# 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.
+#
+##############################################################################
+"""Unit tests for KeeperAnnotations
+
+$Id$
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+  
+def test_suite():
+    return DocTestSuite('book.principalinfo.info')
+  
+if __name__ == '__main__':
+      unittest.main(defaultTest='test_suite')



More information about the Zope-CVS mailing list