示例#1
0
def getBlitem(blitem_id=None):
    flat = request.args.get('flat')
    if flat:
        return jsonify(Blitem.get_item({'_id': ObjectId(blitem_id)}))
    else:
        return jsonify(Blitem.get_flat(blitem_id))
    abort(404)
示例#2
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'))
示例#3
0
def newTag():
    target_id = None
    key = request.form['k']
    user = get_user_name(key)
    target_id = request.form['i']

    if Blitem.isOwner(target_id, user):
        tag = request.form['t']
        Blitem.addTag(target_id, tag)
    return jsonify({'response': 'ok'})
示例#4
0
def deleteBlitem(blitem_id=None, login_key=None):
    user = get_user_name(login_key)
    if user:
        if is_valid_id(blitem_id):
            if Blitem.can_write(user, blitem_id):
                current_app.logger.error(blitem_id)
                Blitem.remove({'_id': ObjectId(blitem_id)})
                return jsonify({'response': 'deleted'})

        abort(404)
    abort(401)
示例#5
0
def update_bookmark(oid, bookmark):
    if oid and bookmark:
        blitem = Blitem.get({'_id': ObjectId(oid)})
        print str(blitem)
        items = blitem['i']
        for item in items:
            # type = item['t']
            # if ControlType.is_bookmark(type):
            item['v'] = bookmark
        # logger.debug(str(blitem))
        Blitem.save(blitem)
示例#6
0
def updateTwitterItem(user_list, namesBag):
    for user in user_list:
        name = user.get('screen_name')
        u_id = getObjectId(name, namesBag)
        if u_id is not None:
            logger.debug('Update Item ' + str(u_id) + ' with ' + str(user))
            blitem = Blitem.get({'_id': ObjectId(u_id)})
            items = blitem['i']
            for item in items:
                type = item['t']
                if ControlType.is_twitter(type):
                    item['cv'] = user
            logger.debug(str(blitem))
            Blitem.save(blitem)
示例#7
0
def get_items_by_tag(username=None, slug=None, tag=None):
    if username is None or slug is None or tag is None:
        abort(404)
    # ip = request.remote_addr
    blibb_id = Blibb.get_by_slug(username, slug)
    if blibb_id:
        cond = {'s': slug, 'u': username}
        Blibb.increase_view(cond, 'vt')
        # return blibb_id
        b = Blitem()

        items = b.get_items_by_tag(blibb_id['id'], tag)
        return jsonify(items)
    return jsonify({'results': '0'})
示例#8
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'))
示例#9
0
	def test_updateItems(self):
		blitem = Blitem()
		blitem.load('4f99146f2ba78b09b4000001')
		blitem.populate()
		items = blitem.items
		d = dict()
		d['l'] = 'http://docs.python.org/library/urlparse.html'
		d['d'] = "docs.python.org" 
		for item in items:
			if BControl.isURL(item['t']):
				item['v'] = d
		
		blitem.items = items
		blitem.save()
示例#10
0
def newComment():
    key = request.form['login_key']
    target_id = request.form['item_id']
    text = request.form['comment']
    user = get_user(key)
    if user is not None:
        if 'status' in user:
            del user['status']
        if 'id' in user:
            del user['id']
        if 'role' in user:
            del user['role']
        c_id = Comment.insert(target_id, user, text)
        Blitem.increase_comment_counter(target_id)
        return jsonify({'item': target_id, 'user': user, 'text': text, 'comment_id': c_id})

        #####
        ###     TODO: queue item_id + comment to be added to
        ###     assoc. blibb object
        #####

    return jsonify({'error': 'user not found'})
示例#11
0
def get_by_slug(username=None, slug=None, url=None, attributes={}, flat=True):
    if username is None or slug is None:
        return None
    page = request.args.get('page', 1)
    # comments = request.args.get('comments', 0)
    blibb = Blibb.get_by_slug(username, slug)
    if url:
        blibb['url'] = url
    ret = dict()
    cond = {'s': slug, 'u': username}
    Blibb.increase_view(cond, 'v')
    ret['blibb'] = blibb
    rs_items = Blitem.get_all_items(blibb['id'], int(page), attributes, flat)
    ret['items'] = rs_items['items']
    return ret
示例#12
0
def addItemtoBlibb(username=None, slug=None):
    if username is None or slug is None:
        abort(404)

    user = ''
    app_token = ''
    if 'login_key' in request.form:
        key = request.form['login_key']
        user = get_key(key)
    else:
        app_token = request.form['app_token'] if \
            'app_token' in request.form else ''

    tags = request.form['tags'] if 'tags' in request.form else ''
    blibb = Blibb.get_object({'u': username, 's': slug}, {'_id': 1, 't.i': 1})
    if blibb:
        blibb_id = blibb['_id']
        controls = Blibb.get_controls_as_dict(blibb.get('t'))
        current_app.logger.info(str(user) + ' - ' + str(app_token) + ' - '
           + str(blibb_id) + ' - ' + username + ' - ' + slug)
        if Blibb.can_write(user, app_token, blibb_id):
            bitems = Blitem.get_items_from_request(controls, request)
            if len(bitems) > 0:
                blitem_id = Blitem.insert(blibb_id, user, bitems, tags)
                if is_valid_id(blitem_id):
                    cond = {'s': slug, 'u': username}
                    Blibb.inc_num_item(cond)
                    Blitem.post_process(blitem_id, bitems)
                    res = {'id': blitem_id}
                    return jsonify(res)
            else:
                return jsonify({'error': 'your POST data was not complete'})
        else:
            abort(401)
    else:
        abort(404)
示例#13
0
def send_message():
    blitem_id = request.form['id']
    transaction = request.form['transaction']
    app_token = request.form['app_token']

    if is_valid_id(blitem_id):
        blitem = Blitem.get({'_id': ObjectId(blitem_id), 'i.v': transaction})
        if blitem:
            flat = Blitem.flat_object(blitem)
            current_app.logger.info(flat)
            template = read_file('/bsm/templates/mysecretvalentine.html')
            html_mail = template.decode('utf-8')
            html_mail = html_mail.replace("*|IMAGE|*", flat['url'])
            # html_mail = html_mail.replace("*|URL|*", 'http://blibb.net/go/' + flat['url_id'])
            html_mail = html_mail.replace("*|MESSAGE|*", flat['message'])

            mail = {
                'to_address': flat['to'],
                'from_name': 'Your Secret Valentine',
                'from': '*****@*****.**',
                'subject': 'Your Secret Valentine',
                'txt_body': flat['message'],
                'html_body': html_mail
            }

            print 'Mail sent to ' + mail['to_address']
            sendgrid_user = get_config_value('SENDGRID_USER')
            sendgrid_password = get_config_value('SENDGRID_PASSWORD')
            s = sendgrid.Sendgrid(sendgrid_user, sendgrid_password, secure=True)
            message = sendgrid.Message((mail['from'], mail['from_name']), mail['subject'], mail['txt_body'], mail['html_body'])
            message.add_to(mail['to_address'], '')
            s.web.send(message)
            # return jsonify({'return': 'success'})
            return html_mail
        abort(401)
    abort(400)
示例#14
0
def loader_excel():
    event = Event('web.content.loader_excel')
    key = request.form['login_key']
    bid = request.form['blibb_id']
    user = get_user_name(key)
    res = dict()
    file = request.files['file']
    if file and allowed_file(file.filename):
        try:
            filename = secure_filename(file.filename)
            excel_file = os.path.join(
                get_config_value('UPLOAD_FOLDER'), filename
            )
            file.save(excel_file)
            if is_valid_id(bid):
                fields = Blibb.get_fields(bid)
                excel_data = loader.excel_to_dict(excel_file, fields)
                current_app.logger.debug(excel_data)
                if len(excel_data):
                    a = Blitem.bulk_insert(bid, user, excel_data)
                    current_app.logger.debug(a)
                    res['result'] = 'ok'
                else:
                    res['error'] = 'No data found in file'
            else:
                if bid == '-1':
                    res['error'] = 'create new blibb from file'

                res['error'] = 'Object Id is not valid'
        # except Exception, e:
            # current_app.logger.error(e)
            # res['error'] = 'Error processing spreadsheet'

        finally:
            if os.path.isfile(filename):
                os.unlink(filename)

    else:
        res['error'] = 'File was not uploaded'

    event.save()
    return jsonify(res)
示例#15
0
 def test_get_flat(self):
     i_id = '4fdb17452ba78b162f00002b'
     b = Blitem.get_flat(i_id)
     print str(b)
示例#16
0
def getAllItemsFlat(blibb_id=None, page=1):
    r = Blitem.get_all_items(blibb_id, page)
    if r != 'null':
        return jsonify(r)
    else:
        abort(404)
示例#17
0
def voteDown(blibb_id=None, page=1):
    item_id = request.form['item_id']
    key = request.form['login_key']
    user = get_user_name(key)
    r = Blitem.vote_down(item_id, user)
    return jsonify(r)