Commit 4b9eafac by Victor Shnayder

moving cert link and survey logic into views, adding tests. In-progress.

parent a7536773
...@@ -6,11 +6,14 @@ Replace this with more appropriate tests for your application. ...@@ -6,11 +6,14 @@ Replace this with more appropriate tests for your application.
""" """
import logging import logging
from datetime import datetime from datetime import datetime
from hashlib import sha1
from django.test import TestCase from django.test import TestCase
from mock import patch, Mock
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from .models import User, UserProfile, CourseEnrollment, replicate_user, USER_FIELDS_TO_COPY from .models import User, UserProfile, CourseEnrollment, replicate_user, USER_FIELDS_TO_COPY
import .views
COURSE_1 = 'edX/toy/2012_Fall' COURSE_1 = 'edX/toy/2012_Fall'
COURSE_2 = 'edx/full/6.002_Spring_2012' COURSE_2 = 'edx/full/6.002_Spring_2012'
...@@ -196,8 +199,22 @@ class ReplicationTest(TestCase): ...@@ -196,8 +199,22 @@ class ReplicationTest(TestCase):
id=portal_user_profile.id) id=portal_user_profile.id)
class CourseEndingTest(TestCase):
"""Test things related to course endings: certificates, surveys, etc"""
def test_process_survey_link(self):
username = "fred"
id = sha1(username)
link1 = "http://www.mysurvey.com"
self.assertEqual(process_survey_link(link1), link1)
link2 = "http://www.mysurvey.com?unique={UNIQUE_ID}"
link2_expected = "http://www.mysurvey.com?unique={UNIQUE_ID}".format(UNIQUE_ID=id)
self.assertEqual(views.process_survey_link(link2), link2_expected)
def test_cert_info(self):
user = Mock(username="fred")
survey_url = "http://a_survey.com"
course = Mock(end_of_course_survey_url=survey_url)
cert_status = None
self.assertEqual(views._cert_info(user, course, None), {'status': 'processing'})
...@@ -39,6 +39,8 @@ from xmodule.modulestore.exceptions import ItemNotFoundError ...@@ -39,6 +39,8 @@ from xmodule.modulestore.exceptions import ItemNotFoundError
from datetime import date from datetime import date
from collections import namedtuple from collections import namedtuple
from hashlib import sha1
from courseware.courses import get_courses_by_university from courseware.courses import get_courses_by_university
from courseware.access import has_access from courseware.access import has_access
...@@ -127,6 +129,73 @@ def press(request): ...@@ -127,6 +129,73 @@ def press(request):
return render_to_response('static_templates/press.html', {'articles': articles}) return render_to_response('static_templates/press.html', {'articles': articles})
def process_survey_link(survey_link, user):
"""
If {UNIQUE_ID} appears in the link, replace it with a unique id for the user.
Currently, this is sha1(user.username). Otherwise, return survey_link.
"""
to_replace = '{UNIQUE_ID}'
if to_replace in survey_link:
unique_id = sha1(user.username)
return survey_link.replace(to_replace, unique_id)
return survey_link
def cert_info(user, course):
"""
Get the certificate info needed to render the dashboard section for the given
student and course. Returns a dictionary with keys:
'status': one of 'generating', 'ready', 'notpassing', 'processing'
'show_download_url': bool
'download_url': url, only present if show_download_url is True
'show_survey_button': bool
'survey_url': url, only if show_survey_button is True
'grade': if status is not 'processing'
"""
if not course.has_ended():
return {}
return _cert_info(user, course, certificate_status_for_student(user, course.id))
def _cert_info(user, course, cert_status):
"""
Implements the logic for cert_info -- split out for testing.
"""
default_status = 'processing'
if cert_status is None:
return {'status': default_status}
# simplify the status for the template using this lookup table
template_state = {
CertificateStatuses.generating: 'generating',
CertificateStatuses.regenerating: 'generating',
CertificateStatuses.downloadable: 'ready',
CertificateStatuses.notpassing: 'notpassing',
}
status = template_state.get(cert_status['status'], default_status)
d = {'status': status,
'show_download_url': status in ('generating', 'ready'),}
if (status in ('generating', 'ready', 'not-available') and
course.end_of_course_survey_url is not None):
d.update({
'show_survey_button': True,
'survey_url': process_survey_link(course.end_of_course_survey_url, user)})
else:
d['show_survey_button'] = False
if template_state == 'ready':
d['download_url'] = cert_status['download_url']
if template_state in 'generating', 'ready', 'notpassing':
d['grade'] = cert_status['grade']
return d
@login_required @login_required
@ensure_csrf_cookie @ensure_csrf_cookie
def dashboard(request): def dashboard(request):
...@@ -163,7 +232,7 @@ def dashboard(request): ...@@ -163,7 +232,7 @@ def dashboard(request):
# TODO: workaround to not have to zip courses and certificates in the template # TODO: workaround to not have to zip courses and certificates in the template
# since before there is a migration to certificates # since before there is a migration to certificates
if settings.MITX_FEATURES.get('CERTIFICATES_ENABLED'): if settings.MITX_FEATURES.get('CERTIFICATES_ENABLED'):
cert_statuses = { course.id: certificate_status_for_student(request.user, course.id) for course in courses} cert_statuses = { course.id: cert_info(request.user, course) for course in courses}
else: else:
cert_statuses = {} cert_statuses = {}
......
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