Commit 9e856851 by asadiqbal08 Committed by Jonathan Piacenti

fields_need_visible_updatable_via_api_761: merged with master

parent 913252d4
...@@ -11,6 +11,7 @@ from django.utils.translation import get_language, ugettext_lazy as _ ...@@ -11,6 +11,7 @@ from django.utils.translation import get_language, ugettext_lazy as _
from rest_framework import status from rest_framework import status
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
from django.db.models import Q
from api_manager.models import GroupProfile from api_manager.models import GroupProfile
from api_manager.permissions import ApiKeyHeaderPermission from api_manager.permissions import ApiKeyHeaderPermission
...@@ -30,7 +31,6 @@ from util.bad_request_rate_limiter import BadRequestRateLimiter ...@@ -30,7 +31,6 @@ from util.bad_request_rate_limiter import BadRequestRateLimiter
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
AUDIT_LOG = logging.getLogger("audit") AUDIT_LOG = logging.getLogger("audit")
def _generate_base_uri(request): def _generate_base_uri(request):
""" """
Constructs the protocol:host:path component of the resource uri Constructs the protocol:host:path component of the resource uri
...@@ -46,6 +46,17 @@ def _generate_base_uri(request): ...@@ -46,6 +46,17 @@ def _generate_base_uri(request):
return resource_uri return resource_uri
def _serialize_user_profile(response_data, user_profile):
"""This function serialize user profile """
response_data['full_name'] = user_profile.name
response_data['city'] = user_profile.city
response_data['country'] = user_profile.country.code
response_data['level_of_education'] = user_profile.level_of_education
response_data['year_of_birth'] = user_profile.year_of_birth
response_data['gender'] = user_profile.gender
return response_data
def _serialize_user(response_data, user): def _serialize_user(response_data, user):
""" """
Loads the object data into the response dict Loads the object data into the response dict
...@@ -59,7 +70,6 @@ def _serialize_user(response_data, user): ...@@ -59,7 +70,6 @@ def _serialize_user(response_data, user):
response_data['is_active'] = user.is_active response_data['is_active'] = user.is_active
return response_data return response_data
def _save_module_position(request, user, course_id, course_descriptor, position): def _save_module_position(request, user, course_id, course_descriptor, position):
""" """
Records the indicated position for the specified course Records the indicated position for the specified course
...@@ -109,6 +119,11 @@ class UsersList(APIView): ...@@ -109,6 +119,11 @@ class UsersList(APIView):
first_name = request.DATA.get('first_name', '') first_name = request.DATA.get('first_name', '')
last_name = request.DATA.get('last_name', '') last_name = request.DATA.get('last_name', '')
is_active = request.DATA.get('is_active', None) is_active = request.DATA.get('is_active', None)
city = request.DATA.get('city', '')
country = request.DATA.get('country', '')
level_of_education = request.DATA.get('level_of_education', '')
year_of_birth = request.DATA.get('year_of_birth', '')
gender = request.DATA.get('gender', '')
# enforce password complexity as an optional feature # enforce password complexity as an optional feature
if settings.FEATURES.get('ENFORCE_PASSWORD_POLICY', False): if settings.FEATURES.get('ENFORCE_PASSWORD_POLICY', False):
...@@ -149,6 +164,18 @@ class UsersList(APIView): ...@@ -149,6 +164,18 @@ class UsersList(APIView):
profile = UserProfile(user=user) profile = UserProfile(user=user)
profile.name = '{} {}'.format(first_name, last_name) profile.name = '{} {}'.format(first_name, last_name)
profile.city = city
profile.country = country
profile.level_of_education = level_of_education
profile.gender = gender
try:
profile.year_of_birth = int(year_of_birth)
except ValueError:
# If they give us garbage, just ignore it instead
# of asking them to put an integer.
profile.year_of_birth = None
profile.save() profile.save()
UserPreference.set_preference(user, LANGUAGE_KEY, get_language()) UserPreference.set_preference(user, LANGUAGE_KEY, get_language())
...@@ -170,7 +197,7 @@ class UsersList(APIView): ...@@ -170,7 +197,7 @@ class UsersList(APIView):
response_data['uri'] = '{}/{}'.format(base_uri, str(user.id)) response_data['uri'] = '{}/{}'.format(base_uri, str(user.id))
else: else:
status_code = status.HTTP_409_CONFLICT status_code = status.HTTP_409_CONFLICT
response_data['message'] = "User '%s' already exists", username response_data['message'] = "User '%s' already exists" % (username)
response_data['field_conflict'] = "username" response_data['field_conflict'] = "username"
return Response(response_data, status=status_code) return Response(response_data, status=status_code)
...@@ -194,6 +221,11 @@ class UsersDetail(APIView): ...@@ -194,6 +221,11 @@ class UsersDetail(APIView):
response_data['resources'].append({'uri': resource_uri}) response_data['resources'].append({'uri': resource_uri})
resource_uri = '{}/courses'.format(base_uri) resource_uri = '{}/courses'.format(base_uri)
response_data['resources'].append({'uri': resource_uri}) response_data['resources'].append({'uri': resource_uri})
existing_user_profile = UserProfile.objects.get(user_id=user_id)
if existing_user_profile:
_serialize_user_profile(response_data, existing_user_profile)
return Response(response_data, status=status.HTTP_200_OK) return Response(response_data, status=status.HTTP_200_OK)
except ObjectDoesNotExist: except ObjectDoesNotExist:
return Response(response_data, status=status.HTTP_404_NOT_FOUND) return Response(response_data, status=status.HTTP_404_NOT_FOUND)
...@@ -203,6 +235,15 @@ class UsersDetail(APIView): ...@@ -203,6 +235,15 @@ class UsersDetail(APIView):
POST provides the ability to update information about an existing user POST provides the ability to update information about an existing user
""" """
response_data = {} response_data = {}
first_name = request.DATA.get('first_name', '')
last_name = request.DATA.get('last_name', '')
city = request.DATA.get('city', '')
country = request.DATA.get('country', '')
level_of_education = request.DATA.get('level_of_education', '')
year_of_birth = request.DATA.get('year_of_birth', '')
gender = request.DATA.get('gender', '')
base_uri = _generate_base_uri(request) base_uri = _generate_base_uri(request)
response_data['uri'] = _generate_base_uri(request) response_data['uri'] = _generate_base_uri(request)
# Add some rate limiting here by re-using the RateLimitMixin as a helper class # Add some rate limiting here by re-using the RateLimitMixin as a helper class
...@@ -217,6 +258,26 @@ class UsersDetail(APIView): ...@@ -217,6 +258,26 @@ class UsersDetail(APIView):
limiter.tick_bad_request_counter(request) limiter.tick_bad_request_counter(request)
existing_user = None existing_user = None
if existing_user: if existing_user:
username = request.DATA.get('username', None)
if username:
try:
validate_slug(username)
except ValidationError:
status_code = status.HTTP_400_BAD_REQUEST
response_data['message'] = _('Username should only consist of A-Z and 0-9, with no spaces.')
return Response(response_data, status=status_code)
existing_username = User.objects.filter(username=username).filter(~Q(id=user_id))
if existing_username:
status_code = status.HTTP_409_CONFLICT
response_data['message'] = "User '%s' already exists" % (username)
response_data['field_conflict'] = "username"
return Response(response_data, status=status_code)
existing_user.username = username
response_data['username'] = existing_user.username
existing_user.save()
is_active = request.DATA.get('is_active', None) is_active = request.DATA.get('is_active', None)
if is_active is not None: if is_active is not None:
...@@ -278,6 +339,23 @@ class UsersDetail(APIView): ...@@ -278,6 +339,23 @@ class UsersDetail(APIView):
password_history_entry = PasswordHistory() password_history_entry = PasswordHistory()
password_history_entry.create(existing_user) password_history_entry.create(existing_user)
existing_user_profile = UserProfile.objects.get(user_id=user_id)
if existing_user_profile:
existing_user_profile.name = '{} {}'.format(first_name, last_name)
existing_user_profile.city = city
existing_user_profile.country = country
existing_user_profile.level_of_education = level_of_education
existing_user_profile.gender = gender
try:
existing_user_profile.year_of_birth = int(year_of_birth)
except ValueError:
# If they give us garbage, just ignore it instead
# of asking them to put an integer.
existing_user_profile.year_of_birth = None
existing_user_profile.save()
status_code = status.HTTP_200_OK status_code = status.HTTP_200_OK
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment