Skip to content

Commit

Permalink
fix: fix generating OPTIONS for models
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodaher committed Apr 27, 2022
1 parent 6df972c commit f4f2309
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sanic-rest"
version = "1.7.0"
version = "1.7.1"
description = "Sanic Rest Framework with Google Cloud Datastore"
authors = ["Joao Daher <[email protected]>"]
repository = "https://github.com/flamingo-run/sanic-rest"
Expand Down
27 changes: 22 additions & 5 deletions sanic_rest/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import inspect
import json
import math
import os
Expand All @@ -9,7 +10,13 @@

import aiofiles
import pydantic
from gcp_pilot.datastore import Document, DoesNotExist, DEFAULT_PK_FIELD_TYPE, DEFAULT_PK_FIELD_NAME
from gcp_pilot.datastore import (
Document,
DoesNotExist,
DEFAULT_PK_FIELD_TYPE,
DEFAULT_PK_FIELD_NAME,
EmbeddedDocument,
)
from gcp_pilot.exceptions import ValidationError
import sanic.views
import sanic.request
Expand Down Expand Up @@ -184,10 +191,20 @@ async def options(self, request: sanic.request.Request) -> sanic.response.HTTPRe
return sanic.response.json(data, 200, default=str)

async def perform_options(self) -> PayloadType:
return {
field_info.name: {"required": field_info.required, "type": field_info.type_.__name__}
for field_info in self.model.__fields__
}
def _get_options(klass):
payload = {}
for field_name, field_info in klass.__fields__.items():
if inspect.isclass(field_info.type_) and issubclass(field_info.type_, EmbeddedDocument):
field_type = "struct"
extra = {"struct": _get_options(klass=field_info.type_)}
else:
field_type = field_info.type_.__name__
extra = {}
payload[field_name] = {"required": field_info.required, "type": field_type, **extra}

return payload

return _get_options(klass=self.model)


class DetailView(ViewBase, abc.ABC):
Expand Down

0 comments on commit f4f2309

Please sign in to comment.