-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
84 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import dataclasses | ||
|
||
from rest_framework_json_api.relations import ( | ||
ResourceRelatedField, | ||
SkipDataMixin, | ||
) | ||
|
||
|
||
class DataclassRelatedField(SkipDataMixin, ResourceRelatedField): | ||
def __init__(self, /, dataclass_model, **kwargs): | ||
assert dataclasses.is_dataclass(dataclass_model) | ||
return super().__init__( | ||
read_only=True, | ||
model=dataclass_model, | ||
**kwargs, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
from rest_framework_json_api import serializers | ||
from rest_framework_json_api.relations import ( | ||
ResourceRelatedField, | ||
SkipDataMixin, | ||
) | ||
from rest_framework_json_api.utils import get_resource_type_from_model | ||
|
||
from addon_service.common.serializer_fields import DataclassRelatedField | ||
from addon_service.storage_operation.models import StorageOperationModel | ||
|
||
from .models import StorageImpModel | ||
|
||
|
||
# note: most other serializers get resource_type from the db model | ||
# (for rest_framework_json_api to behave correctly) but operations | ||
# are declared as dataclasses, not stored in a database | ||
RESOURCE_TYPE = "storage-imps" | ||
RESOURCE_TYPE = get_resource_type_from_model(StorageImpModel) | ||
SELF_LINK_VIEW_NAME = f"{RESOURCE_TYPE}-detail" | ||
RELATED_LINK_VIEW_NAME = f"{RESOURCE_TYPE}-related" | ||
|
||
|
||
class StorageImpSerializer(serializers.Serializer): | ||
url = serializers.HyperlinkedIdentityField(view_name=SELF_LINK_VIEW_NAME) | ||
name = serializers.CharField(read_only=True) | ||
docstring = serializers.CharField(read_only=True) | ||
# TODO operation_parameters = ListField( | ||
|
||
class JSONAPIMeta: | ||
resource_name = RESOURCE_TYPE | ||
|
||
|
||
class StorageImpRelationField(SkipDataMixin, ResourceRelatedField): | ||
def __init__(self, **kwargs): | ||
return super().__init__( | ||
many=True, | ||
read_only=True, | ||
model=StorageImpModel, | ||
**kwargs, | ||
) | ||
implemented_operations = DataclassRelatedField( | ||
dataclass_model=StorageOperationModel, | ||
related_link_view_name=RELATED_LINK_VIEW_NAME, | ||
) | ||
|
||
class Meta: | ||
model = StorageImpModel | ||
fields = [ | ||
"url", | ||
"name", | ||
"docstring", | ||
"implemented_operations", | ||
] |
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