rewrite of DownloadView to allow pagination #7

This commit is contained in:
simon 2021-09-16 22:01:30 +07:00
parent 9b493ae9b0
commit 46487d682e
2 changed files with 35 additions and 5 deletions

View File

@ -29,7 +29,7 @@
<h2>Download queue</h2> <h2>Download queue</h2>
<div> <div>
{% if pending %} {% if pending %}
<h3>Total pending downloads: {{ pending|length }}</h3> <h3>Total pending downloads: {{ max_hits }}</h3>
{% for video in pending %} {% for video in pending %}
<div class="dl-item" id="dl-{{ video.youtube_id }}"> <div class="dl-item" id="dl-{{ video.youtube_id }}">
<div class="dl-thumb"> <div class="dl-thumb">

View File

@ -148,20 +148,50 @@ class DownloadView(View):
takes POST for downloading youtube links takes POST for downloading youtube links
""" """
@staticmethod def get(self, request):
def get(request):
""" handle get requests """ """ handle get requests """
config = AppConfig().config config = AppConfig().config
colors = config['application']['colors'] 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 = { context = {
'pending': all_pending, 'pending': all_pending,
'max_hits': max_hits,
'pagination': pagination,
'title': 'Downloads', 'title': 'Downloads',
'colors': colors 'colors': colors
} }
return render(request, 'home/downloads.html', context) 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 @staticmethod
def post(request): def post(request):
""" handle post requests """ """ handle post requests """