Skip to content

Commit

Permalink
Merge pull request #40 from saritasa-nest/feature/add-search-and-orde…
Browse files Browse the repository at this point in the history
…ring-fields-to-api

Add search and ordering to API views
  • Loading branch information
TheSuperiorStanislav committed Sep 11, 2024
2 parents 4eccbad + 917e916 commit 2256d62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ UNRELEASED
* Extend response of import job api
* Added support for django-import-export >= 4.0
* Removed support for django 3.2
* Add search and ordering to API views

0.5.0 (2023-12-19)
------------------
Expand Down
18 changes: 13 additions & 5 deletions import_export_extensions/api/views/export_job.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import contextlib
import typing

from rest_framework import (
decorators,
exceptions,
mixins,
permissions,
response,
status,
viewsets,
)
from rest_framework.exceptions import ValidationError
from rest_framework.request import Request

import django_filters
Expand Down Expand Up @@ -68,7 +69,7 @@ def start(self: "ExportJobViewSet", request: Request):
],
)(start)
# Correct specs of drf-spectacular if it is installed
try:
with contextlib.suppress(ImportError):
from drf_spectacular.utils import extend_schema, extend_schema_view

detail_serializer_class = viewset().get_detail_serializer_class()
Expand All @@ -87,8 +88,6 @@ def start(self: "ExportJobViewSet", request: Request):
},
),
)(viewset)
except ImportError:
pass
return viewset


Expand All @@ -113,6 +112,15 @@ class ExportJobViewSet(
serializer_class = serializers.ExportJobSerializer
resource_class: type[resources.CeleryModelResource] | None = None
filterset_class: django_filters.rest_framework.FilterSet = None
search_fields = ("id",)
ordering = (
"id",
)
ordering_fields = (
"id",
"created",
"modified",
)

def get_queryset(self):
"""Filter export jobs by resource used in viewset."""
Expand Down Expand Up @@ -154,7 +162,7 @@ def cancel(self, *args, **kwargs):
try:
job.cancel_export()
except ValueError as error:
raise ValidationError(error.args[0]) from error
raise exceptions.ValidationError(error.args[0]) from error

serializer = self.get_serializer(instance=job)
return response.Response(
Expand Down
20 changes: 14 additions & 6 deletions import_export_extensions/api/views/import_job.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import contextlib
import typing

from rest_framework import (
decorators,
exceptions,
mixins,
permissions,
response,
status,
viewsets,
)
from rest_framework.exceptions import ValidationError

from ... import models, resources
from .. import serializers
Expand Down Expand Up @@ -37,7 +38,7 @@ def __new__(cls, name, bases, attrs, **kwargs):
return viewset

# Correct specs of drf-spectacular if it is installed
try:
with contextlib.suppress(ImportError):
from drf_spectacular.utils import extend_schema, extend_schema_view

detail_serializer_class = viewset().get_detail_serializer_class()
Expand All @@ -61,8 +62,6 @@ def __new__(cls, name, bases, attrs, **kwargs):
},
),
)(viewset)
except ImportError:
pass
return viewset


Expand Down Expand Up @@ -91,6 +90,15 @@ class ImportJobViewSet(
queryset = models.ImportJob.objects.all()
serializer_class = serializers.ImportJobSerializer
resource_class: type[resources.CeleryModelResource] | None = None
search_fields = ("id",)
ordering = (
"id",
)
ordering_fields = (
"id",
"created",
"modified",
)

def get_queryset(self):
"""Filter import jobs by resource used in viewset."""
Expand Down Expand Up @@ -147,7 +155,7 @@ def confirm(self, *args, **kwargs):
try:
job.confirm_import()
except ValueError as error:
raise ValidationError(
raise exceptions.ValidationError(
f"Wrong import job status: {job.import_status}",
) from error

Expand All @@ -165,7 +173,7 @@ def cancel(self, *args, **kwargs):
try:
job.cancel_import()
except ValueError as error:
raise ValidationError(
raise exceptions.ValidationError(
f"Wrong import job status: {job.import_status}",
) from error

Expand Down

0 comments on commit 2256d62

Please sign in to comment.