Beispiel #1
0
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())
Beispiel #2
0
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
Beispiel #3
0
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)
Beispiel #4
0
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)