From 2d08d669caebfe0c784248db1e416c4600b9502e Mon Sep 17 00:00:00 2001 From: muzaffaryousaf Date: Thu, 7 May 2015 15:14:19 +0500 Subject: [PATCH] Learner Profile page tweaks. TNL-2047 --- lms/djangoapps/student_profile/views.py | 27 ++++++-- .../views/learner_profile_factory.js | 67 +++++++++++-------- lms/static/js/views/fields.js | 6 +- lms/static/sass/views/_learner-profile.scss | 1 - .../student_profile/learner_profile.html | 3 +- 5 files changed, 67 insertions(+), 37 deletions(-) diff --git a/lms/djangoapps/student_profile/views.py b/lms/djangoapps/student_profile/views.py index 1a38b06d25..8145f2564f 100644 --- a/lms/djangoapps/student_profile/views.py +++ b/lms/djangoapps/student_profile/views.py @@ -10,6 +10,10 @@ from django.http import HttpResponse from django.views.decorators.http import require_http_methods from edxmako.shortcuts import render_to_response +from openedx.core.djangoapps.user_api.preferences.api import get_user_preferences +from openedx.core.djangoapps.user_api.accounts.api import get_account_settings +from openedx.core.djangoapps.user_api.errors import UserNotFound, UserNotAuthorized +from openedx.core.djangoapps.user_api.accounts.serializers import PROFILE_IMAGE_KEY_PREFIX from student.models import User from microsite_configuration import microsite @@ -38,13 +42,17 @@ def learner_profile(request, username): try: return render_to_response( 'student_profile/learner_profile.html', - learner_profile_context(request.user.username, username, request.user.is_staff) + learner_profile_context(request, request.user.username, username, request.user.is_staff) ) + except UserNotAuthorized: + return HttpResponse(status=403) + except UserNotFound: + return HttpResponse(status=404) except ObjectDoesNotExist: return HttpResponse(status=404) -def learner_profile_context(logged_in_username, profile_username, user_is_staff): +def learner_profile_context(request, logged_in_username, profile_username, user_is_staff): """Context for the learner profile page. Args: @@ -67,6 +75,16 @@ def learner_profile_context(logged_in_username, profile_username, user_is_staff) ) ] + own_profile = (logged_in_username == profile_username) + + accounts_data = get_account_settings(request.user, profile_username) + # Account for possibly relative URLs. + for key, value in accounts_data['profile_image'].items(): + if key.startswith(PROFILE_IMAGE_KEY_PREFIX): + accounts_data['profile_image'][key] = request.build_absolute_uri(value) + + preferences_data = get_user_preferences(profile_user, profile_username) + context = { 'data': { 'profile_user_id': profile_user.id, @@ -74,17 +92,18 @@ def learner_profile_context(logged_in_username, profile_username, user_is_staff) 'default_visibility': settings.ACCOUNT_VISIBILITY_CONFIGURATION['default_visibility'], 'accounts_api_url': reverse("accounts_api", kwargs={'username': profile_username}), 'preferences_api_url': reverse('preferences_api', kwargs={'username': profile_username}), + 'preferences_data': preferences_data, + 'accounts_data': accounts_data, '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, 'account_settings_page_url': reverse('account_settings'), 'has_preferences_access': (logged_in_username == profile_username or user_is_staff), - 'own_profile': (logged_in_username == profile_username), + 'own_profile': own_profile, 'country_options': country_options, 'language_options': settings.ALL_LANGUAGES, 'platform_name': microsite.get_value('platform_name', settings.PLATFORM_NAME), } } - return context diff --git a/lms/static/js/student_profile/views/learner_profile_factory.js b/lms/static/js/student_profile/views/learner_profile_factory.js index a66dc3f45e..28ba6ab2af 100644 --- a/lms/static/js/student_profile/views/learner_profile_factory.js +++ b/lms/static/js/student_profile/views/learner_profile_factory.js @@ -16,18 +16,19 @@ var learnerProfileElement = $('.wrapper-profile'); var defaultVisibility = options.default_visibility; - var AccountPreferencesModelWithDefaults = AccountPreferencesModel.extend({ - defaults: { - account_privacy: defaultVisibility - } - }); - var accountPreferencesModel = new AccountPreferencesModelWithDefaults(); - accountPreferencesModel.url = options.preferences_api_url; + var accountPreferencesModel, accountSettingsModel; - var accountSettingsModel = new AccountSettingsModel({ - 'default_public_account_fields': options.default_public_account_fields - }); + if (options.own_profile) { + accountSettingsModel = new AccountSettingsModel({ + 'default_public_account_fields': options.default_public_account_fields + }); + accountPreferencesModel = new AccountPreferencesModel({account_privacy: defaultVisibility}); + } else { + accountSettingsModel = new AccountSettingsModel(options.accounts_data, {parse: true}); + accountPreferencesModel = new AccountPreferencesModel(options.preferences_data); + } accountSettingsModel.url = options.accounts_api_url; + accountPreferencesModel.url = options.preferences_api_url; var editable = options.own_profile ? 'toggle' : 'never'; @@ -146,26 +147,34 @@ learnerProfileView.render(); }; - accountSettingsModel.fetch({ - success: function () { - // Fetch the preferences model if the user has access - if (options.has_preferences_access) { - accountPreferencesModel.fetch({ - success: function() { - if (accountSettingsModel.get('requires_parental_consent')) { - accountPreferencesModel.set('account_privacy', 'private'); - } - showLearnerProfileView(); - }, - error: showLoadingError - }); + if (options.own_profile) { + accountSettingsModel.fetch({ + success: function () { + // Fetch the preferences model if the user has access + if (options.has_preferences_access) { + accountPreferencesModel.fetch({ + success: function () { + if (accountSettingsModel.get('requires_parental_consent')) { + accountPreferencesModel.set('account_privacy', 'private'); + } + showLearnerProfileView(); + }, + error: showLoadingError + }); + } else { + showLearnerProfileView(); + } + }, + error: showLoadingError + }); + } else { + if (options.has_preferences_access) { + if (accountSettingsModel.get('requires_parental_consent')) { + accountPreferencesModel.set('account_privacy', 'private'); } - else { - showLearnerProfileView(); - } - }, - error: showLoadingError - }); + } + showLearnerProfileView(); + } return { accountSettingsModel: accountSettingsModel, diff --git a/lms/static/js/views/fields.js b/lms/static/js/views/fields.js index c0424b4ada..67e42e296d 100644 --- a/lms/static/js/views/fields.js +++ b/lms/static/js/views/fields.js @@ -201,13 +201,15 @@ this.$el.addClass('mode-edit'); }, - startEditing: function () { + startEditing: function (event) { + event.preventDefault(); if (this.editable === 'toggle' && this.mode !== 'edit') { this.showEditMode(true); } }, - finishEditing: function() { + finishEditing: function(event) { + event.preventDefault(); if (this.fieldValue() !== this.modelValue()) { this.saveValue(); } else { diff --git a/lms/static/sass/views/_learner-profile.scss b/lms/static/sass/views/_learner-profile.scss index b929151c98..7c56d30d08 100644 --- a/lms/static/sass/views/_learner-profile.scss +++ b/lms/static/sass/views/_learner-profile.scss @@ -218,7 +218,6 @@ .wrapper-profile-section-two { width: flex-grid(8, 12); - margin-top: ($baseline*1.5); } .profile-section-two-fields { diff --git a/lms/templates/student_profile/learner_profile.html b/lms/templates/student_profile/learner_profile.html index d1159103c6..3d895ed744 100644 --- a/lms/templates/student_profile/learner_profile.html +++ b/lms/templates/student_profile/learner_profile.html @@ -1,6 +1,7 @@ <%! import json %> <%! from django.core.urlresolvers import reverse %> <%! from django.utils.translation import ugettext as _ %> +<%! from xmodule.modulestore import EdxJSONEncoder %> <%inherit file="/main.html" /> <%namespace name='static' file='/static_content.html'/> @@ -38,7 +39,7 @@