예제 #1
0
def rating(demand_id, recipient):
    """
    The '/bid/<demand_id>/rating/<recipient>' route is where the user can rate another user 
    for a demand they were involved in.
    """
    if 'username' not in session:
        return redirect(url_for('login'))

    demand_info = Demand.get_info(demand_id)

    # make sure the user is authorized to rate the recipient
    if session['role'] == 'developer':
        # developer rates the client, so client is recipient
        if session['username'] != demand_info['chosen_developer_username']:
            return render_template('access_denied.html')
    elif session['role'] == 'client':
        # client rates the developer, so developer is recipient
        if session['username'] != demand_info['client_username']:
            return render_template('access_denied.html')

    if Rating.check_if_valid_rating_form(int(demand_id), recipient,
                                         session['username']):
        form = RatingForm()

        if request.method == "GET":
            return render_template("rating.html",
                                   form=form,
                                   recipient=recipient,
                                   demand_id=demand_id)
        elif request.method == "POST":
            # low rating
            if form.rating.data <= 2:
                session['rating' + demand_id] = form.rating.data
                return redirect(
                    url_for('ratingMessage',
                            demand_id=demand_id,
                            recipient=recipient))
            elif form.rating.data == None:
                return render_template('rating.html',
                                       form=form,
                                       recipient=recipient,
                                       demand_id=demand_id)
            else:
                # add to form data
                Rating(demand_id, recipient, session['username'],
                       form.rating.data)

                # if the client gave a good rating to a developer (<= 3)
                # the remaining half of the bid amount gets transferred over to the developer
                if session['role'] == 'client':
                    bid_amount = Demand.get_info(
                        demand_id)['chosen_bid_amount']
                    Transaction(recipient, session['username'],
                                round(bid_amount / 2, 2))

                    # update developer's earnings
                    Developer.add_earnings(recipient, bid_amount)

                return render_template('ratingFinished.html',
                                       recipient=recipient)
    return render_template('access_denied.html')