Repository: ocadotechnology/codeforlife-portal

Issue:
change_email and verify_email_new functions should be the same
In email_new in the portal, these two functions are the same except for the redirection - please factorise :)


Discussion:

Resolving patch:
diff --git a/portal/emailMessages.py b/portal/emailMessages.py
--- a/portal/emailMessages.py
+++ b/portal/emailMessages.py
@@ -58,7 +58,7 @@ def emailChangeVerificationEmail(request, token):
     return {
         'subject': emailSubjectPrefix() + " : Email address verification needed",
         'message': ("You are changing your email, please go to " +
-                    request.build_absolute_uri(reverse('change_email', kwargs={'token': token})) +
+                    request.build_absolute_uri(reverse('verify_email', kwargs={'token': token})) +
                     " to verify your new email address. If you are not part of Code for Life " +
                     "then please ignore this email. " + emailBodySignOff(request)),
     }
diff --git a/portal/urls.py b/portal/urls.py
--- a/portal/urls.py
+++ b/portal/urls.py
@@ -50,7 +50,7 @@
 
 from game.views.level import play_default_level
 
-from portal.views.email import verify_email, change_email
+from portal.views.email import verify_email
 from portal.views.home import login_view, logout_view, register_view, contact
 from portal.views.play import student_details, student_edit_account, student_join_organisation
 from portal.views.organisation import organisation_fuzzy_lookup, organisation_manage, organisation_leave
@@ -111,7 +111,6 @@
     url(r'^login_form', login_view, name='login_view'),
     url(r'^logout/$', logout_view, name='logout_view'),
     url(r'^verify_email/(?P<token>[0-9a-f]+)/$', verify_email, name='verify_email'),
-    url(r'^change_email/(?P<token>[0-9a-f]+)/$', change_email, name='change_email'),
     url(r'^user/password/reset/student/$', student_password_reset, name="student_password_reset"),
     url(r'^user/password/reset/teacher/$', teacher_password_reset, name="teacher_password_reset"),
     url(r'^user/password/reset/done/$', password_reset_done, name='reset_password_email_sent'),
diff --git a/portal/views/email.py b/portal/views/email.py
--- a/portal/views/email.py
+++ b/portal/views/email.py
@@ -74,48 +74,12 @@ def verify_email(request, token):
     # copy newly verified user to secure salesforce db
     add_to_salesforce(user)
 
-    if hasattr(user.userprofile, 'student'):
-        return HttpResponseRedirect(reverse_lazy('login_view'))
-    if hasattr(user.userprofile, 'teacher'):
-        return HttpResponseRedirect(reverse_lazy('onboarding-organisation'))
-
-    # default to homepage if something goes wrong
-    return HttpResponseRedirect(reverse_lazy('home'))
-
-
-def change_email(request, token):
-    verifications = EmailVerification.objects.filter(token=token)
-
-    if has_verification_failed(verifications):
-        return render(request, 'portal/email_verification_failed.html')
-
-    verification = verifications[0]
-
-    verification.verified = True
-    verification.save()
-
-    user = verification.user
-
-    if verification.email:  # verifying change of email address
-        user.email = verification.email
-        user.save()
-
-        user.email_verifications.exclude(email=user.email).delete()
-
-    messages.success(request, 'Your email address was successfully verified, please log in.')
-
-    # copy newly verified user to secure salesforce db
-    add_to_salesforce(user)
-
-    if hasattr(user.userprofile, 'teacher'):
-        return HttpResponseRedirect(reverse_lazy('dashboard'))
-
-    # default to homepage if something goes wrong
-    return HttpResponseRedirect(reverse_lazy('home'))
+    return HttpResponseRedirect(reverse_lazy('login_view'))
 
 
 def has_verification_failed(verifications):
-    return len(verifications) != 1 or verifications[0].verified or (verifications[0].expiry - timezone.now()) < timedelta()
+    return len(verifications) != 1 or verifications[0].verified or \
+           (verifications[0].expiry - timezone.now()) < timedelta()
 
 
 def send_new_users_report(request):
@@ -127,7 +91,8 @@ def send_new_users_report(request):
     student_count = Student.objects.count()
     schools_countries = School.objects.values('country').annotate(nb_countries=Count('id')).order_by('-nb_countries')
     nb_countries = schools_countries.count()
-    countries_count = "\n".join('{}: {}'.format(dict(countries)[k["country"]], k["nb_countries"]) for k in schools_countries[:3])
+    countries_count = "\n".join('{}: {}'.format(dict(countries)[k["country"]], k["nb_countries"])
+                                for k in schools_countries[:3])
     send_email(NOTIFICATION_EMAIL, CONTACT_FORM_EMAILS, "new users",
                'There are {new_users} new users this week!\n'
                'The total number of registered users is now: {users}\n'


Test patch: