Clean up validation of price selection. Add error messaging.
This commit is contained in:
@@ -19,14 +19,15 @@ from student.views import course_from_id
|
||||
class ChooseModeView(View):
|
||||
|
||||
@method_decorator(login_required)
|
||||
def get(self, request):
|
||||
def get(self, request, error=None):
|
||||
course_id = request.GET.get("course_id")
|
||||
modes = CourseMode.modes_for_course_dict(course_id)
|
||||
context = {
|
||||
"course_id": course_id,
|
||||
"modes": modes,
|
||||
"course_name": course_from_id(course_id).display_name,
|
||||
"chosen_price" : None,
|
||||
"chosen_price": None,
|
||||
"error": error,
|
||||
}
|
||||
if "verified" in modes:
|
||||
context["suggested_prices"] = modes["verified"].suggested_prices.split(",")
|
||||
@@ -43,7 +44,8 @@ class ChooseModeView(View):
|
||||
# but I don't really have the time to refactor it more nicely and test.
|
||||
course = course_from_id(course_id)
|
||||
if not has_access(user, course, 'enroll'):
|
||||
return HttpResponseBadRequest(_("Enrollment is closed"))
|
||||
error_msg = _("Enrollment is closed")
|
||||
return self.get(request, error=error_msg)
|
||||
|
||||
requested_mode = self.get_requested_mode(request.POST.get("mode"))
|
||||
|
||||
@@ -55,16 +57,20 @@ class ChooseModeView(View):
|
||||
CourseEnrollment.enroll(user, course_id)
|
||||
return redirect('dashboard')
|
||||
|
||||
mode_info = allowed_modes[requested_mode]
|
||||
|
||||
if requested_mode == "verified":
|
||||
amount = request.POST.get("contribution") or \
|
||||
request.POST.get("contribution-other-amt") or \
|
||||
requested_mode.min_price.format("{:g}")
|
||||
request.POST.get("contribution-other-amt") or 0
|
||||
|
||||
donation_for_course = request.session.get("donation_for_course", {})
|
||||
donation_for_course[course_id] = amount
|
||||
request.session["donation_for_course"] = donation_for_course
|
||||
|
||||
# TODO: Check here for minimum pricing
|
||||
# Check for minimum pricing
|
||||
if int(amount) < mode_info.min_price:
|
||||
error_msg = _("No selected price or selected price is too low.")
|
||||
return self.get(request, error=error_msg)
|
||||
|
||||
return redirect(
|
||||
"{}?{}".format(
|
||||
@@ -75,7 +81,7 @@ class ChooseModeView(View):
|
||||
|
||||
def get_requested_mode(self, user_choice):
|
||||
choices = {
|
||||
"Select Audit" : "audit",
|
||||
"Select Certificate" : "verified"
|
||||
"Select Audit": "audit",
|
||||
"Select Certificate": "verified"
|
||||
}
|
||||
return choices.get(user_choice)
|
||||
|
||||
@@ -25,6 +25,11 @@
|
||||
<header class="page-header header-white">
|
||||
<h2 class="title header-white-title">You are registering for ${course_name} (ID Verified)</h2>
|
||||
</header>
|
||||
%if error:
|
||||
<div class="error">
|
||||
${error}
|
||||
</div>
|
||||
%endif
|
||||
<h3 class="title">Select your track:</h3>
|
||||
|
||||
<form method="post" name="enrollment_mode_form" id="enrollment_mode_form">
|
||||
|
||||
@@ -104,7 +104,7 @@ def render_purchase_form_html(cart):
|
||||
"""
|
||||
return render_to_string('shoppingcart/cybersource_form.html', {
|
||||
'action': get_purchase_endpoint(),
|
||||
'params': get_signed_purchase_params(params),
|
||||
'params': get_signed_purchase_params(cart),
|
||||
})
|
||||
|
||||
def get_signed_purchase_params(cart):
|
||||
|
||||
@@ -9,9 +9,10 @@ from mitxmako.shortcuts import render_to_response
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpResponse
|
||||
from django.http import HttpResponse, HttpResponseBadRequest
|
||||
from django.shortcuts import redirect
|
||||
from django.views.generic.base import View
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from course_modes.models import CourseMode
|
||||
from student.models import CourseEnrollment
|
||||
@@ -47,14 +48,14 @@ class VerifyView(View):
|
||||
else:
|
||||
chosen_price = verify_mode.min_price.format("{:g}")
|
||||
context = {
|
||||
"progress_state" : progress_state,
|
||||
"user_full_name" : request.user.profile.name,
|
||||
"course_id" : course_id,
|
||||
"course_name" : course_from_id(course_id).display_name,
|
||||
"purchase_endpoint" : get_purchase_endpoint(),
|
||||
"suggested_prices" : verify_mode.suggested_prices.split(","),
|
||||
"currency" : verify_mode.currency.upper(),
|
||||
"chosen_price" : chosen_price,
|
||||
"progress_state": progress_state,
|
||||
"user_full_name": request.user.profile.name,
|
||||
"course_id": course_id,
|
||||
"course_name": course_from_id(course_id).display_name,
|
||||
"purchase_endpoint": get_purchase_endpoint(),
|
||||
"suggested_prices": verify_mode.suggested_prices.split(","),
|
||||
"currency": verify_mode.currency.upper(),
|
||||
"chosen_price": chosen_price,
|
||||
}
|
||||
|
||||
return render_to_response('verify_student/photo_verification.html', context)
|
||||
@@ -66,17 +67,20 @@ def create_order(request):
|
||||
attempt.save()
|
||||
|
||||
course_id = request.POST['course_id']
|
||||
donation_for_course = request.session.get("donation_for_course", {})
|
||||
contribution = request.POST.get("contribution", 0)
|
||||
|
||||
# FIXME: When this isn't available we do...?
|
||||
verified_mode = CourseMode.modes_for_course_dict(course_id)["verified"]
|
||||
amount = donation_for_course.get(course_id, verified_mode.min_price)
|
||||
verified_mode = CourseMode.modes_for_course_dict(course_id).get('verified', None)
|
||||
# make sure this course has a verified mode
|
||||
if not verified_mode:
|
||||
return HttpResponseBadRequest(_("This course doesn't support verified certificates"))
|
||||
|
||||
if contribution < verified_mode.min_price:
|
||||
return HttpResponseBadRequest(_("No selected price or selected price is below minimum."))
|
||||
|
||||
# I know, we should check this is valid. All kinds of stuff missing here
|
||||
# enrollment = CourseEnrollment.create_enrollment(request.user, course_id)
|
||||
cart = Order.get_cart_for_user(request.user)
|
||||
cart.clear()
|
||||
CertificateItem.add_to_order(cart, course_id, amount, 'verified')
|
||||
CertificateItem.add_to_order(cart, course_id, contribution, 'verified')
|
||||
|
||||
params = get_signed_purchase_params(cart)
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
.done(function(data) {
|
||||
$("#pay_form").submit();
|
||||
})
|
||||
.fail(function() { alert("error"); });
|
||||
.fail(function(jqXhr,text_status, error_thrown) { alert(jqXhr.responseText); });
|
||||
}
|
||||
|
||||
function initSnapshotHandler(names) {
|
||||
|
||||
Reference in New Issue
Block a user