Esempio n. 1
0
    def create(self, attributes, db_pipe=None):
        for required in self.REQUIRED_FIELDS:
            if required not in attributes:
                raise MissingRequiredField(required)

        obj = dict(self.DEFAULTS.items() + attributes.items())
        _id = uuid.uuid4().hex

        for p in self.PARAMS_TO_ENCODE:
            obj[p] = json.dumps(obj[p])

        def m(p):
            for lookup in self.REVERSE_LOOKUPS:
                self._set_reverse_lookup(p, lookup, obj[lookup], _id)

            self._create(p, _id, obj)
            self._post_create(p, _id, obj)

        if db_pipe:
            m(db_pipe)
        else:
            execute_multi(m)

        obj['id'] = _id

        return obj
Esempio n. 2
0
    def create(self, attributes, db_pipe=None):
        for required in self.REQUIRED_FIELDS:
            if required not in attributes:
                raise MissingRequiredField(required)

        obj = dict(self.DEFAULTS.items() + attributes.items())
        _id = uuid.uuid4().hex

        for p in self.PARAMS_TO_ENCODE:
            obj[p] = json.dumps(obj[p])

        def m(p):
            for lookup in self.REVERSE_LOOKUPS:
                self._set_reverse_lookup(p, lookup, obj[lookup], _id)

            self._create(p, _id, obj)
            self._post_create(p, _id, obj)

        if db_pipe:
            m(db_pipe)
        else:
            execute_multi(m)

        obj['id'] = _id

        return obj
Esempio n. 3
0
    def delete(self, _id, db_pipe=None):
        obj = self.get(_id)
        def m(p):
            for lookup in self.REVERSE_LOOKUPS:
                lookup_key = "%s-lookup-%s" % (lookup, self.KEY)
                p.delete(lookup_key % obj[lookup])

            p.delete(self.KEY % _id)
            self._post_delete(p, _id, obj)

        if db_pipe:
            m(db_pipe)
        else:
            execute_multi(m)
Esempio n. 4
0
    def delete(self, _id, db_pipe=None):
        obj = self.get(_id)

        def m(p):
            for lookup in self.REVERSE_LOOKUPS:
                lookup_key = "%s-lookup-%s" % (lookup, self.KEY)
                p.delete(lookup_key % obj[lookup])

            p.delete(self.KEY % _id)
            self._post_delete(p, _id, obj)

        if db_pipe:
            m(db_pipe)
        else:
            execute_multi(m)
Esempio n. 5
0
    def set(self, task_id, user_id, updated_tags):
        task = task_model.get(task_id)
        current_tags = set(task['tags'].split(','))
        updated_tags = set(updated_tags)

        tags_to_remove = current_tags.difference(updated_tags)
        tags_to_add = updated_tags.difference(current_tags)

        def m(p):
            task_model.update(task_id, 'tags', ','.join(updated_tags), db_pipe=p, internal=True)

            for tag in tags_to_add:
                p.zincrby(self.ORG_TAGS % task['org'], tag, amount=1)
                p.zincrby(self.USER_TAGS % user_id, tag, amount=1)
            for tag in tags_to_remove:
                p.zincrby(self.ORG_TAGS % task['org'], tag, amount=-1)
                p.zincrby(self.USER_TAGS % user_id, tag, amount=-1)

        execute_multi(m)
Esempio n. 6
0
    def set(self, task_id, user_id, updated_tags):
        task = task_model.get(task_id)
        current_tags = set(task['tags'].split(','))
        updated_tags = set(updated_tags)

        tags_to_remove = current_tags.difference(updated_tags)
        tags_to_add = updated_tags.difference(current_tags)

        def m(p):
            task_model.update(task_id,
                              'tags',
                              ','.join(updated_tags),
                              db_pipe=p,
                              internal=True)

            for tag in tags_to_add:
                p.zincrby(self.ORG_TAGS % task['org'], tag, amount=1)
                p.zincrby(self.USER_TAGS % user_id, tag, amount=1)
            for tag in tags_to_remove:
                p.zincrby(self.ORG_TAGS % task['org'], tag, amount=-1)
                p.zincrby(self.USER_TAGS % user_id, tag, amount=-1)

        execute_multi(m)
Esempio n. 7
0
    def update(self, _id, field, value, db_pipe=None, internal=False):
        if not (internal or field in self.UPDATABLE_FIELDS):
            raise UpdateNotPermitted(field)

        if value in self.PARAMS_TO_ENCODE:
            value = json.dumps(value)

        def m(p):
            if field in self.REVERSE_LOOKUPS:
                old_value = db.hget(self.KEY % _id, field)
                self._set_reverse_lookup(p, field, value, _id)
                self._remove_reverse_lookup(p, field, old_value)

            p.hset(self.KEY % _id, field, value)
            try:
                getattr(self, '_post_update_%s' % field)(p, _id, value)
            except AttributeError:
                pass

        if db_pipe:
            m(db_pipe)
        else:
            execute_multi(m)
Esempio n. 8
0
    def update(self, _id, field, value, db_pipe=None, internal=False):
        if not (internal or field in self.UPDATABLE_FIELDS):
            raise UpdateNotPermitted(field)

        if value in self.PARAMS_TO_ENCODE:
            value = json.dumps(value)

        def m(p):
            if field in self.REVERSE_LOOKUPS:
                old_value = db.hget(self.KEY % _id, field)
                self._set_reverse_lookup(p, field, value, _id)
                self._remove_reverse_lookup(p, field, old_value)

            p.hset(self.KEY % _id, field, value)
            try:
                getattr(self, '_post_update_%s' % field)(p, _id, value)
            except AttributeError:
                pass

        if db_pipe:
            m(db_pipe)
        else:
            execute_multi(m)