def __init__(self, *args, **kwargs): if self.Meta.slug_field: setattr(self, 'validate_' + self.Meta.slug_field, validate_unique_key(self, self.Meta.slug_field)) for field in self.Meta.unique: setattr(self, 'validate_' + field, validate_unique_key(self, field)) super(RethinkSerializer, self).__init__(*args, **kwargs) self.conn = get_connection()
def __init__(self, *args, **kwargs): super(RethinkSerializer, self).__init__(*args, **kwargs) if self.Meta.slug_field: field = self.fields.get(self.Meta.slug_field, None) if field is not None: field.validators.append( validate_unique_key(self.Meta.slug_field)) for field_name in self.Meta.unique: field = self.fields.get(field_name, None) if field is not None: field.validators.append(validate_unique_key(field_name)) self.conn = get_connection()
def rethinkdb_unlock(self, *args, **kwargs): if 'name' in kwargs: name = kwargs['name'] else: name = args[0] args = args[1:] token = kwargs.get('token', self.request.root_id) result = r.table("locks").get_all(_distributed_lock_id(name)). \ filter({"token": token}).delete().run(get_connection()) if result['deleted'] == 1: logger.info("unlocked %s", name) else: logger.warning("unable to unlock %s, token was not %s", name, token) if len(args) > 0: return args[0]
def rethinkdb_lock(self, name, token=None, timeout=300): if token is None: token = self.request.root_id result = r.table("locks").insert({ "id": _distributed_lock_id(name), "token": token, "server": socket.gethostname(), "timestamp": r.now(), }).run(get_connection()) if result['inserted'] == 0: self.retry(exc=Exception("failed to acquire lock %s" % name), countdown=1, max_retries=timeout) else: logger.info("locked %s with token %s", name, token) return token
def filter(cls, *args, **fields): query = r.table(cls.Meta.table_name) try: reql = fields.pop('reql') except KeyError: reql = False if len(args) == 0 and len(fields) == 1 and list(fields.keys( ))[0] in cls.Meta.indices and not cls.Meta.order_by: index, value = list(fields.items())[0] query = query.get_all(value, index=index) else: if args: query = query.filter(*args) if fields: query = query.filter(fields) if cls.Meta.order_by: query = query.order_by(*cls.Meta.order_by) if reql: return query else: rs = query.run(get_connection()) if not isinstance(rs, list): rs.reql_query = query return rs
def get_connection(self): if self.rethink_conn is None: self.rethink_conn = get_connection() return self.rethink_conn