Skip to content
Snippets Groups Projects
views.py 48.3 KiB
Newer Older
import feedparser
import logging
import sys
import urllib
import uuid
Piotr Mitros's avatar
Piotr Mitros committed
from django.contrib.auth import logout, authenticate, login
Piotr Mitros's avatar
Piotr Mitros committed
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.models import User
Calen Pennington's avatar
Calen Pennington committed
from django.contrib.auth.decorators import login_required
Brian Wilson's avatar
Brian Wilson committed
from django.core.cache import cache
Piotr Mitros's avatar
Piotr Mitros committed
from django.core.context_processors import csrf
from django.core.mail import send_mail
Brian Wilson's avatar
Brian Wilson committed
from django.core.urlresolvers import reverse
from django.core.validators import validate_email, validate_slug, ValidationError
from django.db import IntegrityError, transaction
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotAllowed, HttpResponseRedirect, Http404
from django.shortcuts import redirect
Brian Wilson's avatar
Brian Wilson committed
from django_future.csrf import ensure_csrf_cookie, csrf_exempt
from django.utils.http import cookie_date
from mitxmako.shortcuts import render_to_response, render_to_string
Piotr Mitros's avatar
Piotr Mitros committed

from student.models import (Registration, UserProfile, TestCenterUser, TestCenterUserForm,
                            TestCenterRegistration, TestCenterRegistrationForm,
                            PendingNameChange, PendingEmailChange,
                            CourseEnrollment, unique_id_for_user,
                            get_testcenter_registration, CourseEnrollmentAllowed)
Victor Shnayder's avatar
Victor Shnayder committed

from certificates.models import CertificateStatuses, certificate_status_for_student

from xmodule.course_module import CourseDescriptor
from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule.modulestore.django import modulestore
from xmodule.modulestore import Location
Piotr Mitros's avatar
Piotr Mitros committed

from collections import namedtuple
from courseware.courses import get_courses, sort_by_announcement
from courseware.access import has_access
from courseware.views import get_module_for_descriptor, jump_to
Calen Pennington's avatar
Calen Pennington committed
from courseware.model_data import ModelDataCache
log = logging.getLogger("mitx.student")
Article = namedtuple('Article', 'title url author image deck publication publish_date')
Calen Pennington's avatar
Calen Pennington committed

