Account settings page should load and create student user profile

ECOM-4776
This commit is contained in:
Ahsan Ulhaq
2017-03-30 16:57:56 +05:00
parent be5c4fad8b
commit 04ca94eaef
8 changed files with 156 additions and 30 deletions

View File

@@ -246,10 +246,11 @@ def _get_user_and_profile(username):
"""
try:
existing_user = User.objects.get(username=username)
existing_user_profile = UserProfile.objects.get(user=existing_user)
except ObjectDoesNotExist:
raise UserNotFound()
existing_user_profile, _ = UserProfile.objects.get_or_create(user=existing_user)
return existing_user, existing_user_profile

View File

@@ -1,9 +1,12 @@
"""
Django REST Framework serializers for the User API Accounts sub-application
"""
import logging
from rest_framework import serializers
from django.contrib.auth.models import User
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from lms.djangoapps.badges.utils import badges_enabled
@@ -18,6 +21,7 @@ from .image_helpers import get_profile_image_urls_for_user
PROFILE_IMAGE_KEY_PREFIX = 'image_url'
LOGGER = logging.getLogger(__name__)
class LanguageProficiencySerializer(serializers.ModelSerializer):
@@ -63,7 +67,12 @@ class UserReadOnlySerializer(serializers.Serializer):
:param user: User object
:return: Dict serialized account
"""
profile = user.profile
try:
user_profile = user.profile
except ObjectDoesNotExist:
user_profile = None
LOGGER.warning("user profile for the user [%s] does not exist", user.username)
accomplishments_shared = badges_enabled()
data = {
@@ -78,32 +87,51 @@ class UserReadOnlySerializer(serializers.Serializer):
# https://docs.djangoproject.com/en/1.8/ref/databases/#fractional-seconds-support-for-time-and-datetime-fields
"date_joined": user.date_joined.replace(microsecond=0),
"is_active": user.is_active,
"bio": AccountLegacyProfileSerializer.convert_empty_to_None(profile.bio),
"country": AccountLegacyProfileSerializer.convert_empty_to_None(profile.country.code),
"profile_image": AccountLegacyProfileSerializer.get_profile_image(
profile,
user,
self.context.get('request')
),
"language_proficiencies": LanguageProficiencySerializer(
profile.language_proficiencies.all(),
many=True
).data,
"name": profile.name,
"gender": AccountLegacyProfileSerializer.convert_empty_to_None(profile.gender),
"goals": profile.goals,
"year_of_birth": profile.year_of_birth,
"level_of_education": AccountLegacyProfileSerializer.convert_empty_to_None(profile.level_of_education),
"mailing_address": profile.mailing_address,
"requires_parental_consent": profile.requires_parental_consent(),
"bio": None,
"country": None,
"profile_image": None,
"language_proficiencies": None,
"name": None,
"gender": None,
"goals": None,
"year_of_birth": None,
"level_of_education": None,
"mailing_address": None,
"requires_parental_consent": None,
"accomplishments_shared": accomplishments_shared,
"account_privacy": get_profile_visibility(profile, user, self.configuration),
"account_privacy": self.configuration.get('default_visibility')
}
if user_profile:
data.update(
{
"bio": AccountLegacyProfileSerializer.convert_empty_to_None(user_profile.bio),
"country": AccountLegacyProfileSerializer.convert_empty_to_None(user_profile.country.code),
"profile_image": AccountLegacyProfileSerializer.get_profile_image(
user_profile, user, self.context.get('request')
),
"language_proficiencies": LanguageProficiencySerializer(
user_profile.language_proficiencies.all(), many=True
).data,
"name": user_profile.name,
"gender": AccountLegacyProfileSerializer.convert_empty_to_None(user_profile.gender),
"goals": user_profile.goals,
"year_of_birth": user_profile.year_of_birth,
"level_of_education": AccountLegacyProfileSerializer.convert_empty_to_None(
user_profile.level_of_education
),
"mailing_address": user_profile.mailing_address,
"requires_parental_consent": user_profile.requires_parental_consent(),
"account_privacy": get_profile_visibility(user_profile, user, self.configuration)
}
)
if self.custom_fields:
fields = self.custom_fields
elif user_profile:
fields = _visible_fields(user_profile, user, self.configuration)
else:
fields = _visible_fields(profile, user, self.configuration)
fields = self.configuration.get('public_fields')
return self._filter_fields(
fields,

View File

@@ -0,0 +1,58 @@
"""
Test cases to cover Accounts-related serializers of the User API application
"""
import logging
from django.test import TestCase
from django.test.client import RequestFactory
from testfixtures import LogCapture
from student.models import UserProfile
from student.tests.factories import UserFactory
from openedx.core.djangoapps.user_api.accounts.serializers import UserReadOnlySerializer
LOGGER_NAME = "openedx.core.djangoapps.user_api.accounts.serializers"
class UserReadOnlySerializerTest(TestCase):
def setUp(self):
super(UserReadOnlySerializerTest, self).setUp()
request_factory = RequestFactory()
self.request = request_factory.get('/api/user/v1/accounts/')
self.user = UserFactory.build(username='test_user', email='test_user@test.com')
self.user.save()
self.config = {
"default_visibility": "public",
"shareable_fields": [
'name',
],
"public_fields": [
'email', 'name', 'username'
],
}
def test_serializer_data(self):
"""
Test serializer return data properly.
"""
UserProfile.objects.create(user=self.user, name='test name')
data = UserReadOnlySerializer(self.user, configuration=self.config, context={'request': self.request}).data
self.assertEqual(data['username'], self.user.username)
self.assertEqual(data['name'], 'test name')
self.assertEqual(data['email'], self.user.email)
def test_user_no_profile(self):
"""
Test serializer return data properly when user does not have profile.
"""
with LogCapture(LOGGER_NAME, level=logging.DEBUG) as logger:
data = UserReadOnlySerializer(self.user, configuration=self.config, context={'request': self.request}).data
logger.check(
(LOGGER_NAME, 'WARNING', 'user profile for the user [test_user] does not exist')
)
self.assertEqual(data['username'], self.user.username)
self.assertEqual(data['name'], None)

View File

@@ -20,9 +20,13 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
def get_name(self, user):
"""
Return the name attribute from the user profile object
Return the name attribute from the user profile object if profile exists else none
"""
profile = UserProfile.objects.get(user=user)
try:
profile = UserProfile.objects.get(user=user)
except UserProfile.DoesNotExist:
return None
return profile.name
def get_preferences(self, user):