Commit 2f4aba80 by Bridger Maxwell

If a user is anonymous when they click the enroll button, they will…

If a user is anonymous when they click the enroll button, they will automatically be enrolled in the course if they immediately log in our create an account.
parent 60b54727
......@@ -10,10 +10,10 @@
<hr>
</header>
<div id="enroll">
<div id="register">
<form id="enroll_form" method="post">
<div id="enroll_error" name="enroll_error"></div>
<form id="register_form" method="post">
<div id="register_error" name="register_error"></div>
<label>E-mail</label>
<input name="email" type="email" placeholder="E-mail">
<label>Password</label>
......@@ -64,17 +64,17 @@
});
}
$('form#enroll_form').submit(function(e) {
$('form#register_form').submit(function(e) {
e.preventDefault();
var submit_data = $('#enroll_form').serialize();
var submit_data = $('#register_form').serialize();
postJSON('/create_account',
submit_data,
function(json) {
if(json.success) {
$('#enroll').html(json.value);
$('#register').html(json.value);
} else {
$('#enroll_error').html(json.value).stop().css("background-color", "#933").animate({ backgroundColor: "#333"}, 2000);
$('#register_error').html(json.value).stop().css("background-color", "#933").animate({ backgroundColor: "#333"}, 2000);
}
}
);
......
......@@ -79,6 +79,9 @@ def index(request):
return render_to_response('index.html', {'universities': universities, 'entries': entries})
def course_from_id(id):
course_loc = CourseDescriptor.id_to_location(id)
return modulestore().get_item(course_loc)
@login_required
@ensure_csrf_cookie
......@@ -86,10 +89,6 @@ def dashboard(request):
user = request.user
enrollments = CourseEnrollment.objects.filter(user=user)
def course_from_id(id):
course_loc = CourseDescriptor.id_to_location(id)
return modulestore().get_item(course_loc)
# Build our courses list for the user, but ignore any courses that no longer
# exist (because the course IDs have changed). Still, we don't delete those
# enrollments, because it could have been a data push snafu.
......@@ -110,6 +109,45 @@ def dashboard(request):
return render_to_response('dashboard.html', context)
@login_required
def change_enrollment_view(request):
return HttpResponse(json.dumps(change_enrollment(request)))
def change_enrollment(request):
if request.method != "POST":
raise Http404
action = request.POST.get("enrollment_action" , "")
user = request.user
course_id = request.POST.get("course_id", None)
if course_id == None:
return HttpResponse(json.dumps({'success': False, 'error': 'There was an error receiving the course id.'}))
if action == "enroll":
# Make sure the course exists
# We don't do this check on unenroll, or a bad course id can't be unenrolled from
try:
course = course_from_id(course_id)
except ItemNotFoundError:
log.error("User {0} tried to enroll in non-existant course {1}"
.format(user.username, enrollment.course_id))
return {'success': False, 'error': 'The course requested does not exist.'}
enrollment, created = CourseEnrollment.objects.get_or_create(user=user, course_id=course.id)
return {'success': True}
elif action == "unenroll":
try:
enrollment = CourseEnrollment.objects.get(user=user, course_id=course_id)
enrollment.delete()
return {'success': True}
except CourseEnrollment.DoesNotExist:
return {'success': False, 'error': 'You are not enrolled for this course.'}
else:
return {'success': False, 'error': 'Invalid enrollment_action.'}
return {'success': False, 'error': 'We weren\'t able to unenroll you. Please try again.'}
# Need different levels of logging
@ensure_csrf_cookie
def login_user(request, error=""):
......@@ -147,6 +185,16 @@ def login_user(request, error=""):
log.exception(e)
log.info("Login success - {0} ({1})".format(username, email))
if 'enrollment_action' in request.POST:
try:
enrollment_output = change_enrollment(request)
# There isn't really a way to display the results to the user, so we just log it
# We expect the enrollment to be a success, and will show up on the dashboard anyway
log.info("Attempted to automatically enroll after login. Results: {0}".format(enrollment_output))
except Exception, e:
log.error("Exception automatically enrolling after login: {0}".format(str(e)))
return HttpResponse(json.dumps({'success':True}))
log.warning("Login failed - Account not active for user {0}".format(username))
......@@ -287,6 +335,15 @@ def create_account(request, post_override=None):
login(request, login_user)
request.session.set_expiry(0)
if 'enrollment_action' in request.POST:
try:
enrollment_output = change_enrollment(request)
# There isn't really a way to display the results to the user, so we just log it
# We expect the enrollment to be a success, and will show up on the dashboard anyway
log.info("Attempted to automatically enroll after login. Results: {0}".format(enrollment_output))
except Exception, e:
log.error("Exception automatically enrolling after login: {0}".format(str(e)))
js={'success': True}
return HttpResponse(json.dumps(js), mimetype="application/json")
......
......@@ -38,7 +38,7 @@ def cache_if_anonymous(view_func):
@wraps(view_func)
def _decorated(request, *args, **kwargs):
if not request.user.is_authenticated():
if False and not request.user.is_authenticated():
#Use the cache
cache_key = "cache_if_anonymous." + request.path
response = cache.get(cache_key)
......
......@@ -269,39 +269,6 @@ def course_about(request, course_id):
course = check_course(course_id, course_must_be_open=False)
registered = registered_for_course(course, request.user)
return render_to_response('portal/course_about.html', {'course': course, 'registered': registered})
@login_required
def change_enrollment(request):
if request.method != "POST":
raise Http404
course_id = request.POST.get("course_id", None)
if course_id == None:
return HttpResponse(json.dumps({'success': False, 'error': 'There was an error receiving the course id.'}))
action = request.POST.get("enrollment_action" , "")
user = request.user
if action == "enroll":
# Make sure the course exists
# We don't do this check on unenroll, or a bad course id can't be unenrolled from
course = check_course(course_id, course_must_be_open=False)
enrollment, created = CourseEnrollment.objects.get_or_create(user=user, course_id=course.id)
return HttpResponse(json.dumps({'success': True}))
elif action == "unenroll":
try:
enrollment = CourseEnrollment.objects.get(user=user, course_id=course_id)
enrollment.delete()
return HttpResponse(json.dumps({'success': True}))
except CourseEnrollment.DoesNotExist:
return HttpResponse(json.dumps({'success': False, 'error': 'You are not enrolled for this course.'}))
else:
return HttpResponse(json.dumps({'success': False, 'error': 'Invalid enrollment_action.'}))
return HttpResponse(json.dumps({'success': False, 'error': 'We weren\'t able to unenroll you. Please try again.'}))
......
......@@ -84,7 +84,7 @@ div.leanModal_box {
form {
text-align: left;
div#enroll_error, div#login_error, div#pwd_error {
div#register_error, div#login_error, div#pwd_error {
$error-color: #333;
background-color: $error-color;
border: darken($error-color, 20%);
......
......@@ -25,7 +25,7 @@
%if registered:
<span class="register disabled">You are registered for this course (${course.number}).</span>
%else:
<a href="#" class="register submit_registration">Register for ${course.number}</a>
<a href="#" class="register">Register for ${course.number}</a>
%endif
%else:
<a href="#signup-modal" class="register" rel="leanModal" data-notice="You must Sign Up in order to register">Register for ${course.number}</a>
......@@ -108,28 +108,49 @@
</section>
%if not registered:
<div display="hidden">
<form id="enroll_form" method="post" data-remote="true" action="${reverse('change_enrollment')}">
<input name="course_id" type="hidden" value="${course.id}">
<input name="enrollment_action" type="hidden" value="enroll">
<div style="display: none;">
<form id="class_enroll_form" method="post" data-remote="true" action="${reverse('change_enrollment')}">
<fieldset class="enroll_fieldset">
<input name="course_id" type="hidden" value="${course.id}">
<input name="enrollment_action" type="hidden" value="enroll">
</fieldset>
<div class="submit">
<input name="submit" type="submit" value="enroll">
</div>
</form>
</div>
<script type="text/javascript">
(function() {
$(".submit_registration").click(function() {
$("#enroll_form").submit();
});
%if user.is_authenticated():
## If the user is authenticated, clicking the enroll button just submits a form
<script type="text/javascript">
(function() {
$(".register").click(function() {
$("#class_enroll_form").submit();
});
$(document).delegate('#enroll_form', 'ajax:success', function(data, json, xhr) {
if(json.success) {
location.href="${reverse('dashboard')}";
}
});
})(this)
</script>
$(document).delegate('#class_enroll_form', 'ajax:success', function(data, json, xhr) {
if(json.success) {
location.href="${reverse('dashboard')}";
}
});
})(this)
</script>
%else:
## If the user is not authenticated, clicking the enroll button pops up the register
## field. We also slip in the registration fields into the login/register fields so
## the user is automatically registered after logging in / registering
<script type="text/javascript">
(function() {
$(".register").click(function() {
if ($("#login_form .enroll_fieldset").length === 0) {
$("#login_form").append( $(".enroll_fieldset").first().clone() );
}
if ($("#register_form .enroll_fieldset").length === 0) {
$("#register_form").append( $(".enroll_fieldset").first().clone() );
}
});
})(this)
</script>
%endif
%endif
......
......@@ -7,16 +7,16 @@
<section id="signup-modal" class="modal signup-modal">
<div class="inner-wrapper">
<div id="enroll">
<div id="register">
<header>
<h2>Sign Up for <span class="edx">edX</span></h2>
<hr>
</header>
<form id="enroll_form" method="post" data-remote="true" action="/create_account">
<form id="register_form" method="post" data-remote="true" action="/create_account">
<div class="notice"></div>
<div id="enroll_error" class="modal-form-error" name="enroll_error"></div>
<div id="enroll_error" name="enroll_error"></div>
<div id="register_error" class="modal-form-error" name="register_error"></div>
<div id="register_error" name="register_error"></div>
<div class="input-group">
<label>E-mail*</label>
......@@ -119,11 +119,11 @@
<script type="text/javascript">
(function() {
$(document).delegate('#enroll_form', 'ajax:success', function(data, json, xhr) {
$(document).delegate('#register_form', 'ajax:success', function(data, json, xhr) {
if(json.success) {
location.href="${reverse('dashboard')}";
} else {
$('#enroll_error').html(json.value).stop().css("display", "block");
$('#register_error').html(json.value).stop().css("display", "block");
}
});
})(this)
......
......@@ -101,7 +101,7 @@ if settings.COURSEWARE_ENABLED:
url(r'^courses/?$', 'courseware.views.courses', name="courses"),
url(r'^change_enrollment$',
'courseware.views.change_enrollment', name="change_enrollment"),
'student.views.change_enrollment_view', name="change_enrollment"),
#About the course
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/about$',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment