def new_post(): title = request.form["title"] url = request.form["url"] text = request.form["text"] time = datetime.datetime.now() user = decode_auth_token(request.cookies.get('token')) if user is None: error = "ERROR: You must be logged to make a new post" return redirect("submit?error={0}".format(error)) elif url != '' and text == '' and title != '' and not Contribution.exists(repository, url): contribution = Contribution(title, url, None, time, user, ContributionTypes.NEW.value, 0) elif text != '' and url == '' and title != '': contribution = Contribution(title, None, text, time, user, ContributionTypes.ASK.value, 0) elif text != '' and url != '': error = "ERROR: You can only fill URL or Text but not both" return redirect("submit?error={0}".format(error)) elif url != '' and text == '' and Contribution.exists(repository, url): contribution_id = Contribution.get_contribution_id_by_URL(repository, url) return redirect("contribution?id={0}".format(contribution_id)) elif text == '' and url == '' and title != '': error = "ERROR: You have to fill either URL or Text" return redirect("submit?error={0}".format(error)) else: error = "ERROR: You must fill title" return redirect("submit?error={0}".format(error)) contribution.save(repository) return redirect('')
def get_contribution(): contribution_id = request.args.get('id') contribution = Contribution.get_contribution(repository, contribution_id) comments = Comment.get_comments_by_contribution(repository, contribution_id) contribution.n_comments = len(comments) username = decode_auth_token(request.cookies.get('token')) all_children = [] comments_voted = [] if username is not None: comments_voted = UserCommentVoted.get_voted(repository, username) for comment in comments: comment.voted = comment.id in [cv['comment_id'] for cv in comments_voted] children = Comment.get_comments_by_parent(repository, comment.id) all_children.extend(children) comment.children = children for child in all_children: child.voted = child.id in [cv['comment_id'] for cv in comments_voted] # TODO: Falta votar ultim child parents_comments = [] for comment in comments: if comment.id not in [c.id for c in all_children]: parents_comments.append(comment) if username is not None: user = User.get(repository, username) contributions_voted = UserContributionVoted.get_voted(repository, username) voted = contribution.id in [cv['contribution_id'] for cv in contributions_voted] return render_template('contribution.html', contribution=contribution, comments=parents_comments, user=user, voted=voted) return render_template('contribution.html', contribution=contribution, comments=parents_comments)
def edit_profile(): username = decode_auth_token(request.cookies.get('token')) token_user = request.cookies.get('token') if username is not None: user = User.get(repository, username) return render_template('editProfile.html', user=user, token=token_user) return redirect('')
def update_profile(): username = decode_auth_token(request.cookies.get('token')) if username is not None: user = User.get(repository, username) user.update(repository, request.form["about"]) return render_template('editProfile.html', user=user) return redirect('')
def user(): username = decode_auth_token(request.cookies.get('token')) user_to_show = User.get(repository, request.args.get('user')) if username is not None: user = User.get(repository, username) return render_template('profile.html', user_to_show=user_to_show, user=user) return render_template('profile.html', user_to_show=user_to_show)
def reply(): username = decode_auth_token(request.cookies.get('token')) comment_id = request.args.get('id') if username is not None: user = User.get(repository, username) comment = Comment.get_comment(repository, comment_id) return render_template('reply.html', user=user, comment=comment) return redirect('')
def new_reply(): username = decode_auth_token(request.cookies.get('token')) text = request.form["text"] contribution = request.form["contribution"] if username is not None and text: parent = request.form["parent"] comment = Comment(username, datetime.datetime.now(), text, contribution, parent) comment.save(repository) return redirect('contribution?id=' + contribution)
def submit(): username = decode_auth_token(request.cookies.get('token')) error = request.args.get('error') if username is not None: user = User.get(repository, username) if error is not None: return render_template('submit.html', user=user, error=error) elif error is None: return render_template('submit.html', user=user) return redirect('')
def create_new_ask(): if 'Authorization' not in request.headers: return jsonify(''), 401 username = decode_auth_token(request.headers['Authorization']) if username is None: return jsonify(''), 401 json = request.get_json() ask = Contribution(title=json['title'], url=None, text=json['text'], time=datetime.datetime.now(), username=username, kind=ContributionTypes.ASK.value) ask.save(repository) return jsonify(ask.toJSON())
def return_updated_user(userput): if 'Authorization' not in request.headers: return jsonify('Unauthorized'), 401 username = decode_auth_token(request.headers['Authorization']) if username is None: return jsonify('Unauthorized'), 401 if username != userput: return jsonify('Forbidden'), 403 json = request.get_json() user_to_return = User.get(repository, username) user_to_return.update(repository, json['about']) return jsonify(user_to_return.toJSON())
def create_new_comment(contribution_id): if 'Authorization' not in request.headers: return jsonify('Unauthorized'), 401 username = decode_auth_token(request.headers['Authorization']) if username is None: return jsonify('Unauthorized'), 401 if not Contribution.exists_contribution(repository, contribution_id): return jsonify('Not Found'), 404 json = request.get_json() comment = Comment(username, datetime.datetime.now(), json['text'], contribution_id, None) comment.save(repository) return jsonify(comment.toJSON())
def voteComment(): username = decode_auth_token(request.cookies.get('token')) if username is not None: comment_id = request.form['comment'] action = request.form['action'] comment_voted = UserCommentVoted(username, comment_id) if action == 'vote': comment_voted.save(repository) elif action == 'unvote': comment_voted.delete(repository) resp = make_response(redirect(request.form['view'])) return resp
def new_comment(): user = decode_auth_token(request.cookies.get('token')) time = datetime.datetime.now() contribution = request.form["contribution"] text = request.form["text"] if user is not None and text: if text != '': comment = Comment(user, time, text, contribution, None) else: return redirect(url_for('get_contribution', id=contribution)) comment.save(repository) return redirect(url_for('get_contribution', id=contribution))
def delete_comment_api(comment_id): if 'Authorization' not in request.headers: return jsonify('Unauthorized'), 401 username = decode_auth_token(request.headers['Authorization']) if username is None: return jsonify('Unauthorized'), 401 if not Comment.exists_comment(repository, comment_id): return jsonify('Not Found'), 404 comment = Comment.get_comment(repository, comment_id) if comment.username != username: return jsonify('Forbidden'), 403 Comment.delete_comment(repository, comment_id) return jsonify('Successful delete'), 204
def new(): contributions = Contribution.get_contributions_new(repository) username = decode_auth_token(request.cookies.get('token')) for c in contributions: aux = Comment.get_number_comments_by_contribution(repository, str(c['id'])) c['n_comments'] = aux[0]['n_comments'] if username is not None: user = User.get(repository, username) contributions_voted = UserContributionVoted.get_voted(repository, username) for c in contributions: c.voted = c['id'] in [cv['contribution_id'] for cv in contributions_voted] return render_template('home.html', contributions=contributions, user=user) return render_template('home.html', contributions=contributions)
def create_new_new(): if 'Authorization' not in request.headers: return jsonify('Unauthorized'), 401 username = decode_auth_token(request.headers['Authorization']) if username is None: return jsonify(''), 401 json = request.get_json() new = Contribution(title=json['title'], url=json['url'], text=None, time=datetime.datetime.now(), username=username, kind=ContributionTypes.NEW.value) if Contribution.exists(repository, new.url): new = Contribution.get_contribution_by_url(repository, new.url) return jsonify(new.toJSON()), 409 new.save(repository) return jsonify(new.toJSON())
def threads(): username = decode_auth_token(request.cookies.get('token')) email = request.args.get('id', default=username) if username is not None: user = User.get(repository, username) comments = Comment.get_comments_by_user(repository, email) all_children = [] for comment in comments: children = Comment.get_comments_by_parent(repository, comment.id) all_children.extend(children) comment.children = children first_level_comments = [] for comment in comments: if not comment.id in [c.id for c in all_children]: first_level_comments.append(comment) return render_template('threads.html', comments=first_level_comments, user=user) return redirect('')
def vote_comment_api(comment_id): if 'Authorization' not in request.headers: return jsonify('Unauthorized'), 401 username = decode_auth_token(request.headers['Authorization']) if username is None: return jsonify('Unauthorized'), 401 if not Comment.exists_comment(repository, comment_id): return jsonify('Not Found'), 404 comment = Comment.get_comment(repository, comment_id) if comment.username == username: return jsonify('Forbidden'), 403 comment_voted = UserCommentVoted(username, comment_id) if request.method == 'POST': if UserCommentVoted.exists(repository, comment_id, username): return jsonify('Conflict'), 409 comment_voted.save(repository) elif request.method == 'DELETE': if not UserCommentVoted.exists(repository, comment_id, username): return jsonify('Not Found'), 404 comment_voted.delete(repository) return return_asked_contribution(str(comment.contribution_id))