class ContactV1(Model): """A contact""" bucket = "contact" VERSION = 1 MIGRATOR = ContactMigrator # key is UUID user_account = ForeignKey(UserAccount) name = Unicode(max_length=255, null=True) surname = Unicode(max_length=255, null=True) email_address = Unicode(null=True) # EmailField? msisdn = Unicode(max_length=255) dob = Timestamp(null=True) twitter_handle = Unicode(max_length=100, null=True) facebook_id = Unicode(max_length=100, null=True) bbm_pin = Unicode(max_length=100, null=True) gtalk_id = Unicode(null=True) mxit_id = Unicode(null=True) wechat_id = Unicode(null=True) created_at = Timestamp(default=datetime.utcnow) groups = ManyToMany(ContactGroupVNone) extra = Dynamic(prefix='extras-') subscription = Dynamic(prefix='subscription-') def add_to_group(self, group): if isinstance(group, ContactGroupVNone): self.groups.add(group) else: self.groups.add_key(group) def addr_for(self, delivery_class): if delivery_class is None: # FIXME: Find a better way to do get delivery_class and get rid of # this hack. return self.msisdn # TODO: delivery classes need to be defined somewhere if delivery_class in ('sms', 'ussd'): return self.msisdn elif delivery_class == 'gtalk': return self.gtalk_id elif delivery_class == 'twitter': return self.twitter_handle elif delivery_class == 'mxit': return self.mxit_id elif delivery_class == 'wechat': return self.wechat_id else: return None def __unicode__(self): if self.name and self.surname: return u' '.join([self.name, self.surname]) else: return (self.surname or self.name or self.gtalk_id or self.twitter_handle or self.msisdn or self.mxit_id or self.wechat_id or 'Unknown User')
class VersionedDynamicModel(Model): bucket = 'versioneddynamicmodel' VERSION = 1 MIGRATOR = VersionedDynamicModelMigrator drop = Dynamic(prefix='drop-') keep = Dynamic(prefix='keep-')
def test_validate(self): dynamic = Dynamic() dynamic.validate({u'a': u'foo', u'b': u'bar'}) self.assertRaises(ValidationError, dynamic.validate, {u'a': 'foo', u'b': u'bar'}) self.assertRaises(ValidationError, dynamic.validate, u'this is not a dict') self.assertRaises(ValidationError, dynamic.validate, {u'a': 'foo', u'b': 2})
class Contact(Model): """A contact""" VERSION = 2 MIGRATOR = ContactMigrator # key is UUID user_account = ForeignKey(UserAccount) name = Unicode(max_length=255, null=True) surname = Unicode(max_length=255, null=True) email_address = Unicode(null=True) # EmailField? dob = Timestamp(null=True) created_at = Timestamp(default=datetime.utcnow) groups = ManyToMany(ContactGroup) extra = Dynamic(prefix='extras-') subscription = Dynamic(prefix='subscription-') # Address fields msisdn = Unicode(max_length=255, index=True) twitter_handle = Unicode(max_length=100, null=True, index=True) facebook_id = Unicode(max_length=100, null=True, index=True) bbm_pin = Unicode(max_length=100, null=True, index=True) gtalk_id = Unicode(null=True, index=True) mxit_id = Unicode(null=True, index=True) wechat_id = Unicode(null=True, index=True) def add_to_group(self, group): if isinstance(group, ContactGroup): self.groups.add(group) else: self.groups.add_key(group) def addr_for(self, delivery_class): if delivery_class is None: # FIXME: Find a better way to do get delivery_class and get rid of # this hack. return self.msisdn delivery_class = DELIVERY_CLASSES.get(delivery_class) if delivery_class is not None: return getattr(self, delivery_class['field']) return None def __unicode__(self): if self.name and self.surname: return u' '.join([self.name, self.surname]) else: return (self.surname or self.name or self.gtalk_id or self.twitter_handle or self.msisdn or self.mxit_id or self.wechat_id or 'Unknown User')
def test_validate(self): dynamic = Dynamic() dynamic.validate({u'a': u'foo', u'b': u'bar'}) self.assertRaises(ValidationError, dynamic.validate, { u'a': 'foo', u'b': u'bar' }) self.assertRaises(ValidationError, dynamic.validate, u'this is not a dict') self.assertRaises(ValidationError, dynamic.validate, { u'a': 'foo', u'b': 2 })
class CurrentTag(Model): # key is flattened tag current_batch = ForeignKey(Batch, null=True) tag = Tag() metadata = Dynamic(Unicode()) @staticmethod def _flatten_tag(tag): return "%s:%s" % tag @staticmethod def _split_key(key): return tuple(key.split(':', 1)) @classmethod def _tag_and_key(cls, tag_or_key): if isinstance(tag_or_key, tuple): # key looks like a tag tag, key = tag_or_key, cls._flatten_tag(tag_or_key) else: tag, key = cls._split_key(tag_or_key), tag_or_key return tag, key def __init__(self, manager, key, _riak_object=None, **kw): tag, key = self._tag_and_key(key) if _riak_object is None: kw['tag'] = tag super(CurrentTag, self).__init__(manager, key, _riak_object=_riak_object, **kw) @classmethod def load(cls, manager, key, result=None): _tag, key = cls._tag_and_key(key) return super(CurrentTag, cls).load(manager, key, result)
class Batch(Model): # key is batch_id tags = ListOf(Tag()) metadata = Dynamic(Unicode())
class BatchVNone(Model): bucket = 'batch' # key is batch_id tags = ListOf(Tag()) metadata = Dynamic(Unicode())
class DynamicModel(Model): a = Unicode() contact_info = Dynamic()
class UnversionedDynamicModel(Model): bucket = 'versioneddynamicmodel' drop = Dynamic(prefix='drop-') keep = Dynamic(prefix='keep-')
def test_fails_on_fancy_subtype(self): self.assertRaises(RuntimeError, FieldWithSubtype, Dynamic())
class DynamicModel(Model): """ Toy model for Dynamic tests. """ a = Unicode() contact_info = Dynamic()