def ask(): contributions = Contribution.get_asks(repository) username = decode_auth_token(request.cookies.get('token')) if username is not None: contributions_voted = UserContributionVoted.get_voted(repository, username) for c in contributions: c.voted = c['id'] in [cv['contribution_id'] for cv in contributions_voted] aux = Comment.get_number_comments_by_contribution(repository, str(c['id'])) c['n_comments'] = aux[0]['n_comments'] user = User.get(repository, username) return render_template('home.html', contributions=contributions, user=user) else: for c in contributions: aux = Comment.get_number_comments_by_contribution(repository, str(c['id'])) c.n_comments = aux[0]['n_comments'] return render_template('home.html', contributions=contributions)
def return_asks(): contributions = Contribution.get_asks(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'], "text": c['text'], "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')