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())
def test_validate(self): t = Tag() t.validate(("pool", "tagname")) self.assertRaises(ValidationError, t.validate, ["pool", "tagname"]) self.assertRaises(ValidationError, t.validate, ("pool",))
def test_from_riak(self): t = Tag() self.assertEqual(t.from_riak(["pool", "tagname"]), ("pool", "tagname"))
def test_to_riak(self): t = Tag() self.assertEqual(t.to_riak(("pool", "tagname")), ["pool", "tagname"])
def test_validate(self): t = Tag() t.validate(("pool", "tagname")) self.assertRaises(ValidationError, t.validate, ["pool", "tagname"]) self.assertRaises(ValidationError, t.validate, ("pool", ))