From 46487d682ee3b349449f9d99d9eff4e7ed0ac7ee Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 16 Sep 2021 22:01:30 +0700 Subject: [PATCH] rewrite of DownloadView to allow pagination #7 --- .../home/templates/home/downloads.html | 2 +- tubearchivist/home/views.py | 38 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/tubearchivist/home/templates/home/downloads.html b/tubearchivist/home/templates/home/downloads.html index db6621e9..6d8f4919 100644 --- a/tubearchivist/home/templates/home/downloads.html +++ b/tubearchivist/home/templates/home/downloads.html @@ -29,7 +29,7 @@

Download queue

{% if pending %} -

Total pending downloads: {{ pending|length }}

+

Total pending downloads: {{ max_hits }}

{% for video in pending %}
diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index a781c9aa..c8f5fb22 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -148,20 +148,50 @@ class DownloadView(View): takes POST for downloading youtube links """ - @staticmethod - def get(request): + def get(self, request): """ handle get requests """ config = AppConfig().config colors = config['application']['colors'] - pending_handler = PendingList() - all_pending, _ = pending_handler.get_all_pending() + + page_get = int(request.GET.get('page', 0)) + pagination_handler = Pagination(page_get) + + url = config['application']['es_url'] + '/ta_download/_search' + data = self.build_data(pagination_handler) + search = SearchHandler(url, data, cache=False) + + videos_hits = search.get_data() + max_hits = search.max_hits + + if videos_hits: + all_pending = [i['source'] for i in videos_hits] + pagination_handler.validate(max_hits) + pagination = pagination_handler.pagination + else: + all_pending = False + pagination = False + context = { 'pending': all_pending, + 'max_hits': max_hits, + 'pagination': pagination, 'title': 'Downloads', 'colors': colors } return render(request, 'home/downloads.html', context) + @staticmethod + def build_data(pagination_handler): + """ build data dict for search """ + page_size = pagination_handler.pagination['page_size'] + page_from = pagination_handler.pagination['page_from'] + data = { + "size": page_size, "from": page_from, + "query": {"term": {"status": {"value": "pending"}}}, + "sort": [{"timestamp": {"order": "desc"}}] + } + return data + @staticmethod def post(request): """ handle post requests """