def test_collection_resource_spec_gen(resource_class, method, expected_response_codes, expected_tag): serializer = EmployeeSerializer(Employee) resource = collection_resource_factory(resource_class, serializer) specs = getattr(resource, method).specs_dict assert specs["tags"] == [expected_tag] assert specs["responses"].keys() == expected_response_codes
def add_model(self, model, url=None, serializer_class=None, request_decorators=None, collection_decorators=None, collection_name=None, preprocessors=None, postprocessors=None): """ Create API endpoints for the given SQLAlchemy declarative class. :param class model: the SQLAlchemy declarative class :param string url: one or more url routes to match for the resource, standard flask routing rules apply. Defaults to model name in lower case. :param string collection_name: custom name for the collection endpoint url definition, if not set the model table name will be used :param Type[ModelSerializer] serializer_class: If `None`, a default serializer will be created. :param list|dict request_decorators: decorators to be applied to HTTP methods. Could be a list of decorators or a dict mapping HTTP method types to a list of decorators (dict keys should be 'get', 'post' or 'put'). See https://flask-restful.readthedocs.io/en/latest/extending.html#resource-method-decorators for more details. :param list|dict collection_decorators: decorators to be applied to HTTP methods for collections. It defaults to request_decorators value. :param preprocessors: A dict with the lists of callable preprocessors for each API method :param postprocessors: A dict with the lists of callable postprocessors for each API method """ restful = self.restful_api collection_name = collection_name or model.__tablename__ if not serializer_class: serializer = self.create_default_serializer(model) else: serializer = serializer_class(model) url = url or '/' + collection_name.lower() if not request_decorators: request_decorators = [] if not collection_decorators: collection_decorators = request_decorators _ItemResource = item_resource_factory( ItemResource, serializer, request_decorators, preprocessors, postprocessors ) _CollectionResource = collection_resource_factory( CollectionResource, serializer, collection_decorators, preprocessors, postprocessors ) restful.add_resource( _CollectionResource, url, endpoint=collection_name + '-list', resource_class_args=(model, serializer, self.get_db_session), ) restful.add_resource( _ItemResource, url + '/<id>', endpoint=collection_name, resource_class_args=(model, serializer, self.get_db_session) )
def add_relation(self, relation_property, url_rule=None, serializer_class=None, request_decorators=None, collection_decorators=None, endpoint_name=None, preprocessors=None, postprocessors=None): """ Create API endpoints for the given SQLAlchemy relationship. :param relation_property: model relationship representing the collection to receive the CRUD operations :param string url_rule: one or more url routes to match for the resource, standard flask routing rules apply. Defaults to model name in lower case. :param Type[ModelSerializer] serializer_class: If `None`, a default serializer will be created. :param list|dict request_decorators: decorators to be applied to HTTP methods. Could be a list of decorators or a dict mapping HTTP method types to a list of decorators (dict keys should be 'get', 'post' or 'put'). See https://flask-restful.readthedocs.io/en/latest/extending.html#resource-method-decorators for more details. :param list|dict collection_decorators: decorators to be applied to HTTP methods for collections. It defaults to request_decorators value. :param string endpoint_name: endpoint name (defaults to :meth:`{model_collection_name}-{related_collection_name}-relation` Can be used to reference this route in :class:`fields.Url` fields """ model = relation_property.prop.mapper.class_ related_model = relation_property.class_ related_collection_name = related_model.__tablename__.lower() if not serializer_class: serializer = self.create_default_serializer(model) else: serializer = serializer_class(model) if url_rule: assert '<relation_id>' in url_rule else: url_rule = '/{}/<relation_id>/{}'.format(related_collection_name, relation_property.key) endpoint_name = endpoint_name or url_rule if not request_decorators: request_decorators = [] if not collection_decorators: collection_decorators = request_decorators _ItemRelationResource = item_resource_factory( ItemRelationResource, serializer, request_decorators, preprocessors, postprocessors ) _CollectionRelationResource = collection_resource_factory( CollectionRelationResource, serializer, collection_decorators, preprocessors, postprocessors ) self._add_item_collection_resources( _ItemRelationResource, _CollectionRelationResource, url_rule, endpoint_name, resource_init_args=(relation_property, serializer, self.get_db_session), )