Esempio n. 1
0
    def get_value(self, meta, base_classes_meta, mcs_args: McsArgs):
        value = super().get_value(meta, base_classes_meta, mcs_args)
        if (value is not _missing or mcs_args.Meta.abstract
                or not mcs_args.Meta.model):
            return value

        return '/' + pluralize(kebab_case(mcs_args.Meta.model.__name__))
Esempio n. 2
0
    def get_value(self, meta, base_classes_meta, mcs_args: McsArgs):
        value = super().get_value(meta, base_classes_meta, mcs_args)
        if value is not _missing:
            return value

        ctrl_name = controller_name(mcs_args.name,
                                    mcs_args.getattr(REMOVE_SUFFIXES_ATTR))
        return '/' + pluralize(ctrl_name.replace('_', '-'))
Esempio n. 3
0
 def __get__(self, instance, cls):
     return '/' + pluralize(controller_name(cls))
Esempio n. 4
0
    def register_model_resource(self, resource: ModelResource):
        """
        Method to manually register a :class:`ModelResource` with APISpec.

        :param resource:
        """
        model_name = resource.Meta.model.__name__
        self.spec.add_tag({
            'name': model_name,
            'description': resource.Meta.model.__doc__,
        })

        for method in resource.methods():
            key = f'{resource.__name__}.{method}'
            if key not in unchained.controller_bundle.controller_endpoints:
                continue

            docs = {}
            http_method = method

            if method == CREATE:
                http_method = 'post'
                docs[http_method] = dict(
                    parameters=[{
                        'in': __location_map__['json'],
                        'required': True,
                        'schema': resource.Meta.serializer_create,
                    }],
                    responses={
                        '201':
                        dict(description=getattr(resource, CREATE).__doc__,
                             schema=resource.Meta.serializer_create),
                    },
                )
            elif method == DELETE:
                docs[http_method] = dict(
                    parameters=[],
                    responses={
                        '204':
                        dict(description=getattr(resource, DELETE).__doc__),
                    },
                )
            elif method == GET:
                docs[http_method] = dict(
                    parameters=[],
                    responses={
                        '200':
                        dict(description=getattr(resource, GET).__doc__,
                             schema=resource.Meta.serializer),
                    },
                )
            elif method == LIST:
                http_method = 'get'
                docs[http_method] = dict(
                    parameters=[],
                    responses={
                        '200':
                        dict(description=getattr(resource, LIST).__doc__,
                             schema=resource.Meta.serializer_many),
                    },
                )
            elif method == PATCH:
                docs[http_method] = dict(
                    parameters=[{
                        'in': __location_map__['json'],
                        'required': False,
                        'schema': resource.Meta.serializer,
                    }],
                    responses={
                        '200':
                        dict(description=getattr(resource, PATCH).__doc__,
                             schema=resource.Meta.serializer),
                    },
                )
            elif method == PUT:
                docs[http_method] = dict(
                    parameters=[{
                        'in': __location_map__['json'],
                        'required': True,
                        'schema': resource.Meta.serializer,
                    }],
                    responses={
                        '200':
                        dict(description=getattr(resource, PUT).__doc__,
                             schema=resource.Meta.serializer),
                    },
                )

            docs[http_method]['tags'] = [model_name]
            display_name = title_case(model_name)
            if method == LIST:
                display_name = pluralize(display_name)
            docs[http_method][
                'summary'] = f'{http_method.upper()} {display_name}'

            routes = unchained.controller_bundle.controller_endpoints[key]
            for route in routes:
                for rule in self.app.url_map.iter_rules(route.endpoint):
                    self.spec.add_path(app=self.app,
                                       rule=rule,
                                       operations=docs,
                                       view=route.view_func)
Esempio n. 5
0
 def __plural_label__(self):
     return title_case(pluralize(self.__name__))
Esempio n. 6
0
 def __plural__(self):
     return pluralize(self.__name__)