Merge pull request #22596 from edx/diana/upgrade-drf

Upgrade DRF to 3.9.4.
This commit is contained in:
Diana Huang
2020-01-02 16:09:02 -05:00
committed by GitHub
10 changed files with 43 additions and 155 deletions

View File

@@ -13,7 +13,6 @@ from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_noop
from django_countries import countries
from pytz import common_timezones, common_timezones_set, country_timezones
from six import text_type
from openedx.core.lib.time_zone_utils import get_display_time_zone
from student.models import User, UserProfile
@@ -279,7 +278,9 @@ def update_email_opt_in(user, org, opt_in):
if hasattr(settings, 'LMS_SEGMENT_KEY') and settings.LMS_SEGMENT_KEY:
_track_update_email_opt_in(user.id, org, opt_in)
except IntegrityError as err:
log.warning(u"Could not update organization wide preference due to IntegrityError: {}".format(text_type(err)))
log.warning(
u"Could not update organization wide preference due to IntegrityError: {}".format(six.text_type(err))
)
def _track_update_email_opt_in(user_id, organization, opt_in):
@@ -386,8 +387,13 @@ def validate_user_preference_serializer(serializer, preference_key, preference_v
}
})
if not serializer.is_valid():
errors = serializer.errors
# DRF error messages are of type ErrorDetail and serialize out as such. We want to coerce those
# messages into the strings only.
for key in errors:
errors[key] = [six.text_type(el) for el in errors[key]]
developer_message = u"Value '{preference_value}' not valid for preference '{preference_key}': {error}".format(
preference_key=preference_key, preference_value=preference_value, error=serializer.errors
preference_key=preference_key, preference_value=preference_value, error=errors
)
if "key" in serializer.errors:
user_message = _(u"Invalid user preference key '{preference_key}'.").format(

View File

@@ -13,7 +13,7 @@ from edx_rest_framework_extensions.auth.session.authentication import SessionAut
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.exceptions import APIException, ErrorDetail
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import RetrieveModelMixin, UpdateModelMixin
from rest_framework.permissions import IsAuthenticated
@@ -132,11 +132,28 @@ def view_auth_classes(is_user=False, is_authenticated=True):
return _decorator
def clean_errors(error):
"""
DRF error messages are of type ErrorDetail and serialize out as such.
We want to coerce the strings into the message only.
This cursively handles the nesting of errors.
"""
if isinstance(error, ErrorDetail):
return text_type(error)
if isinstance(error, list):
return [clean_errors(el) for el in error]
else:
# We assume that it's a nested dictionary if it's not a list.
return {key: clean_errors(value) for key, value in error.items()}
def add_serializer_errors(serializer, data, field_errors):
"""Adds errors from serializer validation to field_errors. data is the original data to deserialize."""
if not serializer.is_valid():
errors = serializer.errors
for key, error in iteritems(errors):
error = clean_errors(error)
field_errors[key] = {
'developer_message': u"Value '{field_value}' is not valid for field '{field_name}': {error}".format(
field_value=data.get(key, ''), field_name=key, error=error