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 delete_contribution_api(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 contribution = Contribution.get_contribution(repository, contribution_id) if contribution.username != username: return jsonify('Forbidden'), 403 Comment.delete_comments_from_contribution(repository, contribution_id) Contribution.delete_contribution(repository, contribution_id) return jsonify('Successful delete'), 204
def vote_contribution_api(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 contribution = Contribution.get_contribution(repository, contribution_id) if contribution.username == username: return jsonify('Forbidden'), 403 contribution_voted = UserContributionVoted(username, contribution_id) if request.method == 'POST': if UserContributionVoted.exists(repository, contribution_id, username): return jsonify('Conflict'), 409 contribution_voted.save(repository) elif request.method == 'DELETE': if not UserContributionVoted.exists(repository, contribution_id, username): return jsonify('Not Found'), 404 contribution_voted.delete(repository) return return_asked_contribution(contribution_id)
def return_asked_contribution(contribution_id): if not Contribution.exists_contribution(repository, contribution_id): return jsonify('Not Found'), 404 contribution_to_show = Contribution.get_contribution(repository, contribution_id) contribution = { "id": contribution_to_show.id, "title": contribution_to_show.title, "url": contribution_to_show.url, "text": contribution_to_show.text, "time": contribution_to_show.time, "user": contribution_to_show.username, "kind": contribution_to_show.kind, "n_votes": contribution_to_show.n_votes, "comments": get_contribution_comments(contribution_id) } votes = UserContributionVoted.get_votes_contribution(repository, contribution_id) contribution_votes = [] for vote in votes: contribution_votes.append(vote['username']) contribution['contribution_votes'] = contribution_votes contribution['n_comments'] = Comment.get_number_comments_by_contribution(repository, contribution_id)[0][ 'n_comments'] return jsonify(contribution)