from persistent.dict import PersistentDict from zope.interface import implements from zope.component import adapts from zope.security.interfaces import IPrincipal from zope.annotation.interfaces import IAnnotations from interfaces import IMemberInfo KEY = "krihvel.memberinfo" class MappingProperty(object): def __init__(self, name): self.name = name def __get__(self, inst, class_=None): # This solves any errors with additional MemberInfo properties if self.name in inst.mapping.keys(): # If the property is in dictionary keys, the value is returned return inst.mapping[self.name] else: # In case the property is not in dictionary yet, an empry values is returned return u'' def __set__(self, inst, value): inst.mapping[self.name] = value class MemberInfo(object): implements(IMemberInfo) adapts(IPrincipal) def __init__(self, context): annotations = IAnnotations(context) mapping = annotations.get(KEY) if mapping is None: blank = {'firstname': u'', 'lastname': u'', 'email': u'', 'gender': u'', 'biography': u'', 'homepage': u''} mapping = annotations[KEY] = PersistentDict(blank) self.mapping = mapping firstname = MappingProperty('firstname') lastname = MappingProperty('lastname') email = MappingProperty('email') gender = MappingProperty('gender') biography = MappingProperty('biography') homepage = MappingProperty('homepage')