From 61105134aaeb893584ea3c630eadf49eefc990ec Mon Sep 17 00:00:00 2001
From: Bridger Maxwell <bridgeyman@gmail.com>
Date: Sun, 22 Jul 2012 14:52:49 -0400
Subject: [PATCH] Cleaned up activation pages. Added "Log In" link to notice
 when anonymous user tries to enroll in course.

---
 common/djangoapps/student/views.py            | 45 ++++++++++---------
 lms/static/js/toggle_login_modal.js           | 14 ++++--
 lms/templates/activation_active.html          | 14 ------
 lms/templates/activation_complete.html        | 14 ------
 lms/templates/portal/course_about.html        |  2 +-
 .../registration/activation_complete.html     | 30 +++++++++++++
 .../activation_invalid.html                   |  4 +-
 7 files changed, 68 insertions(+), 55 deletions(-)
 delete mode 100644 lms/templates/activation_active.html
 delete mode 100644 lms/templates/activation_complete.html
 create mode 100644 lms/templates/registration/activation_complete.html
 rename lms/templates/{ => registration}/activation_invalid.html (86%)

diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py
index 1f7ea626a57..6925acf1841 100644
--- a/common/djangoapps/student/views.py
+++ b/common/djangoapps/student/views.py
@@ -109,6 +109,24 @@ def dashboard(request):
     return render_to_response('dashboard.html', context)
 
 
+def try_change_enrollment(request):
+    """
+    This method calls change_enrollment if the necessary POST 
+    parameters are present, but does not return anything. It
+    simply logs the result or exception. This is usually
+    called after a registration or login, as secondary action.
+    It should not interrupt a successful registration or login.
+    """
+    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)))
+    
+
 @login_required
 def change_enrollment_view(request):
     return HttpResponse(json.dumps(change_enrollment(request)))
@@ -186,14 +204,7 @@ def login_user(request, error=""):
 
         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)))
+        try_change_enrollment(request)
         
         return HttpResponse(json.dumps({'success':True}))
 
@@ -335,14 +346,7 @@ 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)))
+    try_change_enrollment(request)
     
     js={'success': True}
     return HttpResponse(json.dumps(js), mimetype="application/json")
@@ -374,14 +378,15 @@ def activate_account(request, key):
     '''
     r=Registration.objects.filter(activation_key=key)
     if len(r)==1:
+        user_logged_in = request.user.is_authenticated()
+        already_active = True
         if not r[0].user.is_active:
             r[0].activate()
-            resp = render_to_response("activation_complete.html",{'csrf':csrf(request)['csrf_token']})
-            return resp
-        resp = render_to_response("activation_active.html",{'csrf':csrf(request)['csrf_token']})
+            already_active = False
+        resp = render_to_response("registration/activation_complete.html",{'user_logged_in':user_logged_in, 'already_active' : already_active})
         return resp
     if len(r)==0:
-        return render_to_response("activation_invalid.html",{'csrf':csrf(request)['csrf_token']})
+        return render_to_response("registration/activation_invalid.html",{'csrf':csrf(request)['csrf_token']})
     return HttpResponse("Unknown error. Please e-mail us to let us know how it happened.")
 
 @ensure_csrf_cookie
diff --git a/lms/static/js/toggle_login_modal.js b/lms/static/js/toggle_login_modal.js
index 4f07f81bb52..663a04453d8 100644
--- a/lms/static/js/toggle_login_modal.js
+++ b/lms/static/js/toggle_login_modal.js
@@ -7,9 +7,11 @@
         closeButton: null,
         position: 'fixed'
       }
-
-      var overlay = $("<div id='lean_overlay'></div>");
-      $("body").append(overlay);
+      
+      if ($("#lean_overlay").length == 0) {
+        var overlay = $("<div id='lean_overlay'></div>");
+        $("body").append(overlay);
+      }
 
       options =  $.extend(defaults, options);
 
@@ -51,7 +53,11 @@
           $(modal_id).find(".notice").hide().html("");
           var notice = $(this).data('notice')
           if(notice !== undefined) {
-            $(modal_id).find(".notice").show().html(notice);
+            $notice = $(modal_id).find(".notice");
+            $notice.show().html(notice);
+            // This is for activating leanModal links that were in the notice. We should have a cleaner way of
+            // allowing all dynamically added leanmodal links to work.
+            $notice.find("a[rel*=leanModal]").leanModal({ top : 120, overlay: 1, closeButton: ".close-modal", position: 'absolute' });
           }
           window.scrollTo(0, 0);
           e.preventDefault();
diff --git a/lms/templates/activation_active.html b/lms/templates/activation_active.html
deleted file mode 100644
index b3ac51d0551..00000000000
--- a/lms/templates/activation_active.html
+++ /dev/null
@@ -1,14 +0,0 @@
-<%! from django.core.urlresolvers import reverse %>
-<%inherit file="main.html" />
-
-<%namespace name='static' file='static_content.html'/>
-
-<section class="container activation">
-
-  <section class="message">
-    <h1>Account already active!</h1>
-    <hr class="horizontal-divider">
-
-    <p> This account has already been activated. You can now <a href="#login-modal" rel="leanModal">login</a>.</p>
-  </section>
-</section>
diff --git a/lms/templates/activation_complete.html b/lms/templates/activation_complete.html
deleted file mode 100644
index f48e8896b46..00000000000
--- a/lms/templates/activation_complete.html
+++ /dev/null
@@ -1,14 +0,0 @@
-<%! from django.core.urlresolvers import reverse %>
-<%inherit file="main.html" />
-
-<%namespace name='static' file='static_content.html'/>
-
-<section class="container activation">
-
-  <section class="message">
-    <h1 class="valid">Activation Complete!</h1>
-    <hr class="horizontal-divider">
-
-    <p>Thanks for activating your account. You can now <a href="#login-modal" rel="leanModal">login</a>.</p>
-  </section>
-</section>
diff --git a/lms/templates/portal/course_about.html b/lms/templates/portal/course_about.html
index 730e3f89cfa..3a33723949e 100644
--- a/lms/templates/portal/course_about.html
+++ b/lms/templates/portal/course_about.html
@@ -28,7 +28,7 @@
                 <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>
+            <a href="#signup-modal" class="register" rel="leanModal" data-notice='You must Sign Up or <a href="#login-modal" rel="leanModal">Log In</a> to enroll.'>Register for ${course.number}</a>
           %endif
         </div>
 
diff --git a/lms/templates/registration/activation_complete.html b/lms/templates/registration/activation_complete.html
new file mode 100644
index 00000000000..7d3579b34e0
--- /dev/null
+++ b/lms/templates/registration/activation_complete.html
@@ -0,0 +1,30 @@
+<%! from django.core.urlresolvers import reverse %>
+<%inherit file="../main.html" />
+
+<%namespace name='static' file='../static_content.html'/>
+
+<section class="container activation">
+
+  <section class="message">
+    %if not already_active:
+      <h1 class="valid">Activation Complete!</h1>
+    %else:
+      <h1>Account already active!</h1>
+    %endif
+    <hr class="horizontal-divider">
+    
+    <p>
+      %if not already_active:
+        Thanks for activating your account. 
+      %else:
+        This account has already been activated.
+      %endif
+      
+      %if user_logged_in:
+        Visit your <a href="${reverse('dashboard')}">dashboard</a> to see your courses.
+      %else:
+        You can now <a href="#login-modal" rel="leanModal">login</a>.
+      %endif
+    </p>
+  </section>
+</section>
diff --git a/lms/templates/activation_invalid.html b/lms/templates/registration/activation_invalid.html
similarity index 86%
rename from lms/templates/activation_invalid.html
rename to lms/templates/registration/activation_invalid.html
index 09832d0df58..09d373a39db 100644
--- a/lms/templates/activation_invalid.html
+++ b/lms/templates/registration/activation_invalid.html
@@ -1,7 +1,7 @@
 <%! from django.core.urlresolvers import reverse %>
-<%inherit file="main.html" />
+<%inherit file="../main.html" />
 
-<%namespace name='static' file='static_content.html'/>
+<%namespace name='static' file='../static_content.html'/>
 
 <section class="container activation">
 
-- 
GitLab