0

I'm trying to implement a route in my flask application to serve the OPTIONS method of a given resource and return a description of the marshmallow schema associated to said resource. Something similar to what django does:

{
    "name": "Dataset List",
    "description": "",
    "renders": [
        "application/json",
        "text/html"
    ],
    "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
    ],
    "actions": {
        "POST": {
            "id": {
                "type": "integer",
                "required": false,
                "read_only": true,
                "label": "ID"
            },
            "url": {
                "type": "field",
                "required": false,
                "read_only": true,
                "label": "Url"
            },
            "name": {
                "type": "string",
                "required": true,
                "read_only": false,
                "label": "Name",
                "help_text": "The dataset name.",
                "max_length": 256
            }
        }
    }
}

I can't seem to find any method in Schema or in the docs that returns this description, and AlbumSchema.opts.fields is empty. Is it possible to iterate throughout the fields and build the description I need? Something in the line of:

desc = {f: {"required": f.required,
            "type": f.__class__.__name__,
            ...}
        for f in AlbumSchema.fields}
ldavid
  • 2,512
  • 2
  • 22
  • 38

1 Answers1

0

Thanks to @Pop I managed to find a solution.
First, I declared a generic options method:

class DescriptiveMethodView(views.MethodView):
    name = description = uri = doc_uri = None

    def options(self, id=None):
        """Base Options View.
        ---
        description: retrieve options available for this resource.
        responses:
            200:
                name: description
                type: object
        """
        d = spec.to_dict()

        info = {'name': self.name,
                'description': self.description % {'id': id or 'model'}}

        return jsonify(info=info,
                       definitions=d['definitions'],
                       actions=d['paths'][self.doc_uri])

Then I just inherit from it and override those class attributes:

class FooView(DescriptiveMethodView):
    name = 'Foos'
    description = 'all Foos in the dataset'
    doc_uri = uri = '/foo'


class BarView(DescriptiveMethodView):
    name = 'Bar'
    description = 'A bar in the dataset in the dataset'
    uri = '/bar/<id>'
    doc_uri = '/bar/{id}'
ldavid
  • 2,512
  • 2
  • 22
  • 38