Exemple #1
0
def postComment():
    username = request.form['username']
    body = request.form['body']
    item_id = ObjectId(request.form['item'])

    if db.getUser(username) is None:
        return 'You must have a valid user account to post'

    if len(request.form['body']) < 1:
        return

    if db.getItem(item_id) is None:
        return 'Reply error'

    #insert comment
    comment_dict = {
        'body': unicode(body),
        'user': unicode(username),
        'item': item_id
    }

    if request.form['reply_to']:
        comment_dict['reply_to'] = ObjectId(request.form['reply_to'])

    new_comment = db.addComment(comment_dict)
    db.processCommentVote(new_comment._id, username, 'up')

    ret_dict = {
        "comment": db.prepareForClient([new_comment])[0]
    }
    return jsonify(ret_dict)
Exemple #2
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)
Exemple #3
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)
Exemple #4
0
def transfer():
    user_lookup = {}
    item_lookup = {}
    rel_lookup = {}
    global db
    db = connect_db()
    mon.init('localhost', 27017, 'conversion')
    dd = mon.createInitialDb()
    user_lookup[0] = mon.getUser(dd['admin'])

    lite_items = query_db('SELECT * FROM items')
    lite_rels = query_db('SELECT * FROM relations')
    lite_comments = query_db('SELECT * FROM comments')
    lite_users = query_db('SELECT * FROM users')
    lite_votes = query_db('SELECT * FROM votes')

    for lu in lite_users:
        print lu['username']
        if lu['username'] == 'NKessel':
            user_lookup[3] = user_lookup[1]
        # print lu['date_registered']
        dr = datetime.strptime(lu['date_registered'], '%Y-%m-%d %H:%M:%S')
        # print str(dr)
        u, error = mon.addUser(unicode(lu['username']), unicode(lu['email']), unicode(lu['password']), dr)
        print 'inserted users: ' + str(u)
        if u:
            user_lookup[lu['id']] = u

    for li in lite_items:
        # print 'uid lookup: ' + str(li['user_id'])+ ' -- ' + str(user_lookup[li['user_id']]) 

        #print li['time_submitted']
        ts = datetime.strptime(li['time_submitted'].split('.')[0], '%Y-%m-%d %H:%M:%S')
        if not li['body']:
            li['body'] = li['title']
            li['title'] = ""
        i = mon.addItem({
            'body': unicode(li['body']),
            'tldr': unicode(li['title']),
            'user': user_lookup[li['user_id']].name,
            "time_submitted": ts,
            "upvotes": li['upvotes'],
            "downvotes": li['downvotes']
        })
        print 'inserted items: ' + str(li['id']) + ' -- ' + str(i)
        item_lookup[li['id']] = i

    for lr in lite_rels:
        print lr.get('time_submitted', "no time")
        #tl = datetime.strptime(lr['time_linked'].split('.')[0], '%Y-%m-%d %H:%M:%S')

        if lr['parent'] == 0:
            rp = mon.root._id
        else:
            rp = item_lookup[lr['parent']]._id

        if lr['child'] == 0:
            rc = mon.root._id
        else:
            rc = item_lookup[lr['child']]._id

        lb = user_lookup.get(lr['linked_by'], None)
        if lb:
            lb = lb.name
        else:
            lb = mon.getUser(item_lookup[lr['child']].user).name

        r = mon.addRel(rp, rc, lb)
        r.upvotes = lr['upvotes']
        r.downvotes = lr['downvotes']
        # r.time_linked = tl
        r.save()
        print 'inserted rels: ' + str(lr['id']) + ' -- ' + str(r)
        rel_lookup[lr['id']] = r

    for lc in lite_comments:
        rel = rel_lookup.get(lc['rel_id'], None)
        if rel is None:
            continue
        rel = mon.getRel(rel._id)
        if lc['head_item'] == 0:
            cp = mon.root._id
        else:
            cp = item_lookup[lc['head_item']]._id
        rel.comment_parent = cp
        rel.save()

        item = mon.getItem(rel.child)
        item.tags.append(u'comment')
        item.save()

    for lv in lite_votes:
        user = mon.getUser(user_lookup[lv['user_id']].name)
        rel = rel_lookup.get(lv['rel_id'], None)
        if rel is None:
            continue
        rel_id = rel._id

        if lv['vote_type'] == 'up':
            is_upvote = True
        else:
            is_upvote = False

        user.votes.append({
            'rel': rel_id,
            'is_upvote': is_upvote
        })
        user.save()

    db.close()
