Created
January 14, 2011 15:59
-
-
Save ericmoritz/779788 to your computer and use it in GitHub Desktop.
I really simple, stupid MixIn to enable r/w of micromodels in Riak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment