From 842f2623f744e6eeaf33cc9e2c204fbb25c77f62 Mon Sep 17 00:00:00 2001 From: andela-tadesanya Date: Mon, 1 Aug 2016 13:05:17 +0100 Subject: [PATCH 1/2] [#127152487] compute profile completion percentage, add it to context --- common/djangoapps/student/views.py | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index f5a1b98161e1..09ab7757ba93 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -513,6 +513,61 @@ def is_course_blocked(request, redeemed_registration_codes, course_key): return blocked +def profile_progress(user): + try: + profile = UserProfile.objects.get(user=user) + except ObjectDoesNotExist, MultipleObjectsReturned: + return False + + completed_fields = 0 + uncompleted_fields = [] + total_profile_fields = 8 + + if profile.name is not None: + completed_fields += 1 + else: + uncompleted_fields.append('name') + + if profile.language is not None: + completed_fields += 1 + else: + uncompleted_fields.append('language') + + if profile.year_of_birth is not None: + completed_fields += 1 + else: + uncompleted_fields.append('year of birth') + + if profile.gender is not None: + completed_fields += 1 + else: + uncompleted_fields.append('gender') + + if profile.level_of_education is not None: + completed_fields += 1 + else: + uncompleted_fields.append('level of education') + + if profile.country is not None: + completed_fields += 1 + else: + uncompleted_fields.append('country') + + if profile.bio is not None: + completed_fields += 1 + else: + uncompleted_fields.append('bio') + + if profile.profile_image_uploaded_at is not None: + completed_fields += 1 + else: + uncompleted_fields.append('profile image') + + percentage_completed = int((float(completed_fields) / float(total_profile_fields)) * 100) + + return percentage_completed, uncompleted_fields + + @login_required @ensure_csrf_cookie def dashboard(request): @@ -676,6 +731,9 @@ def dashboard(request): else: redirect_message = '' + # get progress for profile + profile_completed, missing_profile_details = profile_progress(user) + context = { 'enrollment_message': enrollment_message, 'redirect_message': redirect_message, @@ -706,6 +764,8 @@ def dashboard(request): 'courses_requirements_not_met': courses_requirements_not_met, 'nav_hidden': True, 'course_programs': course_programs, + 'profile_completed': profile_completed, + 'missing_profile_details': missing_profile_details, } return render_to_response('dashboard.html', context) From 09ba9d041b98f4bdcbad5c8704f8beacf122ab84 Mon Sep 17 00:00:00 2001 From: andela-tadesanya Date: Tue, 2 Aug 2016 13:50:15 +0100 Subject: [PATCH 2/2] [#127152487] refactor profile_progress using filter --- common/djangoapps/student/views.py | 64 +++++++----------------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 09ab7757ba93..e003bf1d72d2 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -127,6 +127,7 @@ # Note that this lives in openedx, so this dependency should be refactored. from openedx.core.djangoapps.user_api.preferences import api as preferences_api from openedx.core.djangoapps.programs.utils import get_programs_for_dashboard +from django.shortcuts import get_object_or_404 log = logging.getLogger("edx.student") @@ -514,58 +515,22 @@ def is_course_blocked(request, redeemed_registration_codes, course_key): def profile_progress(user): - try: - profile = UserProfile.objects.get(user=user) - except ObjectDoesNotExist, MultipleObjectsReturned: - return False - - completed_fields = 0 - uncompleted_fields = [] - total_profile_fields = 8 - - if profile.name is not None: - completed_fields += 1 - else: - uncompleted_fields.append('name') - - if profile.language is not None: - completed_fields += 1 - else: - uncompleted_fields.append('language') - - if profile.year_of_birth is not None: - completed_fields += 1 - else: - uncompleted_fields.append('year of birth') + profile = get_object_or_404(UserProfile, user=user) - if profile.gender is not None: - completed_fields += 1 - else: - uncompleted_fields.append('gender') + profile_fields = [profile.name, + profile.language, + profile.year_of_birth, + profile.gender, + profile.level_of_education, + profile.country, + profile.bio, + profile.profile_image_uploaded_at] - if profile.level_of_education is not None: - completed_fields += 1 - else: - uncompleted_fields.append('level of education') - - if profile.country is not None: - completed_fields += 1 - else: - uncompleted_fields.append('country') - - if profile.bio is not None: - completed_fields += 1 - else: - uncompleted_fields.append('bio') - - if profile.profile_image_uploaded_at is not None: - completed_fields += 1 - else: - uncompleted_fields.append('profile image') + completed_fields = len(filter(lambda x: x is not None, profile_fields)) - percentage_completed = int((float(completed_fields) / float(total_profile_fields)) * 100) + percentage_completed = int((float(completed_fields) / float(len(profile_fields)) * 100)) - return percentage_completed, uncompleted_fields + return percentage_completed @login_required @@ -732,7 +697,7 @@ def dashboard(request): redirect_message = '' # get progress for profile - profile_completed, missing_profile_details = profile_progress(user) + profile_completed = profile_progress(user) context = { 'enrollment_message': enrollment_message, @@ -765,7 +730,6 @@ def dashboard(request): 'nav_hidden': True, 'course_programs': course_programs, 'profile_completed': profile_completed, - 'missing_profile_details': missing_profile_details, } return render_to_response('dashboard.html', context)