From d086f63861b78b4f6a3309ce6a16c3ca015eb4ff Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 17 Apr 2022 20:10:49 +0700 Subject: [PATCH] API: sort and query filter download view, delete by filter --- tubearchivist/api/README.md | 9 ++++++++- tubearchivist/api/views.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 2067331..c635b11 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -136,7 +136,10 @@ POST /api/channel/ /api/playlist/\/video/ ## Download Queue List View -/api/download/ +GET /api/download/ + +Parameter: +- filter: pending, ignore ### Add list of videos to download queue POST /api/download/ @@ -148,6 +151,10 @@ POST /api/download/ } ``` +### Delete download queue items by filter +DELETE /api/download/?filter=ignore +DELETE /api/download/?filter=pending + ## Download Queue Item View GET /api/download/\/ POST /api/download/\/ diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index a610c56..a30484e 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -337,14 +337,28 @@ class DownloadApiListView(ApiBaseView): """resolves to /api/download/ GET: returns latest videos in the download queue POST: add a list of videos to download queue + DELETE: remove items based on query filter """ search_base = "ta_download/_search/" + valid_filter = ["pending", "ignore"] def get(self, request): # pylint: disable=unused-argument """get request""" - data = {"query": {"match_all": {}}} + query_filter = request.GET.get("filter", False) + data = { + "query": {"match_all": {}}, + "sort": [{"timestamp": {"order": "asc"}}], + } + if query_filter: + if query_filter not in self.valid_filter: + message = f"invalid url query filder: {query_filter}" + print(message) + return Response({"message": message}, status=400) + + data["query"] = {"term": {"status": {"value": query_filter}}} + self.get_document_list(data) self.get_paginate() return Response(self.response) @@ -374,6 +388,20 @@ class DownloadApiListView(ApiBaseView): return Response(data) + def delete(self, request): + """delete download queue""" + query_filter = request.GET.get("filter", False) + if query_filter not in self.valid_filter: + message = f"invalid url query filter: {query_filter}" + print(message) + return Response({"message": message}, status=400) + + message = f"delete queue by status: {query_filter}" + print(message) + PendingInteract(status=query_filter).delete_by_status() + + return Response({"message": message}) + class PingView(ApiBaseView): """resolves to /api/ping/