mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Better tasks api endpoint
This commit is contained in:
parent
aa999b34e2
commit
5c0829b052
@ -8,7 +8,7 @@ def init_paperless_tasks(apps, schema_editor):
|
||||
PaperlessTask = apps.get_model("documents", "PaperlessTask")
|
||||
Task = apps.get_model("django_q", "Task")
|
||||
|
||||
for task in Task.objects.all():
|
||||
for task in Task.objects.filter(func="documents.tasks.consume_file"):
|
||||
if not hasattr(task, "paperlesstask"):
|
||||
paperlesstask = PaperlessTask.objects.create(
|
||||
attempted_task=task,
|
||||
|
@ -598,12 +598,29 @@ class UiSettingsViewSerializer(serializers.ModelSerializer):
|
||||
return ui_settings
|
||||
|
||||
|
||||
class TasksViewSerializer(serializers.Serializer):
|
||||
class TasksViewSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = PaperlessTask
|
||||
depth = 1
|
||||
fields = "__all__"
|
||||
|
||||
type = serializers.ChoiceField(
|
||||
choices=["all", "incomplete", "complete", "failed"],
|
||||
default="all",
|
||||
)
|
||||
type = serializers.SerializerMethodField()
|
||||
|
||||
def get_type(self, obj):
|
||||
# just file tasks, for now
|
||||
return "file"
|
||||
|
||||
status = serializers.SerializerMethodField()
|
||||
|
||||
def get_status(self, obj):
|
||||
if obj.attempted_task is None:
|
||||
return "incomplete"
|
||||
elif obj.attempted_task.success:
|
||||
return "complete"
|
||||
elif not obj.attempted_task.success:
|
||||
return "failed"
|
||||
else:
|
||||
return "unknown"
|
||||
|
||||
|
||||
class AcknowledgeTasksViewSerializer(serializers.Serializer):
|
||||
|
@ -46,6 +46,7 @@ from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework.viewsets import ReadOnlyModelViewSet
|
||||
from rest_framework.viewsets import ViewSet
|
||||
|
||||
from .bulk_download import ArchiveOnlyStrategy
|
||||
@ -800,51 +801,18 @@ class UiSettingsView(GenericAPIView):
|
||||
)
|
||||
|
||||
|
||||
class TasksView(GenericAPIView):
|
||||
class TasksViewSet(ReadOnlyModelViewSet):
|
||||
|
||||
permission_classes = (IsAuthenticated,)
|
||||
serializer_class = TasksViewSerializer
|
||||
|
||||
def get(self, request, format=None):
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
tasks = (
|
||||
PaperlessTask.objects.filter(
|
||||
acknowledged=False,
|
||||
)
|
||||
.order_by("attempted_task__started")
|
||||
.reverse()
|
||||
)
|
||||
incomplete_tasks = tasks.filter(attempted_task=None).values(
|
||||
"id",
|
||||
"task_id",
|
||||
"name",
|
||||
"created",
|
||||
"acknowledged",
|
||||
)
|
||||
failed_tasks = tasks.filter(attempted_task__success=0).values(
|
||||
"id",
|
||||
"task_id",
|
||||
"name",
|
||||
"created",
|
||||
"acknowledged",
|
||||
)
|
||||
completed_tasks = tasks.filter(attempted_task__success=1).values(
|
||||
"id",
|
||||
"task_id",
|
||||
"name",
|
||||
"created",
|
||||
"acknowledged",
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"total": tasks.count(),
|
||||
"incomplete": incomplete_tasks,
|
||||
"failed": failed_tasks,
|
||||
"completed": completed_tasks,
|
||||
},
|
||||
queryset = (
|
||||
PaperlessTask.objects.filter(
|
||||
acknowledged=False,
|
||||
)
|
||||
.order_by("created")
|
||||
.reverse()
|
||||
)
|
||||
|
||||
|
||||
class AcknowledgeTasksView(GenericAPIView):
|
||||
@ -859,11 +827,9 @@ class AcknowledgeTasksView(GenericAPIView):
|
||||
tasks = serializer.validated_data.get("tasks")
|
||||
|
||||
try:
|
||||
logger.debug(tasks)
|
||||
result = PaperlessTask.objects.filter(id__in=tasks).update(
|
||||
acknowledged=True,
|
||||
)
|
||||
return Response({"result": result})
|
||||
pass
|
||||
except Exception as e:
|
||||
return HttpResponseBadRequest(str(e))
|
||||
except Exception:
|
||||
return HttpResponseBadRequest()
|
||||
|
@ -22,7 +22,7 @@ from documents.views import SelectionDataView
|
||||
from documents.views import StatisticsView
|
||||
from documents.views import StoragePathViewSet
|
||||
from documents.views import TagViewSet
|
||||
from documents.views import TasksView
|
||||
from documents.views import TasksViewSet
|
||||
from documents.views import UiSettingsView
|
||||
from documents.views import UnifiedSearchViewSet
|
||||
from paperless.consumers import StatusConsumer
|
||||
@ -38,6 +38,7 @@ api_router.register(r"logs", LogViewSet, basename="logs")
|
||||
api_router.register(r"tags", TagViewSet)
|
||||
api_router.register(r"saved_views", SavedViewViewSet)
|
||||
api_router.register(r"storage_paths", StoragePathViewSet)
|
||||
api_router.register(r"tasks", TasksViewSet, basename="tasks")
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
@ -88,11 +89,6 @@ urlpatterns = [
|
||||
UiSettingsView.as_view(),
|
||||
name="ui_settings",
|
||||
),
|
||||
re_path(
|
||||
r"^tasks/",
|
||||
TasksView.as_view(),
|
||||
name="tasks",
|
||||
),
|
||||
re_path(
|
||||
r"^acknowledge_tasks/",
|
||||
AcknowledgeTasksView.as_view(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user