backend bulk delete filter

This commit is contained in:
Simon 2025-07-06 16:36:16 +07:00
parent 8d9cb9261e
commit a0f40d9970
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 24 additions and 3 deletions

View File

@ -53,6 +53,10 @@ class DownloadListQueueDeleteQuerySerializer(serializers.Serializer):
"""serialize bulk delete download queue query string"""
filter = serializers.ChoiceField(choices=["pending", "ignore"])
channel = serializers.CharField(required=False, help_text="channel ID")
vid_type = serializers.ChoiceField(
choices=VideoTypeEnum.values_known(), required=False
)
class AddDownloadItemSerializer(serializers.Serializer):

View File

@ -100,9 +100,17 @@ class PendingInteract:
path = f"ta_download/_doc/{self.youtube_id}"
_, _ = ElasticWrap(path).delete(refresh=True)
def delete_by_status(self):
def delete_bulk(self, channel_id: str | None, vid_type: str | None):
"""delete all matching item by status"""
data = {"query": {"term": {"status": {"value": self.status}}}}
must_list = [{"term": {"status": {"value": self.status}}}]
if channel_id:
must_list.append({"term": {"channel_id": {"value": channel_id}}})
if vid_type:
must_list.append({"term": {"vid_type": {"value": vid_type}}})
data = {"query": {"bool": {"must": must_list}}}
path = "ta_download/_delete_by_query"
_, _ = ElasticWrap(path).post(data=data)

View File

@ -138,9 +138,18 @@ class DownloadApiListView(ApiBaseView):
validated_query = serializer.validated_data
query_filter = validated_query["filter"]
channel = validated_query.get("channel")
vid_type = validated_query.get("vid_type")
message = f"delete queue by status: {query_filter}"
if channel:
message += f" - filter by channel: {channel}"
if vid_type:
message += f" - filter by vid_type: {vid_type}"
print(message)
PendingInteract(status=query_filter).delete_by_status()
PendingInteract(status=query_filter).delete_bulk(
channel_id=channel, vid_type=vid_type
)
return Response(status=204)