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 return_newest_contributions(): contributions = Contribution.get_contributions_new(repository) for c in contributions: aux = Comment.get_number_comments_by_contribution(repository, str(c['id'])) c['n_comments'] = aux[0]['n_comments'] aux = UserContributionVoted.get_votes_contribution(repository, str(c['id'])) contribution_votes = [] for aux_v in aux: contribution_votes.append(aux_v['username']) c['contribution_votes'] = contribution_votes return Response(json.dumps(contributions), mimetype='application/json')
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 voteContribution(): username = decode_auth_token(request.cookies.get('token')) if username is not None: contribution_id = request.form['contribution'] action = request.form['action'] contribution_voted = UserContributionVoted(username, contribution_id) if action == 'vote': contribution_voted.save(repository) elif action == 'unvote': contribution_voted.delete(repository) resp = make_response(redirect(request.form['view'])) return resp
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)
def return_news(): contributions = Contribution.get_news_home(repository) news_to_show = [] for c in contributions: aux = Comment.get_number_comments_by_contribution(repository, str(c['id'])) c['n_comments'] = aux[0]['n_comments'] aux = UserContributionVoted.get_votes_contribution(repository, str(c['id'])) contribution_votes = [] for aux_v in aux: contribution_votes.append(aux_v['username']) c['contribution_votes'] = contribution_votes new_attributes = { "id": c['id'], "title": c['title'], "url": c['url'], "time": c['time'], "user": c['user'], "n_votes": c['n_votes'], "n_comments": c['n_comments'], "contribution_votes": c['contribution_votes'] } news_to_show.append(new_attributes) return Response(json.dumps(news_to_show), mimetype='application/json')
if time_ago < 60: return str(int(round(time_ago))) + " seconds" time_ago = time_ago / 60 if time_ago < 60: return str(int(round(time_ago))) + " minutes" time_ago = time_ago / 60 if time_ago / 60 < 24: return str(int(round(time_ago))) + " hours" time_ago = time_ago / 24 if time_ago < 12: return str(int(round(time_ago))) + " months" time_ago = time_ago / 12 return str(int(round(time_ago))) + " years" if __name__ == '__main__': repository = Persistence(os.environ['DB_PATH'], logging.getLogger(__name__)) repository.init_db( [User.get_table_creation(), Contribution.get_table_creation(), UserContributionVoted.get_table_creation(), Comment.get_table_creation(), UserCommentVoted.get_table_creation()]) basicConfig(filename=os.environ['LOG'], level=INFO) app.config.update(TEMPLATES_AUTO_RELOAD=True) app.run(host=str(os.environ['HOST']), port=int(os.environ['PORT']), threaded=True)