-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract relate filter from rest module (fixes #35)
- Loading branch information
Showing
9 changed files
with
121 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from rest_framework.filters import BaseFilterBackend | ||
|
||
from wq.db.rest.models import get_ct, get_by_identifier | ||
from .models import get_related_parents | ||
|
||
|
||
class RelatedFilterBackend(BaseFilterBackend): | ||
def filter_queryset(self, request, queryset, view): | ||
ctype = get_ct(view.model) | ||
filter = {} | ||
for key, val in list(view.kwargs.items()) + list(request.GET.items()): | ||
if not key.startswith('related_'): | ||
continue | ||
if isinstance(val, list): | ||
val = val[0] | ||
for pct in get_related_parents(ctype): | ||
if key == 'related_' + pct.identifier: | ||
pclass = pct.model_class() | ||
parent = get_by_identifier(pclass.objects, val) | ||
objs = view.model.objects.filter_by_related(parent) | ||
filter['pk__in'] = objs.values_list('pk', flat=True) | ||
return queryset.filter(**filter) |
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,53 @@ | ||
from rest_framework.routers import Route | ||
from wq.db.rest.views import ModelViewSet | ||
from wq.db.rest.models import get_ct | ||
from .models import get_related_parents, get_related_children | ||
from .filters import RelatedFilterBackend | ||
|
||
|
||
class RelatedModelViewSet(ModelViewSet): | ||
filter_backends = ModelViewSet.filter_backends + [RelatedFilterBackend] | ||
|
||
def list(self, request, *args, **kwargs): | ||
response = super(RelatedModelViewSet, self).list( | ||
request, *args, **kwargs | ||
) | ||
ct = get_ct(self.model) | ||
for pct in get_related_parents(ct): | ||
self.get_parent(pct, 'related_%s' % pct.identifier, response) | ||
return response | ||
|
||
@classmethod | ||
def extra_routes(cls): | ||
routes = [] | ||
ct = get_ct(cls.model) | ||
for pct in get_related_parents(ct): | ||
if not pct.is_registered(): | ||
continue | ||
if pct.urlbase == '': | ||
purlbase = '' | ||
else: | ||
purlbase = pct.urlbase + '/' | ||
|
||
routes.append(Route( | ||
( | ||
'^' + purlbase + r'(?P<related_' + pct.identifier | ||
+ '>[^\/\?]+)/{prefix}{trailing_slash}$' | ||
), | ||
mapping={'get': 'list'}, | ||
name="{basename}-for-related-%s" % pct.identifier, | ||
initkwargs={'suffix': 'List'}, | ||
)) | ||
|
||
for cct in get_related_children(ct): | ||
if not cct.is_registered(): | ||
continue | ||
cbase = cct.urlbase | ||
routes.append(Route( | ||
url='^%s-by-{prefix}' % cbase, | ||
mapping={'get': 'list'}, | ||
name="%s-by-%s" % (cct.identifier, ct.identifier), | ||
initkwargs={'target': cbase, 'suffix': 'List'}, | ||
)) | ||
|
||
return routes |
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
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