Piotr Mitros's avatar
Piotr Mitros committed
def csrf_token(context):
Matthew Mongeau's avatar
Matthew Mongeau committed
    ''' A csrf token that can be included in a form.
Piotr Mitros's avatar
Piotr Mitros committed
    csrf_token = context.get('csrf_token', '')
    if csrf_token == 'NOTPROVIDED':
        return ''
    return (u'<div style="display:none"><input type="hidden"'
            ' name="csrfmiddlewaretoken" value="%s" /></div>' % (csrf_token))
Piotr Mitros's avatar
Piotr Mitros committed

# NOTE: This view is not linked to directly--it is called from
# branding/views.py:index(), which is cached for anonymous users.
# This means that it should always return the same thing for anon
# users. (in particular, no switching based on query params allowed)
def index(request, extra_context={}, user=None):
ichuang's avatar
ichuang committed
    '''
    Render the edX main page.

    extra_context is used to allow immediate display of certain modal windows, eg signup,
    as used by external_auth.
    '''
    # The course selection work is done in courseware.courses.
    domain = settings.MITX_FEATURES.get('FORCE_UNIVERSITY_DOMAIN')  # normally False
Victor Shnayder's avatar
Victor Shnayder committed
    # do explicit check, because domain=None is valid
    if domain == False:
        domain = request.META.get('HTTP_HOST')
    courses = get_courses(None, domain=domain)
    courses = sort_by_announcement(courses)

    # Get the 3 most recent news
    top_news = _get_news(top=3)

    context = {'courses': courses, 'news': top_news}
ichuang's avatar
ichuang committed
    context.update(extra_context)
    return render_to_response('index.html', context)
def course_from_id(course_id):
    """Return the CourseDescriptor corresponding to this course_id"""
    course_loc = CourseDescriptor.id_to_location(course_id)
    return modulestore().get_instance(course_id, course_loc)
import re
day_pattern = re.compile('\s\d+,\s')
multimonth_pattern = re.compile('\s?\-\s?\S+\s')

Calen Pennington's avatar
Calen Pennington committed

def get_date_for_press(publish_date):
    import datetime
    # strip off extra months, and just use the first:
    date = re.sub(multimonth_pattern, ", ", publish_date)
    if re.search(day_pattern, date):
        date = datetime.datetime.strptime(date, "%B %d, %Y")
    else:
        date = datetime.datetime.strptime(date, "%B, %Y")
Calen Pennington's avatar
Calen Pennington committed

def press(request):
    json_articles = cache.get("student_press_json_articles")
    if json_articles is None:
        if hasattr(settings, 'RSS_URL'):
            content = urllib.urlopen(settings.PRESS_URL).read()
            json_articles = json.loads(content)
        else:
            content = open(settings.PROJECT_ROOT / "templates" / "press.json").read()
            json_articles = json.loads(content)
        cache.set("student_press_json_articles", json_articles)
    articles = [Article(**article) for article in json_articles]
    articles.sort(key=lambda item: get_date_for_press(item.publish_date), reverse=True)
    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.
    """
    return survey_link.format(UNIQUE_ID=unique_id_for_user(user))


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', 'restricted'
    'show_download_url': bool
    'download_url': url, only present if show_download_url is True
    'show_disabled_download_button': bool -- true if state is 'generating'
    '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))

Calen Pennington's avatar
Calen Pennington committed

def _cert_info(user, course, cert_status):
    """
    Implements the logic for cert_info -- split out for testing.
    """
    default_status = 'processing'

    default_info = {'status': default_status,
                    'show_disabled_download_button': False,
                    'show_download_url': False,
                    'show_survey_button': False}


    # simplify the status for the template using this lookup table
    template_state = {
        CertificateStatuses.generating: 'generating',
        CertificateStatuses.regenerating: 'generating',
        CertificateStatuses.downloadable: 'ready',
        CertificateStatuses.notpassing: 'notpassing',
        CertificateStatuses.restricted: 'restricted',
        }

    status = template_state.get(cert_status['status'], default_status)

    d = {'status': status,
         'show_download_url': status == 'ready',
Calen Pennington's avatar
Calen Pennington committed
         'show_disabled_download_button': status == 'generating', }
    if (status in ('generating', 'ready', 'notpassing', 'restricted') 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 status == 'ready':
        if 'download_url' not in cert_status:
            log.warning("User %s has a downloadable cert for %s, but no download url",
                        user.username, course.id)
            return default_info
        else:
            d['download_url'] = cert_status['download_url']
    if status in ('generating', 'ready', 'notpassing', 'restricted'):
        if 'grade' not in cert_status:
            # Note: as of 11/20/2012, we know there are students in this state-- cs169.1x,
            # who need to be regraded (we weren't tracking 'notpassing' at first).
            # We can add a log.warning here once we think it shouldn't happen.
            return default_info
        else:
            d['grade'] = cert_status['grade']
Calen Pennington's avatar
Calen Pennington committed

def signin_user(request):
John Jarvis's avatar
John Jarvis committed
    """
    This view will display the non-modal login form
    """
    if request.user.is_authenticated():
        return redirect(reverse('dashboard'))

    context = {
        'course_id': request.GET.get('course_id'),
        'enrollment_action': request.GET.get('enrollment_action')
    }
John Jarvis's avatar
John Jarvis committed
    return render_to_response('login.html', context)
John Jarvis's avatar
John Jarvis committed

def register_user(request, extra_context={}):
John Jarvis's avatar
John Jarvis committed
    """
    This view will display the non-modal registration form
    """
    if request.user.is_authenticated():
        return redirect(reverse('dashboard'))

    context = {
        'course_id': request.GET.get('course_id'),
        'enrollment_action': request.GET.get('enrollment_action')
    }
    context.update(extra_context)

John Jarvis's avatar
John Jarvis committed
    return render_to_response('register.html', context)


@login_required
Matthew Mongeau's avatar
Matthew Mongeau committed
@ensure_csrf_cookie
def dashboard(request):
    user = request.user
    enrollments = CourseEnrollment.objects.filter(user=user)

    # 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.
    courses = []
    for enrollment in enrollments:
        try:
            courses.append(course_from_id(enrollment.course_id))
        except ItemNotFoundError:
            log.error("User {0} enrolled in non-existent course {1}"
                      .format(user.username, enrollment.course_id))
    message = ""
    if not user.is_active:
        message = render_to_string('registration/activate_account_notice.html', {'email': user.email})
    # Global staff can see what courses errored on their dashboard
    staff_access = False
Victor Shnayder's avatar
Victor Shnayder committed
    errored_courses = {}
    if has_access(user, 'global', 'staff'):
        # Show any courses that errored on load
        staff_access = True
        errored_courses = modulestore().get_errored_courses()

    show_courseware_links_for = frozenset(course.id for course in courses
                                          if has_access(request.user, course, 'load'))

Calen Pennington's avatar
Calen Pennington committed
    cert_statuses = {course.id: cert_info(request.user, course) for course in courses}
Calen Pennington's avatar
Calen Pennington committed
    exam_registrations = {course.id: exam_registration_info(request.user, course) for course in courses}
    top_news = _get_news(top=3) if not settings.MITX_FEATURES.get('ENABLE_MKTG_SITE', False) else None
            
    # get info w.r.t ExternalAuthMap
    external_auth_map = None
    if 'external_auth' in settings.INSTALLED_APPS:
        from external_auth.models import ExternalAuthMap
        try:
            external_auth_map = ExternalAuthMap.objects.get(user=user)
        except ExternalAuthMap.DoesNotExist:
            pass
    context = {'courses': courses,
               'message': message,
               'external_auth_map': external_auth_map,
               'staff_access': staff_access,
Calen Pennington's avatar
Calen Pennington committed
               'show_courseware_links_for': show_courseware_links_for,
Victor Shnayder's avatar
Victor Shnayder committed
               'cert_statuses': cert_statuses,
               'exam_registrations': exam_registrations,
Victor Shnayder's avatar
Victor Shnayder committed
               }
    return render_to_response('dashboard.html', context)
    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_response = 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. Response code: {0}; response body: {1}".format(
                    enrollment_response.status_code,
                    enrollment_response.content
                )
            )
            log.exception("Exception automatically enrolling after login: {0}".format(str(e)))
    """
    Modify the enrollment status for the logged-in user.

    The request parameter must be a POST request (other methods return 405)
    that specifies course_id and enrollment_action parameters. If course_id or
    enrollment_action is not specified, if course_id is not valid, if
    enrollment_action is something other than "enroll" or "unenroll", if
    enrollment_action is "enroll" and enrollment is closed for the course, or
    if enrollment_action is "unenroll" and the user is not enrolled in the
    course, a 400 error will be returned. If the user is not logged in, 403
    will be returned; it is important that only this case return 403 so the
    front end can redirect the user to a registration or login page when this
    happens. This function should only be called from an AJAX request or
    as a post-login/registration helper, so the error messages in the responses
    should never actually be user-visible.
    """
        return HttpResponseNotAllowed(["POST"])
    if not user.is_authenticated():
        return HttpResponseForbidden()
    action = request.POST.get("enrollment_action")
    course_id = request.POST.get("course_id")
    if course_id is None:
        return HttpResponseBadRequest("Course id not specified")
    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.warning("User {0} tried to enroll in non-existent course {1}"
                        .format(user.username, course_id))
            return HttpResponseBadRequest("Course id is invalid")
        if not has_access(user, course, 'enroll'):
            return HttpResponseBadRequest("Enrollment is closed")
Calen Pennington's avatar
Calen Pennington committed
        org, course_num, run = course_id.split("/")
        statsd.increment("common.student.enrollment",
                         tags=["org:{0}".format(org),
                               "course:{0}".format(course_num),
                               "run:{0}".format(run)])
        try:
            enrollment, created = CourseEnrollment.objects.get_or_create(user=user, course_id=course.id)
        except IntegrityError:
            # If we've already created this enrollment in a separate transaction,
            # then just continue
            pass
            enrollment = CourseEnrollment.objects.get(user=user, course_id=course_id)
Calen Pennington's avatar
Calen Pennington committed
            org, course_num, run = course_id.split("/")
            statsd.increment("common.student.unenrollment",
                             tags=["org:{0}".format(org),
                                   "course:{0}".format(course_num),
                                   "run:{0}".format(run)])
        except CourseEnrollment.DoesNotExist:
            return HttpResponseBadRequest("You are not enrolled in this course")
        return HttpResponseBadRequest("Enrollment action is invalid")
@ensure_csrf_cookie
def accounts_login(request, error=""):

    return render_to_response('login.html', {'error': error})
# Need different levels of logging
Piotr Mitros's avatar
Piotr Mitros committed
def login_user(request, error=""):
    ''' AJAX request to log in the user. '''
Piotr Mitros's avatar
Piotr Mitros committed
    if 'email' not in request.POST or 'password' not in request.POST:
        return HttpResponse(json.dumps({'success': False,
                                        'value': 'There was an error receiving your login information. Please email us.'}))  # TODO: User error message
Piotr Mitros's avatar
Piotr Mitros committed
    email = request.POST['email']
    password = request.POST['password']
Piotr Mitros's avatar
Piotr Mitros committed
    try:
        user = User.objects.get(email=email)
Piotr Mitros's avatar
Piotr Mitros committed
    except User.DoesNotExist:
        log.warning(u"Login failed - Unknown user email: {0}".format(email))
        return HttpResponse(json.dumps({'success': False,
                                        'value': 'Email or password is incorrect.'}))  # TODO: User error message
Piotr Mitros's avatar
Piotr Mitros committed

    username = user.username
    user = authenticate(username=username, password=password)
Piotr Mitros's avatar
Piotr Mitros committed
    if user is None:
        log.warning(u"Login failed - password for {0} is invalid".format(email))
        return HttpResponse(json.dumps({'success': False,
                                        'value': 'Email or password is incorrect.'}))
Piotr Mitros's avatar
Piotr Mitros committed
    if user is not None and user.is_active:
        try:
            login(request, user)
            if request.POST.get('remember') == 'true':
                log.debug("Setting user session to never expire")
            else:
                request.session.set_expiry(0)
        except Exception as e:
            log.critical("Login failed - Could not create session. Is memcached running?")
            log.exception(e)
        log.info(u"Login success - {0} ({1})".format(username, email))
        statsd.increment("common.student.successful_login")
        response = HttpResponse(json.dumps({'success': True}))

        # set the login cookie for the edx marketing site
        # we want this cookie to be accessed via javascript
        # so httponly is set to None

        if request.session.get_expire_at_browser_close():
            max_age = None
            expires = None
        else:
            max_age = request.session.get_expiry_age()
            expires_time = time.time() + max_age
            expires = cookie_date(expires_time)
        response.set_cookie(settings.EDXMKTG_COOKIE_NAME,
                            'true', max_age=max_age,
                            expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
                            path='/',
                            secure=None,
                            httponly=None)
    log.warning(u"Login failed - Account not active for user {0}, resending activation".format(username))
    reactivation_email_for_user(user)
    not_activated_msg = "This account has not been activated. We have " + \
                        "sent another activation message. Please check your " + \
                        "e-mail for the activation instructions."
    return HttpResponse(json.dumps({'success': False,
Piotr Mitros's avatar
Piotr Mitros committed

Piotr Mitros's avatar
Piotr Mitros committed
def logout_user(request):
    '''
    HTTP request to log out the user. Redirects to marketing page.
    Deletes both the CSRF and sessionid cookies so the marketing
    site can determine the logged in state of the user
    '''

Piotr Mitros's avatar
Piotr Mitros committed
    logout(request)
    response = redirect('/')
    response.delete_cookie(settings.EDXMKTG_COOKIE_NAME,
                           path='/',
                           domain=settings.SESSION_COOKIE_DOMAIN)
Piotr Mitros's avatar
Piotr Mitros committed

def change_setting(request):
Matthew Mongeau's avatar
Matthew Mongeau committed
    ''' JSON call to change a profile setting: Right now, location
Piotr Mitros's avatar
Piotr Mitros committed
    '''
    # TODO (vshnayder): location is no longer used
    up = UserProfile.objects.get(user=request.user)  # request.user.profile_cache
Piotr Mitros's avatar
Piotr Mitros committed
    if 'location' in request.POST:
        up.location = request.POST['location']
    return HttpResponse(json.dumps({'success': True,
                                    'location': up.location, }))

Calen Pennington's avatar
Calen Pennington committed

def _do_create_account(post_vars):
    """
    Given cleaned post variables, create the User and UserProfile objects, as well as the
    registration for this user.

    Returns a tuple (User, UserProfile, Registration).

    Note: this function is also used for creating test users.
    """
    user = User(username=post_vars['username'],
                email=post_vars['email'],
                is_active=False)
    user.set_password(post_vars['password'])
    registration = Registration()
    # TODO: Rearrange so that if part of the process fails, the whole process fails.
    # Right now, we can have e.g. no registration e-mail sent out and a zombie account
    try:
    except IntegrityError:
        # Figure out the cause of the integrity error
        if len(User.objects.filter(username=post_vars['username'])) > 0:
            js['value'] = "An account with the Public Username  '" + post_vars['username'] + "' already exists."
            js['field'] = 'username'
            return HttpResponse(json.dumps(js))

        if len(User.objects.filter(email=post_vars['email'])) > 0:
            js['value'] = "An account with the Email '" + post_vars['email'] + "' already exists."
            js['field'] = 'email'
            return HttpResponse(json.dumps(js))

        raise

    registration.register(user)
    profile = UserProfile(user=user)
    profile.name = post_vars['name']
    profile.level_of_education = post_vars.get('level_of_education')
    profile.gender = post_vars.get('gender')
    profile.mailing_address = post_vars.get('mailing_address')
    profile.goals = post_vars.get('goals')
        profile.year_of_birth = int(post_vars['year_of_birth'])
    except (ValueError, KeyError):
        # If they give us garbage, just ignore it instead
        # of asking them to put an integer.
        profile.year_of_birth = None
        profile.save()
        log.exception("UserProfile creation failed for user {0}.".format(user.id))
    return (user, profile, registration)
def create_account(request, post_override=None):
ichuang's avatar
ichuang committed
    '''
    JSON call to create new edX account.
    Used by form in signup_modal.html, which is included into navigation.html
    '''
    js = {'success': False}
    post_vars = post_override if post_override else request.POST
ichuang's avatar
ichuang committed
    # if doing signup for an external authorization, then get email, password, name from the eamap
    # don't use the ones from the form, since the user could have hacked those
    # unless originally we didn't get a valid email or name from the external auth
    DoExternalAuth = 'ExternalAuthMap' in request.session
    if DoExternalAuth:
ichuang's avatar
ichuang committed
        eamap = request.session['ExternalAuthMap']
        try:
            validate_email(eamap.external_email)
            email = eamap.external_email
        except ValidationError:
            email = post_vars.get('email', '')
        if eamap.external_name.strip() == '':
            name = post_vars.get('name', '')
        else:
            name = eamap.external_name 
ichuang's avatar
ichuang committed
        password = eamap.internal_password
        post_vars = dict(post_vars.items())
        post_vars.update(dict(email=email, name=name, password=password))
        log.info('In create_account with external_auth: post_vars = %s' % post_vars)
ichuang's avatar
ichuang committed

Piotr Mitros's avatar
Piotr Mitros committed
    # Confirm we have a properly formed request
Matthew Mongeau's avatar
Matthew Mongeau committed
    for a in ['username', 'email', 'password', 'name']:
            js['value'] = "Error (401 {field}). E-mail us.".format(field=a)
Piotr Mitros's avatar
Piotr Mitros committed
            return HttpResponse(json.dumps(js))

    if post_vars.get('honor_code', 'false') != u'true':
        js['value'] = "To enroll, you must follow the honor code.".format(field=a)
        js['field'] = 'honor_code'
Piotr Mitros's avatar
Piotr Mitros committed
        return HttpResponse(json.dumps(js))

    # Can't have terms of service for Stanford users, according to Stanford's Office of General Counsel
    if settings.MITX_FEATURES.get("AUTH_USE_SHIB") and DoExternalAuth and ("stanford" in eamap.external_domain):
        pass
    else:
        if post_vars.get('terms_of_service', 'false') != u'true':
            js['value'] = "You must accept the terms of service.".format(field=a)
            js['field'] = 'terms_of_service'
            return HttpResponse(json.dumps(js))
Piotr Mitros's avatar
Piotr Mitros committed

Matthew Mongeau's avatar
Matthew Mongeau committed
    # Confirm appropriate fields are there.
    # TODO: Check e-mail format is correct.
    # TODO: Confirm e-mail is not from a generic domain (mailinator, etc.)? Not sure if
Piotr Mitros's avatar
Piotr Mitros committed
    # this is a good idea
    # TODO: Check password is sane

    required_post_vars = ['username', 'email', 'name', 'password', 'terms_of_service', 'honor_code']
    if settings.MITX_FEATURES.get("AUTH_USE_SHIB") and DoExternalAuth and ("stanford" in eamap.external_domain):
        # Can't have terms of service for Stanford users, according to Stanford's Office of General Counsel
        required_post_vars =  ['username', 'email', 'name', 'password', 'honor_code']

    for a in required_post_vars:
        if len(post_vars[a]) < 2:
            error_str = {'username': 'Username must be minimum of two characters long.',
                         'email': 'A properly formatted e-mail is required.',
                         'name': 'Your legal name must be a minimum of two characters long.',
                         'password': 'A valid password is required.',
                         'terms_of_service': 'Accepting Terms of Service is required.',
                         'honor_code': 'Agreeing to the Honor Code is required.'}
            js['value'] = error_str[a]
Piotr Mitros's avatar
Piotr Mitros committed
            return HttpResponse(json.dumps(js))

    try:
    except ValidationError:
        js['value'] = "Valid e-mail is required.".format(field=a)
        js['field'] = 'email'
Piotr Mitros's avatar
Piotr Mitros committed
        return HttpResponse(json.dumps(js))

    try:
    except ValidationError:
        js['value'] = "Username should only consist of A-Z and 0-9, with no spaces.".format(field=a)
        js['field'] = 'username'
Piotr Mitros's avatar
Piotr Mitros committed
        return HttpResponse(json.dumps(js))
    # Ok, looks like everything is legit.  Create the account.
    ret = _do_create_account(post_vars)
    if isinstance(ret, HttpResponse):  # if there was an error then return that
        return ret
    (user, profile, registration) = ret
    d = {'name': post_vars['name'],
         'key': registration.activation_key,
Piotr Mitros's avatar
Piotr Mitros committed

ichuang's avatar
ichuang committed
    # composes activation email
    subject = render_to_string('emails/activation_email_subject.txt', d)
ichuang's avatar
ichuang committed
    # Email subject *must not* contain newlines
Piotr Mitros's avatar
Piotr Mitros committed
    subject = ''.join(subject.splitlines())
    message = render_to_string('emails/activation_email.txt', d)
Piotr Mitros's avatar
Piotr Mitros committed

    try:
        if settings.MITX_FEATURES.get('REROUTE_ACTIVATION_EMAIL'):
            dest_addr = settings.MITX_FEATURES['REROUTE_ACTIVATION_EMAIL']
            message = ("Activation for %s (%s): %s\n" % (user, user.email, profile.name) +
                       '-' * 80 + '\n\n' + message)
            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [dest_addr], fail_silently=False)
        elif not settings.GENERATE_RANDOM_USER_CREDENTIALS:
            res = user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
Piotr Mitros's avatar
Piotr Mitros committed
    except:
        log.warning('Unable to send activation email to user', exc_info=True)
        js['value'] = 'Could not send activation e-mail.'
Piotr Mitros's avatar
Piotr Mitros committed
        return HttpResponse(json.dumps(js))
    # Immediately after a user creates an account, we log them in. They are only
    # logged in until they close the browser. They can't log in again until they click
    # the activation link from the email.
    login_user = authenticate(username=post_vars['username'], password=post_vars['password'])
    request.session.set_expiry(0)

    if DoExternalAuth:
ichuang's avatar
ichuang committed
        eamap.user = login_user
        eamap.dtsignup = datetime.datetime.now(UTC)
ichuang's avatar
ichuang committed
        eamap.save()
        log.info("User registered with external_auth %s" % post_vars['username'])
        log.info('Updated ExternalAuthMap for %s to be %s' % (post_vars['username'], eamap))
ichuang's avatar
ichuang committed

        if settings.MITX_FEATURES.get('BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH'):
            log.info('bypassing activation email')
ichuang's avatar
ichuang committed
            login_user.is_active = True
            login_user.save()
    try_change_enrollment(request)

    statsd.increment("common.student.account_created")
    js = {'success': True}
    HttpResponse(json.dumps(js), mimetype="application/json")

    response = HttpResponse(json.dumps({'success': True}))

    # set the login cookie for the edx marketing site
    # we want this cookie to be accessed via javascript
    # so httponly is set to None

    if request.session.get_expire_at_browser_close():
        max_age = None
        expires = None
    else:
        max_age = request.session.get_expiry_age()
        expires_time = time.time() + max_age
        expires = cookie_date(expires_time)

    response.set_cookie(settings.EDXMKTG_COOKIE_NAME,
                        'true', max_age=max_age,
                        expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
                        path='/',
                        secure=None,
                        httponly=None)
    return response

def exam_registration_info(user, course):
    """ Returns a Registration object if the user is currently registered for a current
    exam of the course.  Returns None if the user is not registered, or if there is no
    current exam for the course.
    """
    exam_info = course.current_test_center_exam
    if exam_info is None:
        return None
    exam_code = exam_info.exam_series_code
    registrations = get_testcenter_registration(user, course.id, exam_code)
    if registrations:
        registration = registrations[0]
    else:
        registration = None
    return registration
Calen Pennington's avatar
Calen Pennington committed

def begin_exam_registration(request, course_id):
    """ Handles request to register the user for the current
    test center exam of the specified course.  Called by form
    in dashboard.html.
    """
Brian Wilson's avatar
Brian Wilson committed
    user = request.user

    try:
        course = course_from_id(course_id)
Brian Wilson's avatar
Brian Wilson committed
    except ItemNotFoundError:
        log.error("User {0} enrolled in non-existent course {1}".format(user.username, course_id))
        raise Http404
Brian Wilson's avatar
Brian Wilson committed

    # get the exam to be registered for:
    # (For now, we just assume there is one at most.)
    # if there is no exam now (because someone bookmarked this stupid page),
    # then return a 404:
    exam_info = course.current_test_center_exam
    # determine if the user is registered for this course:
    registration = exam_registration_info(user, course)
    # we want to populate the registration page with the relevant information,
    # if it already exists.  Create an empty object otherwise.
    try:
        testcenteruser = TestCenterUser.objects.get(user=user)
    except TestCenterUser.DoesNotExist:
        testcenteruser = TestCenterUser()
        testcenteruser.user = user
    context = {'course': course,
               'user': user,
               'testcenteruser': testcenteruser,
Brian Wilson's avatar
Brian Wilson committed
               'registration': registration,
               'exam_info': exam_info,
               }

    return render_to_response('test_center_register.html', context)

Calen Pennington's avatar
Calen Pennington committed

Brian Wilson's avatar
Brian Wilson committed
@ensure_csrf_cookie
def create_exam_registration(request, post_override=None):
Brian Wilson's avatar
Brian Wilson committed
    '''
    JSON call to create a test center exam registration.
    Called by form in test_center_register.html
