Beispiel #1
0
def newItem():
    bid = request.form['blibb_id']
    key = request.form['login_key']
    tags = request.form['tags']
    app_token = request.form['app_token']

    user = get_user_name(key)
    current_app.logger.info('labels: ' + str(user))
    if is_valid_id(bid):
        b = Blibb.get_object({'_id': ObjectId(bid)}, {'u': 1, 't.i': 1})
        controls = Blibb.get_controls_as_dict(b.get('t'))
        current_app.logger.info(controls)
        if Blibb.can_write(user, app_token, bid):
            # labels = Blibb.get_labels(b.get('t'))
            # current_app.logger.info('labels: ' + str(labels))
            bitems = Blitem.get_items_from_request(controls, request)
            current_app.logger.info('items from request: ' + str(bitems))
            blitem_id = Blitem.insert(bid, user, bitems, tags)
            if blitem_id:
                cond = {'_id': ObjectId(bid)}
                Blibb.inc_num_item(cond)
                Blitem.post_process(blitem_id, bitems)
                return blitem_id
        else:
            abort(401)
    return jsonify(Message.get('id_not_valid'))
Beispiel #2
0
def getBlitemFields(blibb_id=None):
    if is_valid_id(blibb_id):
        b = Blibb.get_object(
            {'_id': ObjectId(blibb_id)}, {'u': 1, 't.i.n': 1, 't.i.s': 1})
        res = Blibb.getLabels(b.get('t'))
    else:
        res = Message.get('id_not_valid')
    return jsonify(res)
Beispiel #3
0
    def get_by_id_params(self, obj_id, params):
        p = dict()
        if is_valid_id(obj_id):
            listparams = params.split(",")
            for param in listparams:
                p[param] = 1

            doc = self.get_object({'_id': ObjectId(obj_id)}, p)
            return self.flat_object(doc)
        return Message.get('id_not_valid')
Beispiel #4
0
 def add_webhook(self, object_id, webhook):
     if is_valid_id(object_id):
         self.objects.update(
             {'_id': ObjectId(object_id)},
             {'$pull': {'wh': {'a': webhook['a']}}}
         )
         self.objects.update(
             {'_id': ObjectId(object_id)},
             {'$addToSet': {'wh': webhook}})
     else:
         return Message.get('id_not_valid')
Beispiel #5
0
def get_item_by_id(username=None, slug=None, id=None):
    if username is None or slug is None or id is None:
        abort(404)

    blibb = Blibb.get_object({'u': username, 's': slug})
    if blibb and is_valid_id(id):
        blibb_id = blibb['_id']
        items = Blitem.get_item({'_id': ObjectId(id), 'b': ObjectId(blibb_id)},
                                {'i': 1, 'tg': 1, 'b': 1, 'c': 1}
                                )
        attributes = {'tags': True, 'comments': True}
        return jsonify(Blitem.flat_object(items, attributes))
    else:
        return jsonify(Message.get('id_not_valid'))
Beispiel #6
0
    def get_items_by_tag(self, blibb_id, tag):
        if is_valid_id(blibb_id):
            docs = self.get_items_page({'b': ObjectId(blibb_id), 'tg': tag}, {'i': 1, 'tg': 1, 'b': 1})
            result = dict()
            blitems = []
            attributes = {'tags': True}
            for d in docs:
                blitems.append(self.flat_object(d, attributes))

            result['count'] = len(blitems)
            result['items'] = blitems

            return result
        return Message.get('id_not_valid')
Beispiel #7
0
    def bulk_insert(self, blibb_id, user, items, tags=None):
        if is_valid_id(blibb_id):
            bid = ObjectId(blibb_id)
            b = Blibb.get_object({'_id': bid}, {'s': 1, 'u': 1, 't.i.n': 1, 't.i.s': 1})
            blibb_slug = b.get('s')
            labels = Blibb.get_labels(b.get('t'))
            count = 0
            for item in items:
                now = datetime.utcnow()
                i = self.get_blitem_from_dict(item, labels)
                doc = {"b": bid, "u": user, "bs": blibb_slug, "c": now, "i": i}
                objects.insert(doc)
                count = count + 1

            return str(count) + 'elements added'
        else:
            return Message.get('id_not_valid')
Beispiel #8
0
    def get_all_items(self, blibb_id, page, attributes={
            'tags': True, 'comments': True}, flat=True):
        if is_valid_id(blibb_id):
            docs = self.get_items_page({'b': ObjectId(blibb_id)}, {'i': 1, 'tg': 1, 'b': 1, 'c': 1}, page)
            result = dict()
            blitems = []
            for d in docs:
                if flat:
                    blitems.append(self.flat_object(d, attributes))
                else:
                    blitems.append(d)
            result['b_id'] = blibb_id
            result['count'] = len(blitems)
            result['items'] = blitems

            return result
        return Message.get('id_not_valid')
Beispiel #9
0
    def update(self, item_id, blibb_id, user, items, tags=None):
        tag_list = []
        if is_valid_id(blibb_id) and is_valid_id(item_id):
            # bid = ObjectId(blibb_id)
            # b = Blibb.get_object({'_id': bid}, {'s': 1})
            # bs = b['s']
            if tags is not None:
                if ',' in tags:
                    tag_list = list(set(tags.lower().split(',')))
                else:
                    tag_list = list(set(tags.lower().split()))
                for t in tag_list:
                    Blibb.add_tag(blibb_id, t)

            # now = datetime.utcnow()
            doc = {"_id": item_id, "b": blibb_id, "i": items}
            objects.update({"_id": ObjectId(item_id)}, {'$set': {"i": items}})
            post_process.send(doc)
            return item_id
        else:
            return Message.get('id_not_valid')
Beispiel #10
0
    def insert(self, blibb_id, user, items, tags=None):
        tag_list = []
        if is_valid_id(blibb_id):
            bid = ObjectId(blibb_id)
            b = Blibb.get_object({'_id': bid}, {'s': 1})
            bs = b['s']
            if tags is not None:
                if ',' in tags:
                    tag_list = list(set(tags.lower().split(',')))
                else:
                    tag_list = list(set(tags.lower().split()))
                for t in tag_list:
                    Blibb.add_tag(blibb_id, t)

            num = int(NUM_CHARS)
            url_id = "".join(sample(digits + ascii_letters, num))
            now = datetime.utcnow()
            doc = {"b": bid, "u": user, "bs": bs, "c": now, "i": items,
                   "tg": tag_list, "st": "active", "si": url_id}
            newId = objects.insert(doc)
            post_process.send(doc)
            return str(newId)
        else:
            return Message.get('id_not_valid')
Beispiel #11
0
 def get_template(self, obj_id):
     if is_valid_id(obj_id):
         template = self.get_object({'_id': ObjectId(obj_id)}, {'t': 1})
         return jsonify(self.flat_object(template))
     else:
         return Message.get('id_not_valid')
Beispiel #12
0
 def add_picture(self, filter, picture_id):
     if is_valid_id(picture_id):
         image = Picture.dump_image(picture_id)
         objects.update(filter, {"$set": {'img': image}})
         return picture_id
     return Message.get('id_not_valid')
Beispiel #13
0
	def test_getErrors(self):		
		print Message.get('id_not_valid')