Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RiakModelMixIn(object):
- """Simply define a property "key" that generates a key
- based on the model's fields and you can store them in riak
- Example::
- class MyModel(micromodels.Model):
- slug = micromodels.CharField()
- title = micromodels.CharField()
- @property
- def key(self):
- return self.slug
- """
- @classmethod
- def get_bucket(cls):
- bucketname = cls.__name__.lower()
- return client.bucket("%s.%s" % (PREFIX, bucketname))
- @classmethod
- def load(cls, key):
- bucket = cls.get_bucket()
- obj = bucket.get(key)
- return cls(obj.get_data())
- def save(self):
- bucket = self.get_bucket()
- data = self.to_dict(serial=True)
- log.debug("Storing %s in %s" % (self.key, bucket.get_name()))
- obj = bucket.new(self.key, data=data)
- obj.store()
- return obj
Add Comment
Please, Sign In to add comment