Brian Wilson's avatar
Brian Wilson committed
    '''
    post_vars = post_override if post_override else request.POST

    # first determine if we need to create a new TestCenterUser, or if we are making any update
    # to an existing TestCenterUser.
    user = User.objects.get(username=username)
    course = course_from_id(course_id)  # assume it will be found....

    # make sure that any demographic data values received from the page have been stripped.
    # Whitespace is not an acceptable response for any of these values
    demographic_data = {}
    for fieldname in TestCenterUser.user_provided_fields():
        if fieldname in post_vars:
            demographic_data[fieldname] = (post_vars[fieldname]).strip()
    try:
        testcenter_user = TestCenterUser.objects.get(user=user)
        needs_updating = testcenter_user.needs_update(demographic_data)
        log.info("User {0} enrolled in course {1} {2}updating demographic info for exam registration".format(user.username, course_id, "" if needs_updating else "not "))
    except TestCenterUser.DoesNotExist:
        # do additional initialization here:
        testcenter_user = TestCenterUser.create(user)
        needs_updating = True
        log.info("User {0} enrolled in course {1} creating demographic info for exam registration".format(user.username, course_id))
Brian Wilson's avatar
Brian Wilson committed

    # perform validation:
    if needs_updating:
        # first perform validation on the user information
        # using a Django Form.
        form = TestCenterUserForm(instance=testcenter_user, data=demographic_data)
        if form.is_valid():
        else:
            response_data = {'success': False}
            # return a list of errors...
            response_data['field_errors'] = form.errors
            response_data['non_field_errors'] = form.non_field_errors()
            return HttpResponse(json.dumps(response_data), mimetype="application/json")
    exam = course.current_test_center_exam
    exam_code = exam.exam_series_code
    registrations = get_testcenter_registration(user, course_id, exam_code)
    if registrations:
        # NOTE: we do not bother to check here to see if the registration has changed,
        # because at the moment there is no way for a user to change anything about their
        # registration.  They only provide an optional accommodation request once, and
        # cannot make changes to it thereafter.
        # It is possible that the exam_info content has been changed, such as the
        # scheduled exam dates, but those kinds of changes should not be handled through
        # this registration screen.

Calen Pennington's avatar
Calen Pennington committed
        accommodation_request = post_vars.get('accommodation_request', '')
        registration = TestCenterRegistration.create(testcenter_user, exam, accommodation_request)
        log.info("User {0} enrolled in course {1} creating new exam registration".format(user.username, course_id))
        # do validation of registration.  (Mainly whether an accommodation request is too long.)
        form = TestCenterRegistrationForm(instance=registration, data=post_vars)
        if form.is_valid():
            form.update_and_save()
        else:
            response_data = {'success': False}
            # return a list of errors...
            response_data['field_errors'] = form.errors
            response_data['non_field_errors'] = form.non_field_errors()
            return HttpResponse(json.dumps(response_data), mimetype="application/json")
    # only do the following if there is accommodation text to send,
    # and a destination to which to send it.
    # TODO: still need to create the accommodation email templates
#    if 'accommodation_request' in post_vars and 'TESTCENTER_ACCOMMODATION_REQUEST_EMAIL' in settings:
#        d = {'accommodation_request': post_vars['accommodation_request'] }
#        # composes accommodation email
#        subject = render_to_string('emails/accommodation_email_subject.txt', d)
#        # Email subject *must not* contain newlines
#        subject = ''.join(subject.splitlines())
#        message = render_to_string('emails/accommodation_email.txt', d)
#
#        try:
#            dest_addr = settings['TESTCENTER_ACCOMMODATION_REQUEST_EMAIL']
#            from_addr = user.email
#            send_mail(subject, message, from_addr, [dest_addr], fail_silently=False)
#        except:
#            log.exception(sys.exc_info())
#            response_data = {'success': False}
#            response_data['non_field_errors'] =  [ 'Could not send accommodation e-mail.', ]
#            return HttpResponse(json.dumps(response_data), mimetype="application/json")
    js = {'success': True}
    return HttpResponse(json.dumps(js), mimetype="application/json")
def get_random_post_override():
    """
    Return a dictionary suitable for passing to post_vars of _do_create_account or post_override
    of create_account, with random user info.
    """
    def id_generator(size=6, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
        return ''.join(random.choice(chars) for x in range(size))
    return {'username': "random_" + id_generator(),
            'email': id_generator(size=10, chars=string.ascii_lowercase) + "_dummy_test@mitx.mit.edu",
            'password': id_generator(),
            'name': (id_generator(size=5, chars=string.ascii_lowercase) + " " +
                     id_generator(size=7, chars=string.ascii_lowercase)),
                     'honor_code': u'true',
                     'terms_of_service': u'true', }

def create_random_account(create_account_function):
    def inner_create_random_account(request):
        return create_account_function(request, post_override=get_random_post_override())
# TODO (vshnayder): do we need GENERATE_RANDOM_USER_CREDENTIALS for anything?
if settings.GENERATE_RANDOM_USER_CREDENTIALS:
    create_account = create_random_account(create_account)

Piotr Mitros's avatar
Piotr Mitros committed
def activate_account(request, key):
    ''' When link in activation e-mail is clicked
    '''
    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()
        #Enroll student in any pending courses he/she may have if auto_enroll flag is set
        student = User.objects.filter(id=r[0].user_id)
        if student:
            ceas = CourseEnrollmentAllowed.objects.filter(email=student[0].email)
            for cea in ceas:
                if cea.auto_enroll:
                    course_id = cea.course_id
                    enrollment, created = CourseEnrollment.objects.get_or_create(user_id=student[0].id, course_id=course_id)
        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("registration/activation_invalid.html", {'csrf': csrf(request)['csrf_token']})
Piotr Mitros's avatar
Piotr Mitros committed
    return HttpResponse("Unknown error. Please e-mail us to let us know how it happened.")
Piotr Mitros's avatar
Piotr Mitros committed

Piotr Mitros's avatar
Piotr Mitros committed
def password_reset(request):
    ''' Attempts to send a password reset e-mail. '''
    if request.method != "POST":
        raise Http404
    # By default, Django doesn't allow Users with is_active = False to reset their passwords,
Victor Shnayder's avatar
Victor Shnayder committed
    # but this bites people who signed up a long time ago, never activated, and forgot their
    # password. So for their sake, we'll auto-activate a user for whom password_reset is called.
    try:
        user = User.objects.get(email=request.POST['email'])
        user.is_active = True
        user.save()
    except:
        log.exception("Tried to auto-activate user to enable password reset, but failed.")
Piotr Mitros's avatar
Piotr Mitros committed
    form = PasswordResetForm(request.POST)
    if form.is_valid():
Calen Pennington's avatar
Calen Pennington committed
        form.save(use_https=request.is_secure(),
                  from_email=settings.DEFAULT_FROM_EMAIL,
                  request=request,
                  domain_override=request.get_host())
        return HttpResponse(json.dumps({'success': True,
                                        'value': render_to_string('registration/password_reset_done.html', {})}))
    else:
        return HttpResponse(json.dumps({'success': False,
                                        'error': 'Invalid e-mail'}))
Calen Pennington's avatar
Calen Pennington committed

@ensure_csrf_cookie
def reactivation_email(request):
    ''' Send an e-mail to reactivate a deactivated account, or to
    resend an activation e-mail. Untested. '''
    email = request.POST['email']
Matthew Mongeau's avatar
Matthew Mongeau committed
    try:
        user = User.objects.get(email='email')
    except User.DoesNotExist:
        return HttpResponse(json.dumps({'success': False,
                                        'error': 'No inactive user with this e-mail exists'}))