Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class RiakModelMixIn(object):
  2. """Simply define a property "key" that generates a key
  3. based on the model's fields and you can store them in riak
  4.  
  5. Example::
  6.  
  7. class MyModel(micromodels.Model):
  8. slug = micromodels.CharField()
  9. title = micromodels.CharField()
  10.  
  11. @property
  12. def key(self):
  13. return self.slug
  14.  
  15. """
  16.  
  17. @classmethod
  18. def get_bucket(cls):
  19. bucketname = cls.__name__.lower()
  20. return client.bucket("%s.%s" % (PREFIX, bucketname))
  21.  
  22. @classmethod
  23. def load(cls, key):
  24. bucket = cls.get_bucket()
  25. obj = bucket.get(key)
  26. return cls(obj.get_data())
  27.  
  28. def save(self):
  29. bucket = self.get_bucket()
  30. data = self.to_dict(serial=True)
  31.  
  32. log.debug("Storing %s in %s" % (self.key, bucket.get_name()))
  33. obj = bucket.new(self.key, data=data)
  34. obj.store()
  35.  
  36. return obj
Add Comment
Please, Sign In to add comment