Exemple #5
0
def transfer():
    user_lookup = {}
    item_lookup = {}
    rel_lookup = {}
    global db
    db = connect_db()
    mon.init("localhost", 27017, "conversion")
    dd = mon.createInitialDb()
    user_lookup[0] = mon.getUser(dd["admin"])

    lite_items = query_db("SELECT * FROM items")
    lite_rels = query_db("SELECT * FROM relations")
    lite_comments = query_db("SELECT * FROM comments")
    lite_users = query_db("SELECT * FROM users")
    lite_votes = query_db("SELECT * FROM votes")

    for lu in lite_users:
        print lu["username"]
        if lu["username"] == "NKessel":
            user_lookup[3] = user_lookup[1]
        # print lu['date_registered']
        dr = datetime.strptime(lu["date_registered"], "%Y-%m-%d %H:%M:%S")
        # print str(dr)
        u, error = mon.addUser(unicode(lu["username"]), unicode(lu["email"]), unicode(lu["password"]), dr)
        print "inserted users: " + str(u)
        if u:
            user_lookup[lu["id"]] = u

    for li in lite_items:
        # print 'uid lookup: ' + str(li['user_id'])+ ' -- ' + str(user_lookup[li['user_id']])

        # print li['time_submitted']
        ts = datetime.strptime(li["time_submitted"].split(".")[0], "%Y-%m-%d %H:%M:%S")
        if not li["body"]:
            li["body"] = li["title"]
            li["title"] = ""
        i = mon.addItem(
            {
                "body": unicode(li["body"]),
                "tldr": unicode(li["title"]),
                "user": user_lookup[li["user_id"]].name,
                "time_submitted": ts,
                "upvotes": li["upvotes"],
                "downvotes": li["downvotes"],
            }
        )
        print "inserted items: " + str(li["id"]) + " -- " + str(i)
        item_lookup[li["id"]] = i

    for lr in lite_rels:
        print lr.get("time_submitted", "no time")
        # tl = datetime.strptime(lr['time_linked'].split('.')[0], '%Y-%m-%d %H:%M:%S')

        if lr["parent"] == 0:
            rp = mon.root._id
        else:
            rp = item_lookup[lr["parent"]]._id

        if lr["child"] == 0:
            rc = mon.root._id
        else:
            rc = item_lookup[lr["child"]]._id

        lb = user_lookup.get(lr["linked_by"], None)
        if lb:
            lb = lb.name
        else:
            lb = mon.getUser(item_lookup[lr["child"]].user).name

        r = mon.addRel(rp, rc, lb)
        r.upvotes = lr["upvotes"]
        r.downvotes = lr["downvotes"]
        # r.time_linked = tl
        r.save()
        print "inserted rels: " + str(lr["id"]) + " -- " + str(r)
        rel_lookup[lr["id"]] = r

    for lc in lite_comments:
        rel = rel_lookup.get(lc["rel_id"], None)
        if rel is None:
            continue
        rel = mon.getRel(rel._id)
        if lc["head_item"] == 0:
            cp = mon.root._id
        else:
            cp = item_lookup[lc["head_item"]]._id
        rel.comment_parent = cp
        rel.save()

        item = mon.getItem(rel.child)
        item.tags.append(u"comment")
        item.save()

    for lv in lite_votes:
        user = mon.getUser(user_lookup[lv["user_id"]].name)
        rel = rel_lookup.get(lv["rel_id"], None)
        if rel is None:
            continue
        rel_id = rel._id

        if lv["vote_type"] == "up":
            is_upvote = True
        else:
            is_upvote = False

        user.votes.append({"rel": rel_id, "is_upvote": is_upvote})
        user.save()

    db.close()
Exemple #6
0
def userItemPage(username):
    user = db.getUser(username)
    if user is None:
        return "This user doesn't exist"

    return redirect(url_for('item_page', item_id=user.item))
Exemple #7
0
def userItemPage(username):
    user = db.getUser(username)
    if user is None:
        return "This user doesn't exist"

    return redirect(url_for('item_page', item_id=user.item))