Commit a1397ac6 by Aider Ibragimov

fix bugs, move version branching to compat, update ModelSerializer mapping

parent d8451579
...@@ -12,6 +12,7 @@ import django ...@@ -12,6 +12,7 @@ import django
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.db import connection, transaction from django.db import connection, transaction
from django.forms import FilePathField as DjangoFilePathField
from django.test.client import FakePayload from django.test.client import FakePayload
from django.utils import six from django.utils import six
from django.utils.encoding import force_text from django.utils.encoding import force_text
...@@ -291,3 +292,21 @@ def set_rollback(): ...@@ -291,3 +292,21 @@ def set_rollback():
else: else:
# transaction not managed # transaction not managed
pass pass
def get_filepathfield(path, match=None, recursive=False, allow_files=True,
allow_folders=False, required=None):
"""Create proper Django FilePathField with allowed kwargs."""
if django.VERSION < (1, 5):
# django field doesn't have allow_folders, allow_files kwargs
field = DjangoFilePathField(
path, match=match, recursive=recursive, required=required
)
else:
field = DjangoFilePathField(
path, match=match, recursive=recursive, allow_files=allow_files,
allow_folders=allow_folders, required=required
)
return field
...@@ -26,7 +26,7 @@ from rest_framework import ISO_8601 ...@@ -26,7 +26,7 @@ from rest_framework import ISO_8601
from rest_framework.compat import ( from rest_framework.compat import (
EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator, EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, OrderedDict, URLValidator, duration_string, MinValueValidator, OrderedDict, URLValidator, duration_string,
parse_duration, unicode_repr, unicode_to_repr parse_duration, unicode_repr, unicode_to_repr, get_filepathfield
) )
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
...@@ -717,13 +717,7 @@ class FilePathField(CharField): ...@@ -717,13 +717,7 @@ class FilePathField(CharField):
super(FilePathField, self).__init__(**kwargs) super(FilePathField, self).__init__(**kwargs)
# create field and get options to avoid code duplication # create field and get options to avoid code duplication
if django.VERSION < (1, 5): field = get_filepathfield(
# django field doesn't have allow_folders, allow_files kwargs
field = DjangoFilePathField(
path, match=match, recursive=recursive, required=required
)
else:
field = DjangoFilePathField(
path, match=match, recursive=recursive, allow_files=allow_files, path, match=match, recursive=recursive, allow_files=allow_files,
allow_folders=allow_folders, required=required allow_folders=allow_folders, required=required
) )
......
...@@ -191,6 +191,7 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend): ...@@ -191,6 +191,7 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
perm_format = '%(app_label)s.view_%(model_name)s' perm_format = '%(app_label)s.view_%(model_name)s'
def filter_queryset(self, request, queryset, view): def filter_queryset(self, request, queryset, view):
extra = {}
user = request.user user = request.user
model_cls = queryset.model model_cls = queryset.model
kwargs = { kwargs = {
......
...@@ -773,6 +773,7 @@ class ModelSerializer(Serializer): ...@@ -773,6 +773,7 @@ class ModelSerializer(Serializer):
models.TimeField: TimeField, models.TimeField: TimeField,
models.URLField: URLField, models.URLField: URLField,
models.GenericIPAddressField: IPAddressField, models.GenericIPAddressField: IPAddressField,
models.FilePathField: FilePathField,
} }
if ModelDurationField is not None: if ModelDurationField is not None:
serializer_field_mapping[ModelDurationField] = DurationField serializer_field_mapping[ModelDurationField] = DurationField
......
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