-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #481 from carltongibson/rest_framework
Add Django REST Framework support
- Loading branch information
Showing
20 changed files
with
738 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# flake8: noqa | ||
from __future__ import absolute_import | ||
from .backends import DjangoFilterBackend | ||
from .filterset import FilterSet | ||
from ..filters import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
|
||
from __future__ import absolute_import | ||
|
||
from django.conf import settings | ||
from django.template import loader | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from rest_framework import compat | ||
from rest_framework.filters import BaseFilterBackend | ||
|
||
from ..compat import crispy_forms | ||
from . import filterset | ||
|
||
|
||
if 'crispy_forms' in settings.INSTALLED_APPS and crispy_forms: | ||
from crispy_forms.helper import FormHelper | ||
from crispy_forms.layout import Layout, Submit | ||
|
||
class FilterSet(filterset.FilterSet): | ||
def __init__(self, *args, **kwargs): | ||
super(FilterSet, self).__init__(*args, **kwargs) | ||
for field in self.form.fields.values(): | ||
field.help_text = None | ||
|
||
layout_components = list(self.form.fields.keys()) + [ | ||
Submit('', _('Submit'), css_class='btn-default'), | ||
] | ||
|
||
helper = FormHelper() | ||
helper.form_method = 'GET' | ||
helper.template_pack = 'bootstrap3' | ||
helper.layout = Layout(*layout_components) | ||
|
||
self.form.helper = helper | ||
|
||
filter_template = 'django_filters/rest_framework/crispy_form.html' | ||
|
||
else: | ||
class FilterSet(filterset.FilterSet): | ||
def __init__(self, *args, **kwargs): | ||
super(FilterSet, self).__init__(*args, **kwargs) | ||
for field in self.form.fields.values(): | ||
field.help_text = None | ||
|
||
filter_template = 'django_filters/rest_framework/form.html' | ||
|
||
|
||
class DjangoFilterBackend(BaseFilterBackend): | ||
default_filter_set = FilterSet | ||
template = filter_template | ||
|
||
def get_filter_class(self, view, queryset=None): | ||
""" | ||
Return the django-filters `FilterSet` used to filter the queryset. | ||
""" | ||
filter_class = getattr(view, 'filter_class', None) | ||
filter_fields = getattr(view, 'filter_fields', None) | ||
|
||
if filter_class: | ||
filter_model = filter_class.Meta.model | ||
|
||
assert issubclass(queryset.model, filter_model), \ | ||
'FilterSet model %s does not match queryset model %s' % \ | ||
(filter_model, queryset.model) | ||
|
||
return filter_class | ||
|
||
if filter_fields: | ||
class AutoFilterSet(self.default_filter_set): | ||
class Meta: | ||
model = queryset.model | ||
fields = filter_fields | ||
|
||
return AutoFilterSet | ||
|
||
return None | ||
|
||
def filter_queryset(self, request, queryset, view): | ||
filter_class = self.get_filter_class(view, queryset) | ||
|
||
if filter_class: | ||
return filter_class(request.query_params, queryset=queryset).qs | ||
|
||
return queryset | ||
|
||
def to_html(self, request, queryset, view): | ||
filter_class = self.get_filter_class(view, queryset) | ||
if not filter_class: | ||
return None | ||
filter_instance = filter_class(request.query_params, queryset=queryset) | ||
context = { | ||
'filter': filter_instance | ||
} | ||
template = loader.get_template(self.template) | ||
return compat.template_render(template, context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
from __future__ import absolute_import | ||
from copy import deepcopy | ||
|
||
from django.db import models | ||
|
||
from django_filters import filterset | ||
from ..filters import BooleanFilter, IsoDateTimeFilter | ||
from ..widgets import BooleanWidget | ||
|
||
|
||
FILTER_FOR_DBFIELD_DEFAULTS = deepcopy(filterset.FILTER_FOR_DBFIELD_DEFAULTS) | ||
FILTER_FOR_DBFIELD_DEFAULTS.update({ | ||
models.DateTimeField: {'filter_class': IsoDateTimeFilter}, | ||
models.BooleanField: { | ||
'filter_class': BooleanFilter, | ||
'extra': lambda f: { | ||
'widget': BooleanWidget, | ||
}, | ||
}, | ||
}) | ||
|
||
|
||
class FilterSet(filterset.FilterSet): | ||
FILTER_DEFAULTS = FILTER_FOR_DBFIELD_DEFAULTS |
5 changes: 5 additions & 0 deletions
5
django_filters/rest_framework/templates/django_filters/rest_framework/crispy_form.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% load crispy_forms_tags %} | ||
{% load i18n %} | ||
|
||
<h2>{% trans "Field filters" %}</h2> | ||
{% crispy filter.form %} |
6 changes: 6 additions & 0 deletions
6
django_filters/rest_framework/templates/django_filters/rest_framework/form.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% load i18n %} | ||
<h2>{% trans "Field filters" %}</h2> | ||
<form class="form" action="" method="get"> | ||
{{ filter.form.as_p }} | ||
<button type="submit" class="btn btn-primary">{% trans "Submit" %}</button> | ||
</form> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ Contents: | |
|
||
install | ||
usage | ||
rest_framework | ||
ref/filterset | ||
ref/filters | ||
ref/fields | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.