views.py 4.39 KB
Newer Older
muzaffaryousaf committed
1 2 3
""" Views for a student's profile information. """

from django.conf import settings
4
from django.contrib.auth.decorators import login_required
5
from django.core.exceptions import ObjectDoesNotExist
muzaffaryousaf committed
6
from django.core.urlresolvers import reverse
7
from django.http import Http404
muzaffaryousaf committed
8
from django.views.decorators.http import require_http_methods
9
from django_countries import countries
10
from django.contrib.staticfiles.storage import staticfiles_storage
muzaffaryousaf committed
11

12
from badges.utils import badges_enabled
13 14
from edxmako.shortcuts import render_to_response, marketing_link
from microsite_configuration import microsite
15
from openedx.core.djangoapps.user_api.accounts.api import get_account_settings
16 17
from openedx.core.djangoapps.user_api.errors import UserNotFound, UserNotAuthorized
from openedx.core.djangoapps.user_api.preferences.api import get_user_preferences
18
from student.models import User
19

muzaffaryousaf committed
20 21 22 23

@login_required
@require_http_methods(['GET'])
def learner_profile(request, username):
24
    """Render the profile page for the specified username.
muzaffaryousaf committed
25 26 27 28 29 30 31 32 33

    Args:
        request (HttpRequest)
        username (str): username of user whose profile is requested.

    Returns:
        HttpResponse: 200 if the page was sent successfully
        HttpResponse: 302 if not logged in (redirect to login page)
        HttpResponse: 405 if using an unsupported HTTP method
34 35
    Raises:
        Http404: 404 if the specified user is not authorized or does not exist
muzaffaryousaf committed
36 37 38 39

    Example usage:
        GET /account/profile
    """
40 41 42
    try:
        return render_to_response(
            'student_profile/learner_profile.html',
43
            learner_profile_context(request, username, request.user.is_staff)
44
        )
45 46
    except (UserNotAuthorized, UserNotFound, ObjectDoesNotExist):
        raise Http404
muzaffaryousaf committed
47 48


49
def learner_profile_context(request, profile_username, user_is_staff):
50
    """Context for the learner profile page.
muzaffaryousaf committed
51 52

    Args:
53
        logged_in_user (object): Logged In user.
muzaffaryousaf committed
54 55
        profile_username (str): username of user whose profile is requested.
        user_is_staff (bool): Logged In user has staff access.
56
        build_absolute_uri_func ():
muzaffaryousaf committed
57 58 59

    Returns:
        dict
60 61 62

    Raises:
        ObjectDoesNotExist: the specified profile_username does not exist.
muzaffaryousaf committed
63
    """
64
    profile_user = User.objects.get(username=profile_username)
65
    logged_in_user = request.user
muzaffaryousaf committed
66

67
    own_profile = (logged_in_user.username == profile_username)
muzaffaryousaf committed
68

69
    account_settings_data = get_account_settings(request, [profile_username])[0]
70 71 72

    preferences_data = get_user_preferences(profile_user, profile_username)

muzaffaryousaf committed
73 74
    context = {
        'data': {
75
            'profile_user_id': profile_user.id,
muzaffaryousaf committed
76
            'default_public_account_fields': settings.ACCOUNT_VISIBILITY_CONFIGURATION['public_fields'],
77
            'default_visibility': settings.ACCOUNT_VISIBILITY_CONFIGURATION['default_visibility'],
muzaffaryousaf committed
78 79
            'accounts_api_url': reverse("accounts_api", kwargs={'username': profile_username}),
            'preferences_api_url': reverse('preferences_api', kwargs={'username': profile_username}),
80
            'preferences_data': preferences_data,
81
            'account_settings_data': account_settings_data,
82 83 84 85
            'profile_image_upload_url': reverse('profile_image_upload', kwargs={'username': profile_username}),
            'profile_image_remove_url': reverse('profile_image_remove', kwargs={'username': profile_username}),
            'profile_image_max_bytes': settings.PROFILE_IMAGE_MAX_BYTES,
            'profile_image_min_bytes': settings.PROFILE_IMAGE_MIN_BYTES,
muzaffaryousaf committed
86
            'account_settings_page_url': reverse('account_settings'),
87
            'has_preferences_access': (logged_in_user.username == profile_username or user_is_staff),
88
            'own_profile': own_profile,
89
            'country_options': list(countries),
90
            'find_courses_url': marketing_link('COURSES'),
91
            'language_options': settings.ALL_LANGUAGES,
92 93 94
            'badges_logo': staticfiles_storage.url('certificates/images/backpack-logo.png'),
            'badges_icon': staticfiles_storage.url('certificates/images/ico-mozillaopenbadges.png'),
            'backpack_ui_img': staticfiles_storage.url('certificates/images/backpack-ui.png'),
95
            'platform_name': microsite.get_value('platform_name', settings.PLATFORM_NAME),
96 97
        },
        'disable_courseware_js': True,
muzaffaryousaf committed
98
    }
99

100
    if badges_enabled():
101 102
        context['data']['badges_api_url'] = reverse("badges_api:user_assertions", kwargs={'username': profile_username})

muzaffaryousaf committed
103
    return context