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

This commit is contained in:
Aider Ibragimov 2015-07-19 00:26:03 +03:00
parent d845157983
commit a1397ac699
4 changed files with 26 additions and 11 deletions

View File

@ -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():
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

View File

@ -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,16 +717,10 @@ 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 path, match=match, recursive=recursive, allow_files=allow_files,
field = DjangoFilePathField( allow_folders=allow_folders, required=required
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
)
self.choices = OrderedDict(field.choices) self.choices = OrderedDict(field.choices)
self.choice_strings_to_values = dict([ self.choice_strings_to_values = dict([

View File

@ -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 = {

View File

@ -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