-
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
10 changed files
with
160 additions
and
41 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,13 @@ | ||
import base64 | ||
|
||
|
||
def make_opaque(key: str) -> str: | ||
"""make a reversible opaque id for the given key | ||
NOT secure in any way, just meant to discourage applying semantic value to ids | ||
""" | ||
return base64.urlsafe_b64encode(str(key).encode()).decode() | ||
|
||
|
||
def unmake_opaque(opaque_key: str) -> str: | ||
return base64.urlsafe_b64decode(opaque_key.encode()).decode() |
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,49 @@ | ||
import dataclasses | ||
|
||
from addon_service.common.opaque import ( | ||
make_opaque, | ||
unmake_opaque, | ||
) | ||
from addon_service.storage_imp.known import ( | ||
ApiStorageImp, | ||
StorageImp, | ||
) | ||
|
||
|
||
# not a database model; just a convenience wrapper for | ||
# AddonOperationImplementation to give the serializer | ||
@dataclasses.dataclass | ||
class StorageImpModel: | ||
interface_imp: type | ||
|
||
@classmethod | ||
def get_by_pk(cls, pk: str) -> "StorageImpModel": | ||
_interface_imp = StorageImp[unmake_opaque(pk)].value | ||
return cls(_interface_imp) | ||
|
||
@property | ||
def pk(self) -> str: | ||
return make_opaque(self.name) | ||
|
||
@property | ||
def name(self) -> str: | ||
return ApiStorageImp.from_original_enum_value(self.interface_imp).value | ||
|
||
@property | ||
def docstring(self): | ||
return self.imp.operation.docstring | ||
|
||
@property | ||
def implementation_docstring(self): | ||
return self.imp.implementation_docstring | ||
|
||
@property | ||
def capability(self): | ||
return self.imp.operation.capability | ||
|
||
@property | ||
def storage_implementation(self) -> str: | ||
# TODO: related storage-implementation resource | ||
return ApiStorageImp.from_original_enum_value( | ||
StorageImp(self.imp.implementation_cls) | ||
).value |
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,18 +1,34 @@ | ||
from addon_service.common.enums.serializers import ( | ||
DualEnumsChoiceField, | ||
DualEnumsMultipleChoiceField, | ||
) | ||
from addon_service.storage_imp.known import ( | ||
ApiStorageImp, | ||
IntStorageImp, | ||
from rest_framework_json_api import serializers | ||
from rest_framework_json_api.relations import ( | ||
ResourceRelatedField, | ||
SkipDataMixin, | ||
) | ||
|
||
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" | ||
SELF_LINK_VIEW_NAME = f"{RESOURCE_TYPE}-detail" | ||
|
||
|
||
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 StorageImpChoiceField(DualEnumsChoiceField): | ||
external_enum = ApiStorageImp | ||
internal_enum = IntStorageImp | ||
class JSONAPIMeta: | ||
resource_name = RESOURCE_TYPE | ||
|
||
|
||
class StorageImpMultipleChoiceField(DualEnumsMultipleChoiceField): | ||
external_enum = ApiStorageImp | ||
internal_enum = IntStorageImp | ||
class StorageImpRelationField(SkipDataMixin, ResourceRelatedField): | ||
def __init__(self, **kwargs): | ||
return super().__init__( | ||
many=True, | ||
read_only=True, | ||
model=StorageImpModel, | ||
**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