def buy_solution(problem_name, seller_name):
    current_problem = None
    error = None
    for name, problem in parsed_problem_file:
        if name == problem_name:
            current_problem = lg.Problem.fromString(problem)
            break

    if not current_problem:
        return render_template(
            "error_page.html", logged_in_user=current_user, error="You entered a URL for a non-existent problem."
        )

    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)

    if not problem_name in (name for name, _ in categorized_problems["to_solve"]):
        return render_template(
            "error_page.html",
            logged_in_user=current_user,
            error="You have either solved the problem or you are not allowed to access it yet.",
        )

    all_commodities = rc.list_all_commodities(User.user_database, user_pool)
    found = False
    for prob, user, price_requested in all_commodities:
        if prob == problem_name and user == seller_name:
            found = True
            break
    if not found:
        error = "The seller is not selling the solution to this problem."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    if get_user_points(username, user_pool) < price_requested:
        error = "You don't have enough points to buy the solution to this problem."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    rc.pay_user(seller_name, price_requested, user_pool)
    rc.charge_user(current_user.name, price_requested, user_pool)
    result, points = rc.submit_solution(current_problem.answer, current_problem.name, problem_pool)
    if not result:
        error = "Something really bad happened."
        return render_template("error_page.html", logged_in_user=current_user, error=error)
    else:
        rc.update_user_solution(points, current_problem.name, current_user.name, user_pool)
        rc.add_to_purchases(current_user.name, current_problem.name, user_pool)
        log_purchase(current_user.name, seller_name, current_problem.name, price_requested, points)
        message = "You bought the solution of {0} from {1} at the price of {2} points. Check the stats page for the solution".format(
            current_problem.display_name.lower(), get_display_name(seller_name), price_requested
        )
        return render_template("success_page.html", logged_in_user=current_user, message=message)
def post_solution(prob_name):
    current_problem = None
    error = None
    for name, problem in parsed_problem_file:
        if name == prob_name:
            current_problem = lg.Problem.fromString(problem)
            break

    if not current_problem:
        return render_template(
            "error_page.html", logged_in_user=current_user, error="You entered a URL for a non-existent problem."
        )

    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)

    if not prob_name in (name for name, _ in categorized_problems["to_solve"]):
        return render_template(
            "error_page.html",
            logged_in_user=current_user,
            error="You have either solved the problem or you are not allowed to solve it.",
        )

    form = AnswerForm(request.form)
    if request.method == "POST":
        submitted_answer = request.form["answer"]
        if submitted_answer != current_problem.answer:
            if not submitted_answer:
                error = "Enter an answer"
            else:
                error = "Wrong answer. Try again."
            return render_template(
                "submit_answer.html",
                form=form,
                error=error,
                logged_in_user=current_user,
                current_problem=current_problem,
            )
        else:
            result, points = rc.submit_solution(submitted_answer, current_problem.name, problem_pool)
            if result:
                status = rc.update_user_solution(points, current_problem.name, current_user.name, user_pool)
                if not status:
                    return render_template(
                        "error_page.html",
                        logged_in_user=current_user,
                        error="Something went wrong. Try submitting your answer again.",
                    )
                else:
                    log_submission(current_user.name, current_problem.name, points)
                    return render_template(
                        "success_page.html",
                        logged_in_user=current_user,
                        message="Your answer was correct. You gained {0} points.".format(points),
                    )

    return render_template(
        "submit_answer.html", form=form, error=error, logged_in_user=current_user, current_problem=current_problem
    )