def myProfilePage(): if not session['logged_in']: return 'You are not logged in' subscriptions = db.getSubscriptions(session['username']) if subscriptions is None: return 'You have no subscriptions' s_rels, s_com_rels = [], [] s_item_ids, s_com_item_ids = [], [] for s in subscriptions: s_item_ids.append(s['item']) s_rels.extend( db.getChildRels(s['item'], { 'comment_parent': None, 'time_linked': { '$gte': s['seen'] }, })) if s['comments']: s_com_item_ids.append(s['item']) s_com_rels.extend( db.getCommentRels(s['item'], {'time_linked': { '$gte': s['comment_seen'] }})) new_rels = list( db.dbcon.relations.Relation.find({ 'parent': { '$in': s_item_ids }, 'comment_parent': None, }).sort('time_linked', -1).limit(10)) new_com_rels = list( db.dbcon.relations.Relation.find({ 'comment_parent': { '$in': s_com_item_ids } }).sort('time_linked', -1).limit(10)) items = set([db.getItem(rel.parent) for rel in new_rels + s_rels]) items.update( [db.getItem(rel.comment_parent) for rel in new_com_rels + s_com_rels]) new_dict = { 's_rels': db.prepareForClient(s_rels), 's_com_rels': db.prepareForClient(s_com_rels), 'new_rels': db.prepareForClient(new_rels), 'new_com_rels': db.prepareForClient(new_com_rels), 'items': db.prepareForClient(items) } return render_template('profile.html', nd=new_dict, tab='view-tab')
def viewItem(item_id): item_id = ObjectId(item_id) if db.getItem(item_id) is None: return 'This item does not exist' session['current_item'] = item_id need = {"parent_items": True, "child_items": True, "child_rels": True} item_info = db.getItemInfo(item_id, need, True) item_info['comment_rels'] = db.getCommentRels(item_id) item_info['comment_items'] = db.prepareForClient( [db.getItem(rel['child']) for rel in item_info['comment_rels']]) item_info['comment_rels'] = db.prepareForClient(item_info['comment_rels']) if session['logged_in']: db.markSeen(session['username'], item_id, True) return render_template('view.html', ii=item_info, tab='view-tab')
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)
def discoverPage(): new_rels = list(db.dbcon.relations.Relation.find({ 'comment_parent': None }).sort('time_linked', -1).limit(10)) new_com_rels = list(db.dbcon.relations.Relation.find({ 'comment_parent': {"$ne": None} }).sort('time_linked', -1).limit(10)) items = set([db.getItem(rel.parent) for rel in new_rels]) items.update([db.getItem(rel.comment_parent) for rel in new_com_rels]) new_dict = { 'new_rels': db.prepareForClient(new_rels), 'new_com_rels': db.prepareForClient(new_com_rels), 'items': db.prepareForClient(items) } return render_template('discover.html', nd=new_dict, tab='view-tab')
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)
def viewItem(item_id): item_id = ObjectId(item_id) if db.getItem(item_id) is None: return 'This item does not exist' session['current_item'] = item_id need = { "parent_items": True, "child_items": True, "child_rels": True } item_info = db.getItemInfo(item_id, need, True) item_info['comment_rels'] = db.getCommentRels(item_id) item_info['comment_items'] = db.prepareForClient([db.getItem(rel['child']) for rel in item_info['comment_rels']]) item_info['comment_rels'] = db.prepareForClient(item_info['comment_rels']) if session['logged_in']: db.markSeen(session['username'], item_id, True) return render_template('view.html', ii=item_info, tab='view-tab')
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)
def myProfilePage(): if not session['logged_in']: return 'You are not logged in' subscriptions = db.getSubscriptions(session['username']) if subscriptions is None: return 'You have no subscriptions' s_rels, s_com_rels = [], [] s_item_ids, s_com_item_ids = [], [] for s in subscriptions: s_item_ids.append(s['item']) s_rels.extend(db.getChildRels(s['item'], { 'comment_parent': None, 'time_linked': {'$gte': s['seen']}, })) if s['comments']: s_com_item_ids.append(s['item']) s_com_rels.extend(db.getCommentRels(s['item'], { 'time_linked': {'$gte': s['comment_seen']} })) new_rels = list(db.dbcon.relations.Relation.find({ 'parent': {'$in': s_item_ids}, 'comment_parent': None, }).sort('time_linked', -1).limit(10)) new_com_rels = list(db.dbcon.relations.Relation.find({ 'comment_parent': {'$in': s_com_item_ids} }).sort('time_linked', -1).limit(10)) items = set([db.getItem(rel.parent) for rel in new_rels + s_rels]) items.update([db.getItem(rel.comment_parent) for rel in new_com_rels + s_com_rels]) new_dict = { 's_rels': db.prepareForClient(s_rels), 's_com_rels': db.prepareForClient(s_com_rels), 'new_rels': db.prepareForClient(new_rels), 'new_com_rels': db.prepareForClient(new_com_rels), 'items': db.prepareForClient(items) } return render_template('profile.html', nd=new_dict, tab='view-tab')
def discoverPage(): new_rels = list( db.dbcon.relations.Relation.find({ 'comment_parent': None }).sort('time_linked', -1).limit(10)) new_com_rels = list( db.dbcon.relations.Relation.find({ 'comment_parent': { "$ne": None } }).sort('time_linked', -1).limit(10)) items = set([db.getItem(rel.parent) for rel in new_rels]) items.update([db.getItem(rel.comment_parent) for rel in new_com_rels]) new_dict = { 'new_rels': db.prepareForClient(new_rels), 'new_com_rels': db.prepareForClient(new_com_rels), 'items': db.prepareForClient(items) } return render_template('discover.html', nd=new_dict, tab='view-tab')
def editItem(): item_id = ObjectId(request.form['item_id']) username = request.form['username'] if db.getItem(item_id).user != unicode(username): return "Not edited" new_item = db.editItem(item_id, request.form['body'], request.form['tldr']) ret_dict = { "item": db.prepareForClient([new_item])[0] } return jsonify(ret_dict)
def retrieveRel(rel_id): rel_id = ObjectId(rel_id) rel = db.getrel(rel_id) if rel is None: return 'This rel does not exist' rel_dict = {'rel': rel} if request.args['parent']: rel_dict['parent'] = db.getItem(rel.parent) if request.args['child']: rel_dict['child'] = db.getItem(rel.child) #session['current_rel'] = rel_id return jsonify(db.prepareForClient(rel_dict))