Exemplo n.º 1
0
 def all(self):
     if self.gives_links:
         unwrap = lambda x: x.get()
     else:
         bucket = client.bucket(self.cls.bucket_name)
         unwrap = lambda x: bucket.get(x)
     return [self.cls.load(unwrap(x)) for x in self.query.run()]
Exemplo n.º 2
0
    def __getattr__(self, key):
        if key in self._meta and self._meta[key].link_type:
            retval = []
            links = self._riak_obj.links
            for link in links:
                if link[2] == key:
                    cls = _registry.class_by_bucket_name(link[0])
                    retval += [cls.load(client.bucket(link[0]).get(link[1]))]
            setattr(self, key, retval)
            return retval

        raise AttributeError('No such key: %s' % (key,))
Exemplo n.º 3
0
    def get(cls, key=None, **kwargs):
        if key:
            bucket = client.bucket(cls.bucket_name)
            obj = bucket.get(key)
            if not obj.exists:
                raise NoSuchObjectError()
            return cls.load(obj)

        if len(kwargs) == 1:
            field = kwargs.keys()[0]
            if cls._meta[field].link_type and cls._meta[field].backref:
                bucket = client.bucket(cls.bucket_name)
                _2i_key = '%s_bin' % (field,)
                _2i_value = ('%s/%s' % (kwargs[field].bucket_name,
                                        kwargs[field].key))
                index_query = client.index(cls.bucket_name,
                                           _2i_key, _2i_value)
                return RiakObjectQuery(index_query, cls, True)
        elif cls.searchable and kwargs:
            return cls.get_search(**kwargs)
        else:
            return cls.get_mr(**kwargs)
Exemplo n.º 4
0
    def save(self):
        self.pre_save()
        self.clean()
        bucket = client.bucket(self.bucket_name)

        # Ideally, this should live in the metaclass, but since we don't
        # have a connection there yet..
        if self.searchable:
            bucket.enable_search()

        data_dict = dict((k, getattr(self, k)) for k in self._meta
                                                if not self._meta[k].link_type
                                                   and hasattr(self, k))
        if self._riak_obj:
            self._riak_obj.data = data_dict
        else:
            self._riak_obj = bucket.new(self.key, data=data_dict)

        # Remove all existing links and indexes
        for l in self._riak_obj.get_links():
            self._riak_obj.remove_link(l)

        indexes = self._riak_obj.get_indexes()
        for idx in list(indexes):
            self._riak_obj.remove_index(*idx)

        # ..and add the new set of links
        for l in self._links:
            self._riak_obj.add_link(l)

        for field in self._meta:
            if self._meta[field].link_type and self._meta[field].backref:
                value = getattr(self, field)
                for link in value:
                    self._riak_obj.add_index('%s_bin' % (field,),
                                             '%s/%s' % (link.bucket_name,
                                                        link.key))

        self._riak_obj.store()
        self.key = self._riak_obj.key
        self.post_save()