class NestedResourceRepositoryViewSet(resource_repository_views.ResourceRepositoryViewSet):
    nested_schema = descriptors.NotImplementedProperty('nested_schema')
    nested_list_view_cls = NestedResourceRepositoryListView

    def __init__(self, *, nested_schema=None, nested_list_view_cls=None, **kwargs):
        super().__init__(**kwargs)
        if nested_schema:
            self.nested = nested_schema
        if nested_list_view_cls:
            self.list_view_cls = nested_list_view_cls
        self.repository = self.extend_repository()

    def extend_repository(self):
        return nested_repository.NestedRepository(repository=self.repository)

    def as_list_view(self, view_name):
        return self.decorate(
            self.nested_list_view_cls.as_view(view_name, filter_schema=self.filter_schema,
                                              **self.get_list_view_kwargs())
        )

    def get_list_view_kwargs(self):
        kwargs = self.get_views_kwargs()
        kwargs.update({
            'nested_schema': self.nested_schema,
        })
        return kwargs
Exemple #2
0
class ProtectedViewSet(ResourceRepositoryViewSet):
    detail_view_cls = ProtectedDetailView
    list_view_cls: ProtectedListView
    permission_checker: checkers.PermissionChecker = descriptors.NotImplementedProperty('permission_checker')

    def get_views_kwargs(self):
        kwargs = super().get_views_kwargs()
        kwargs['permission_checker'] = self.permission_checker
        return kwargs
class ResourceRepositoryViewSet:
    repository = repositories.ResourceRepository()
    schema = descriptors.NotImplementedProperty('schema')
    filter_schema = filters_schema.FilterSchema()
    detail_view_cls = ResourceRepositoryDetailView
    list_view_cls = ResourceRepositoryListView
    view_decorators = ()
    view_kwargs = None

    def __init__(self,
                 *,
                 repository=None,
                 schema=None,
                 filter_schema=None,
                 detail_view_cls=None,
                 list_view_cls=None,
                 view_decorators=None,
                 view_kwargs=None):
        if repository:
            self.repository = repository
        if schema:
            self.schema = schema
        if filter_schema:
            self.filter_schema = filter_schema
        if detail_view_cls:
            self.detail_view_cls = detail_view_cls
        if list_view_cls:
            self.list_view_cls = list_view_cls
        if view_decorators:
            self.view_decorators = view_decorators
        if view_kwargs:
            self.view_kwargs = view_kwargs

    def as_detail_view(self, view_name):
        return self.decorate(
            self.detail_view_cls.as_view(view_name, **self.get_views_kwargs()))

    def as_list_view(self, view_name):
        return self.decorate(
            self.list_view_cls.as_view(view_name,
                                       filter_schema=self.filter_schema,
                                       **self.get_views_kwargs()))

    def decorate(self, view):
        for decorator in self.view_decorators:
            view = decorator(view)
        return view

    def get_views_kwargs(self):
        return {
            'schema': self.schema,
            'repository': self.repository,
            **(self.view_kwargs or {})
        }
Exemple #4
0
class ResourceBase(views.View):
    schema = descriptors.NotImplementedProperty('schema')
    args = None
    kwargs = None

    def __init__(self, *, schema=None):
        if schema:
            self.schema = schema

    @classmethod
    def as_view(cls, name, *class_args, **class_kwargs):
        view = super().as_view(name, *class_args, **class_kwargs)
        return decorators.check_headers(view)

    def dispatch_request(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        handler = getattr(self, request.method.lower(),
                          self._http_method_not_allowed)
        try:
            response_object = handler(request, *args, **kwargs)
        except exceptions.JsonApiException as e:
            return response.JsonApiErrorResponse(
                e.to_dict(), status=e.status).make_response()
        else:
            return response_object.make_response()

    def _http_method_not_allowed(self, request, *args, **kwargs):
        logger.error('Method Not Allowed (%s): %s',
                     request.method,
                     request.path,
                     extra={
                         'status_code': http.HTTPStatus.METHOD_NOT_ALLOWED,
                         'request': request
                     })
        return helpers.make_response('Method Not Allowed.',
                                     http.HTTPStatus.METHOD_NOT_ALLOWED)

    def _check_include_fields(self):
        include_parameter = flask.request.args.get('include')
        if include_parameter:
            include_fields = tuple(include_parameter.split(','))
            try:
                self.schema().check_relations(include_fields)
            except ValueError as exc:
                raise exceptions.InvalidInclude(detail=str(exc))
            return include_fields
        else:
            return tuple()
class ResourceBase(views.View):
    schema = descriptors.NotImplementedProperty('schema')
    args = None
    kwargs = None

    def __init__(self, *, schema=None):
        if schema:
            self.schema = schema
        self.sort_parser = query_string.SortParser(schema=self.schema)
        self.include_parser = query_string.IncludeParser(schema=self.schema)
        self.sparse_fields_parser = query_string.SparseFieldsParser(
            schema=self.schema)

    @classmethod
    def as_view(cls, name, *class_args, **class_kwargs):
        view = super().as_view(name, *class_args, **class_kwargs)
        return decorators.check_headers(view)

    def dispatch_request(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        handler = getattr(self, request.method.lower(),
                          self._http_method_not_allowed)
        try:
            response_object = handler(request, *args, **kwargs)
        except exceptions.JsonApiException as e:
            return response.JsonApiErrorResponse(
                e.to_dict(), status=e.status).make_response()
        else:
            return response_object.make_response()

    def _http_method_not_allowed(self, request, *args, **kwargs):
        logger.error('Method Not Allowed (%s): %s',
                     request.method,
                     request.path,
                     extra={
                         'status_code': http.HTTPStatus.METHOD_NOT_ALLOWED,
                         'request': request
                     })
        return helpers.make_response('Method Not Allowed.',
                                     http.HTTPStatus.METHOD_NOT_ALLOWED)