Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle failed API requests on schema page #3323

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 55 additions & 35 deletions mathesar/api/db/viewsets/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,41 @@
from rest_access_policy import AccessViewSetMixin
pavish marked this conversation as resolved.
Show resolved Hide resolved

from rest_framework import viewsets
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin
from rest_framework.mixins import (
ListModelMixin,
RetrieveModelMixin,
CreateModelMixin,
UpdateModelMixin,
DestroyModelMixin,
)
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.decorators import action

from mathesar.api.db.permissions.query import QueryAccessPolicy
from mathesar.api.dj_filters import UIQueryFilter

from mathesar.api.exceptions.query_exceptions.exceptions import DeletedColumnAccess, DeletedColumnAccessAPIException
from mathesar.api.pagination import DefaultLimitOffsetPagination, TableLimitOffsetPagination
from mathesar.api.exceptions.query_exceptions.exceptions import (
DeletedColumnAccess,
DeletedColumnAccessAPIException,
)
from mathesar.api.pagination import (
DefaultLimitOffsetPagination,
TableLimitOffsetPagination,
)
from mathesar.api.serializers.queries import BaseQuerySerializer, QuerySerializer
from mathesar.api.serializers.records import RecordListParameterSerializer
from mathesar.models.query import UIQuery


class QueryViewSet(
AccessViewSetMixin,
CreateModelMixin,
UpdateModelMixin,
RetrieveModelMixin,
ListModelMixin,
DestroyModelMixin,
viewsets.GenericViewSet
AccessViewSetMixin,
CreateModelMixin,
UpdateModelMixin,
RetrieveModelMixin,
ListModelMixin,
DestroyModelMixin,
viewsets.GenericViewSet,
):
serializer_class = QuerySerializer
pagination_class = DefaultLimitOffsetPagination
Expand All @@ -35,11 +47,13 @@ class QueryViewSet(
access_policy = QueryAccessPolicy

def get_queryset(self):
print("i was callled")
queryset = self._get_scoped_queryset()
schema_id = self.request.query_params.get('schema')
schema_id = self.request.query_params.get("schema")
if schema_id:
queryset = queryset.filter(base_table__schema=schema_id)
return queryset.order_by('-created_at')
raise Exception("i was called")
return queryset.order_by("-created_at")

def _get_scoped_queryset(self):
"""
Expand All @@ -50,18 +64,19 @@ def _get_scoped_queryset(self):
`QueryAccessPolicy.get_should_queryset_be_unscoped` docstring for more
information.
"""
should_queryset_be_scoped = \
should_queryset_be_scoped = (
not QueryAccessPolicy.get_should_queryset_be_unscoped(self.action)
)
if should_queryset_be_scoped:
queryset = self.access_policy.scope_queryset(
self.request,
UIQuery.objects.all()
self.request, UIQuery.objects.all()
)
else:
queryset = UIQuery.objects.all()
raise Exception("i was called")
return queryset

@action(methods=['get'], detail=True)
@action(methods=["get"], detail=True)
def records(self, request, pk=None):
paginator = TableLimitOffsetPagination()
query = self.get_object()
Expand All @@ -71,22 +86,24 @@ def records(self, request, pk=None):
queryset=self.get_queryset(),
request=request,
table=query,
filters=serializer.validated_data['filter'],
order_by=serializer.validated_data['order_by'],
grouping=serializer.validated_data['grouping'],
search=serializer.validated_data['search_fuzzy'],
duplicate_only=serializer.validated_data['duplicate_only'],
filters=serializer.validated_data["filter"],
order_by=serializer.validated_data["order_by"],
grouping=serializer.validated_data["grouping"],
search=serializer.validated_data["search_fuzzy"],
duplicate_only=serializer.validated_data["duplicate_only"],
)
print("executing")
return paginator.get_paginated_response(records)

@action(methods=['get'], detail=True)
@action(methods=["get"], detail=True)
def columns(self, request, pk=None):
query = self.get_object()
output_col_desc = query.output_columns_described
return Response(output_col_desc)

@action(methods=['get'], detail=True)
@action(methods=["get"], detail=True)
def results(self, request, pk=None):
print("i am special")
paginator = TableLimitOffsetPagination()
query = self.get_object()
serializer = RecordListParameterSerializer(data=request.GET)
Expand All @@ -95,11 +112,11 @@ def results(self, request, pk=None):
queryset=self.get_queryset(),
request=request,
table=query,
filters=serializer.validated_data['filter'],
order_by=serializer.validated_data['order_by'],
grouping=serializer.validated_data['grouping'],
search=serializer.validated_data['search_fuzzy'],
duplicate_only=serializer.validated_data['duplicate_only'],
filters=serializer.validated_data["filter"],
order_by=serializer.validated_data["order_by"],
grouping=serializer.validated_data["grouping"],
search=serializer.validated_data["search_fuzzy"],
duplicate_only=serializer.validated_data["duplicate_only"],
)
paginated_records = paginator.get_paginated_response(records)
columns = query.output_columns_simple
Expand All @@ -112,12 +129,14 @@ def results(self, request, pk=None):
}
)

@action(methods=['post'], detail=False)
@action(methods=["post"], detail=False)
def run(self, request):
params = request.data.pop("parameters", {})
request.GET |= {k: [json.dumps(v)] for k, v in params.items()}
paginator = TableLimitOffsetPagination()
input_serializer = BaseQuerySerializer(data=request.data, context={'request': request})
input_serializer = BaseQuerySerializer(
data=request.data, context={"request": request}
)
input_serializer.is_valid(raise_exception=True)
query = UIQuery(**input_serializer.validated_data)
try:
Expand All @@ -130,11 +149,11 @@ def run(self, request):
queryset=self.get_queryset(),
request=request,
table=query,
filters=record_serializer.validated_data['filter'],
order_by=record_serializer.validated_data['order_by'],
grouping=record_serializer.validated_data['grouping'],
search=record_serializer.validated_data['search_fuzzy'],
duplicate_only=record_serializer.validated_data['duplicate_only'],
filters=record_serializer.validated_data["filter"],
order_by=record_serializer.validated_data["order_by"],
grouping=record_serializer.validated_data["grouping"],
search=record_serializer.validated_data["search_fuzzy"],
duplicate_only=record_serializer.validated_data["duplicate_only"],
)
paginated_records = paginator.get_paginated_response(records)
except DeletedColumnAccess as e:
Expand All @@ -149,6 +168,7 @@ def _get_param_val(val):
except json.JSONDecodeError:
ret_val = val
return ret_val

return Response(
{
"query": output_serializer.data,
Expand Down
Loading
Loading