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

Chapter 36. Registering New WebDAV Names... > Step 2: Implementing the IPhoto to I...

Step 2: Implementing the IPhoto to IImage Adapter

Next, you need to implement the adapter, which will use annotations to store the attribute data. This means the IImage object must also implement IAttributeAnnotable. With the knowledge you gained in previous chapters, the following implementation should seem simple. You need to place it in the __init__.py file of the photodavns package:

01 from persistent.dict import PersistentDict
02 from zope.interface import implements
03 from zope.schema import getFieldNames
04 from zope.app.annotation.interfaces import IAnnotations
05 from zope.app.file.interfaces import IImage
06 from interfaces import IPhoto, photodavns
07
08 class ImagePhotoNamespace(object):
09     """Implement IPhoto namespace for IImage."""
10
11     implements(IPhoto)
12     __used_for__ = IImage
13
14     def __init__(self, context):
15         self.context = context
16         self._annotations = IAnnotations(context)
17         if not self._annotations.get(photodavns):
18             self._annotations[photodavns] = PersistentDict()
19
20     def __getattr__(self, name):
21         if not name in getFieldNames(IPhoto):
22             raise AttributeError, "'%s' object has no attribute '%s'" %(
23                 self.__class__.__name__, name)
24         return self._annotations[photodavns].get(name, None)
25
26     def __setattr__(self, name, value):
27         if not name in getFieldNames(IPhoto):
28             return super(ImagePhotoNamespace, self).__setattr__(name, value)
29         field = IPhoto[name]
30         field.validate(value)
31         self._annotations[photodavns][name] = value


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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