def signature(self): """ return two-string tuple signature of (namespace, user or email); can be used as a composed key for storage implementations. Raises a zope.interface.Invalid exception if signature is not possible due to insufficient field data. """ IItemSubscriber.validateInvariants(self) # may raise Invalid... namespace = self.namespace identifier = self.user if self.email and not self.user: namespace = "email" # ignore field default identifier = self.email return (namespace, identifier)
def test_add_subscriber_email_only(self): # add by email, declare explicit namespace key = ("email", "*****@*****.**") self.container.add(namespace=key[0], email=key[1]) assert key in self.container sub = self.container[key] assert IItemSubscriber.providedBy(sub) IItemSubscriber.validateInvariants(sub) assert sub.email == key[1] assert sub.namespace == key[0] # add by email, should imply namespace self.container.add(email="*****@*****.**") assert ("email", "*****@*****.**") in self.container
def __init__(self, **kwargs): """ Construct, if keyword arguments are used to construct, validate invariant on passed field values. """ if kwargs: user = kwargs.get("user", None) name = kwargs.get("name", None) namespace = kwargs.get("namespace", "member") email = kwargs.get("email", None) if isinstance(user, unicode): user = user.encode("utf-8") self.user = user if isinstance(email, unicode): email = email.encode("utf-8") self.email = email if isinstance(name, str): name = name.decode("utf-8") self.name = name if isinstance(namespace, unicode): namespace = namespace.encode("utf-8") self.namespace = namespace IItemSubscriber.validateInvariants(self)
def test_invariants(self): """Test interface invariants for IItemSubscriber""" verify = lambda sub: IItemSubscriber.validateInvariants(sub) # test empty email and user subscriber = ItemSubscriber() self.assertRaises(Invalid, verify, subscriber) # test email only subscriber.email = "*****@*****.**" self.fail_on_err(Invalid, verify, subscriber) # test both user and email subscriber.user = "******" self.fail_on_err(Invalid, verify, subscriber) # test user only subscriber.email = None self.fail_on_err(Invalid, verify, subscriber)