-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(generic): use
import_string
instead of custom method
This gives us a bit more flexibility regarding the searched dotted paths. Closes: #697
- Loading branch information
Showing
4 changed files
with
64 additions
and
79 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 |
---|---|---|
@@ -1,32 +1,35 @@ | ||
from rest_framework import viewsets | ||
from .serializers import serializer_factory, GenericHyperlinkedModelSerializer | ||
from .helpers import first_match_via_mro | ||
from .helpers import module_paths, first_member_match | ||
|
||
|
||
class ModelViewSet(viewsets.ModelViewSet): | ||
""" | ||
API ViewSet for a generic model. | ||
The queryset is overridden by the first match from | ||
the `first_match_via_mro` helper. | ||
the `first_member_match` helper. | ||
The serializer class is overridden by the first match from | ||
the `first_match_via_mro` helper. | ||
the `first_member_match` helper. | ||
""" | ||
|
||
def dispatch(self, *args, **kwargs): | ||
self.model = kwargs.get("contenttype").model_class() | ||
return super().dispatch(*args, **kwargs) | ||
|
||
def get_queryset(self): | ||
queryset = first_match_via_mro( | ||
queryset_methods = module_paths( | ||
self.model, path="querysets", suffix="ViewSetQueryset" | ||
) or (lambda x: x) | ||
) | ||
queryset = first_member_match(queryset_methods, lambda x: x) | ||
return queryset(self.model.objects.all()) | ||
|
||
def get_serializer_class(self): | ||
renderer = self.request.accepted_renderer | ||
serializer_class = ( | ||
first_match_via_mro(self.model, path="serializers", suffix="Serializer") | ||
or getattr(renderer, "serializer", None) | ||
or GenericHyperlinkedModelSerializer | ||
serializer_class_modules = module_paths( | ||
self.model, path="serializers", suffix="Serializer" | ||
) | ||
serializer_class = first_member_match( | ||
serializer_class_modules, | ||
getattr(renderer, "serializer", GenericHyperlinkedModelSerializer), | ||
) | ||
return serializer_factory(self.model, serializer=serializer_class) |
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