• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 27. Principal Annotations > Step 2: Creating the Information Adapter

Step 2: Creating the Information Adapter

At this point you need to provide an adapter that is able to adapt from IPrincipal to IPrincipalInformation, using the principal annotation service to store the data. In a new module named info.py, you need to add the following adapter code:

01 from persistent.dict import PersistentDict
02 from zope.interface import implements
03 from zope.app import zapi
04
05 from interfaces import IPrincipalInformation
06
07 key = 'book.principalinfo.Information'
08
09 class PrincipalInformation(object):
10     r"""Principal Information Adapter"""
11     implements(IPrincipalInformation)
12
13     def __init__(self, principal):
14         annotationsvc = zapi.getService('PrincipalAnnotation')
15         annotations = annotationsvc.getAnnotations(principal)
16         if annotations.get(key) is None:
17             annotations[key] = PersistentDict()
18         self.info = annotations[key]
19
20     def __getattr__(self, name):
21         if name in IPrincipalInformation:
22             return self.info.get(name, None)
23         raise AttributeError, "'%s' not in interface." %name
24
25     def __setattr__(self, name, value):
26         if name in IPrincipalInformation:
27             self.info[name] = value
28         else:
29             super(PrincipalInformation, self).__setattr__(name, value)


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint