Esempio n. 1
0
def add_entry():
    if len(request.form['body']) < 1:
        return

    if db.getUser(request.form['username']) is None:
        return

    body = request.form['body']
    username = request.form['username']
    parent_id = ObjectId(request.form['parent'])

    #insert item
    item_dict = {
        'body': unicode(body),
        'user': unicode(username),
    }
    if request.form['tldr']:
        item_dict['tldr'] = unicode(request.form['tldr'])
    new_item = db.addItem(item_dict)

    #insert the new rel
    new_rel = db.addRel(parent_id, new_item._id, username)

    """
    # insert each tag into the tag table if the tag is valid
    tags = request.form['tags']
    if len(tags) > 0:
        for tag_name in tags.split():
            tag = getTag(tag_name)
            if tag is None:
                continue
            cur.execute('insert into tag_relations (item, tag) values (?, ?)', [new_item_id, tag['id']])
    """

    #automatically upvote for the user
    db.processVote(new_rel._id, request.form['username'], 'up')

    #flash('New entry was successfully posted')

    ret_dict = {
        "item": db.prepareForClient([new_item])[0],
        "rel": db.prepareForClient([db.getRel(new_rel._id)])[0]
    }
    return jsonify(ret_dict)
Esempio n. 2
0
def addLink():
    l_item_id, parent_id, username = ObjectId(request.form['link_item']), ObjectId(request.form['parent']), request.form['username']
    user, l_item, parent = db.getUser(username), db.getItem(l_item_id), db.getItem(parent_id)
    if user is None:
        return "Not a user"
    if l_item is None or parent is None:
        return "invalid ids"

    new_rel = db.addRel(parent._id, l_item._id, username)
    db.processVote(new_rel._id, request.form['username'], 'up')

    rel_child = db.getItem(new_rel['child'])
    # child_user = db.getUser(rel_child['user'])

    ret_dict = {
        "new_rel": db.prepareForClient([db.getRel(new_rel._id)])[0],
        "rel_child": db.prepareForClient([rel_child])[0]
    }
    return jsonify(ret_dict)
Esempio n. 3
0
def vote():
    uv_c, dv_c = db.processVote(ObjectId(request.form['rel_id']), request.form['username'], request.form['vote_type'])
    return jsonify(uv_c=uv_c, dv_c=dv_c)