From 8c1e0fff0adb1ae582d1749f0131d2e39b0614aa Mon Sep 17 00:00:00 2001 From: Ethan Paul Date: Mon, 24 Feb 2020 21:29:10 -0500 Subject: [PATCH] Add serializers for domain extras Update domain serializer to properly access extras --- keyosk/serializers/domain.py | 24 ++++++++++++++++++------ keyosk/serializers/domain_extras.py | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 keyosk/serializers/domain_extras.py diff --git a/keyosk/serializers/domain.py b/keyosk/serializers/domain.py index dea344c..3d982e1 100644 --- a/keyosk/serializers/domain.py +++ b/keyosk/serializers/domain.py @@ -1,11 +1,17 @@ +from typing import List + import marshmallow as msh +from keyosk import fields as custom_fields +from keyosk.serializers.domain_extras import DomainAccessListSerializer +from keyosk.serializers.domain_extras import DomainPermissionSerializer + class DomainSerializer(msh.Schema): uuid = msh.fields.UUID(required=True) - created = msh.fields.DateTime(required=True) - updated = msh.fields.DateTime(required=True) + created = custom_fields.Epoch(required=True) + updated = custom_fields.Epoch(required=True) name = msh.fields.String(required=True) audience = msh.fields.String(required=True) title = msh.fields.String(required=True, allow_none=True) @@ -21,9 +27,15 @@ class DomainSerializer(msh.Schema): enable_refresh = msh.fields.Boolean(required=True, data_key="enable-refresh") lifespan_access = msh.fields.Boolean(required=True, data_key="lifespan-access") lifespan_refresh = msh.fields.Boolean(required=True, data_key="lifespan-refresh") - access_list_names = msh.fields.List( - msh.fields.String(), required=True, data_key="access-lists" + access_lists = msh.fields.List( + msh.fields.Nested(DomainAccessListSerializer), + required=True, + data_key="access-lists", ) - permission_names = msh.fields.List( - msh.fields.String(), required=True, data_key="permissions" + permissions = msh.fields.List( + msh.fields.Nested(DomainPermissionSerializer), required=True ) + + @staticmethod + def creation_fields() -> List[str]: + return ["uuid", "created", "updated"] diff --git a/keyosk/serializers/domain_extras.py b/keyosk/serializers/domain_extras.py new file mode 100644 index 0000000..cb0f736 --- /dev/null +++ b/keyosk/serializers/domain_extras.py @@ -0,0 +1,22 @@ +from typing import Dict + +import marshmallow as msh + + +class DomainAccessListSerializer(msh.Schema): + + name = msh.fields.String(required=True) + + @msh.pre_load + def _from_string(self, data: str, *args, **kwargs) -> Dict[str, str]: + return {"name": data} + + @msh.post_dump + def _to_string(self, data, *args, **kwargs) -> str: + return data["name"] + + +class DomainPermissionSerializer(msh.Schema): + + name = msh.fields.String(required=True) + bitindex = msh.fields.Integer(required=True, validate=msh.validate.Range(min=0))