-
Notifications
You must be signed in to change notification settings - Fork 0
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 #28 from stefanofusai/27-add-support-for-versioning
- Loading branch information
Showing
4 changed files
with
62 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "drf-multi-serializers" | ||
version = "1.1.1" | ||
version = "1.2.0" | ||
authors = [{ "name" = "Stefano Fusai", "email" = "[email protected]" }] | ||
description = "Handle multiple serializers for the same view in Django Rest Framework." | ||
readme = "README.md" | ||
|
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 @@ | ||
class ActionVersionSetError(Exception): | ||
"""Raised when serializer_classes matches against both action and version.""" | ||
|
||
def __init__(self) -> None: # noqa: D107 | ||
super().__init__("action and version can't be set at the same time") |
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,13 +1,34 @@ | ||
from rest_framework.serializers import Serializer | ||
|
||
from .exceptions import ActionVersionSetError | ||
|
||
|
||
class MultiSerializerMixin: | ||
"""A mixin that allows you to define different serializers for different ViewSet actions.""" | ||
"""A mixin that allows you to define different serializers for different view actions/methods/versions.""" | ||
|
||
serializer_classes: dict[str, type[Serializer]] | ||
serializer_classes: dict[str, type[Serializer] | dict[str, type[Serializer]]] | ||
|
||
def get_serializer_class(self) -> type[Serializer]: # noqa: D102 | ||
if self.action in self.serializer_classes: | ||
return self.serializer_classes[self.action] | ||
action = getattr(self, "action", None) | ||
version = self.request.version | ||
|
||
if action in self.serializer_classes and version in self.serializer_classes: | ||
raise ActionVersionSetError | ||
|
||
if action is not None and action in self.serializer_classes: | ||
action_serializer_or_versioned_serializers = self.serializer_classes[action] | ||
|
||
if ( | ||
isinstance(action_serializer_or_versioned_serializers, dict) | ||
and version is not None | ||
and version in action_serializer_or_versioned_serializers | ||
): | ||
return action_serializer_or_versioned_serializers[version] | ||
|
||
if issubclass(action_serializer_or_versioned_serializers, Serializer): | ||
return action_serializer_or_versioned_serializers | ||
|
||
if version is not None and version in self.serializer_classes: | ||
return self.serializer_classes[version] | ||
|
||
return super().get_serializer_class() |