Move code samples to utils.py

This commit is contained in:
Radoslav Georgiev 2018-12-28 19:24:43 +02:00
parent ffaf960cde
commit 5105e53b7a
2 changed files with 51 additions and 0 deletions

View File

@ -349,6 +349,8 @@ class CourseCreateApi(
return Response(status=status.HTTP_201_CREATED)
```
All of code above can be found in `utils.py` in this repository.
## Inspiration
The way we do Django is inspired by the following things:

View File

@ -1,3 +1,9 @@
from rest_framework import serializers
from rest_framework import exceptions as rest_exceptions
from django.core.exceptions import ValidationError
def create_serializer_class(name, fields):
return type(name, (serializers.Serializer, ), fields)
@ -9,3 +15,46 @@ def inline_serializer(*, fields, data=None, **kwargs):
return serializer_class(data=data, **kwargs)
return serializer_class(**kwargs)
def get_first_matching_attr(obj, *attrs, default=None):
for attr in attrs:
if hasattr(obj, attr):
return getattr(obj, attr)
return default
def get_error_message(exc):
if hasattr(exc, 'message_dict'):
return exc.message_dict
error_msg = get_first_matching_attr(exc, 'message', 'messages')
if isinstance(error_msg, list):
error_msg = ', '.join(error_msg)
if error_msg is None:
error_msg = str(exc)
return error_msg
class ExceptionHandlerMixin:
"""
Mixin that transforms Django and Python exceptions into rest_framework ones.
without the mixin, they return 500 status code which is not desired.
"""
expected_exceptions = {
ValueError: rest_exceptions.ValidationError,
ValidationError: rest_exceptions.ValidationError,
PermissionError: rest_exceptions.PermissionDenied
}
def handle_exception(self, exc):
if isinstance(exc, tuple(self.expected_exceptions.keys())):
drf_exception_class = self.expected_exceptions[exc.__class__]
drf_exception = drf_exception_class(get_error_message(exc))
return super().handle_exception(drf_exception)
return super().handle_exception(exc)