refacter DownloadView to use new inheritance structure, #116

This commit is contained in:
simon 2021-12-27 10:58:49 +07:00
parent 6229d92333
commit 2ae25540d1
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 45 additions and 80 deletions

View File

@ -58,9 +58,9 @@
<div class="dl-list {{ view_style }}"> <div class="dl-list {{ view_style }}">
{% if all_video_hits %} {% if all_video_hits %}
{% for video in all_video_hits %} {% for video in all_video_hits %}
<div class="dl-item {{ view_style }}" id="dl-{{ video.youtube_id }}"> <div class="dl-item {{ view_style }}" id="dl-{{ video.source.youtube_id }}">
<div class="dl-thumb {{ view_style }}"> <div class="dl-thumb {{ view_style }}">
<img src="/cache/{{ video.vid_thumb_url }}" alt="video_thumb"> <img src="/cache/{{ video.source.vid_thumb_url }}" alt="video_thumb">
{% if show_ignored_only %} {% if show_ignored_only %}
<span>ignored</span> <span>ignored</span>
{% else %} {% else %}
@ -68,19 +68,19 @@
{% endif %} {% endif %}
</div> </div>
<div class="dl-desc {{ view_style }}"> <div class="dl-desc {{ view_style }}">
<h3>{{ video.title }}</h3> <h3>{{ video.source.title }}</h3>
{% if video.channel_indexed %} {% if video.source.channel_indexed %}
<a href="{% url 'channel_id' video.channel_id %}">{{ video.channel_name }}</a> <a href="{% url 'channel_id' video.source.channel_id %}">{{ video.source.channel_name }}</a>
{% else %} {% else %}
<span>{{ video.channel_name }}</span> <span>{{ video.source.channel_name }}</span>
{% endif %} {% endif %}
<p>Published: {{ video.published }} | Duration: {{ video.duration }} | {{ video.youtube_id }}</p> <p>Published: {{ video.source.published }} | Duration: {{ video.source.duration }} | {{ video.source.youtube_id }}</p>
{% if show_ignored_only %} {% if show_ignored_only %}
<button data-id="{{ video.youtube_id }}" onclick="forgetIgnore(this)">Forget</button> <button data-id="{{ video.source.youtube_id }}" onclick="forgetIgnore(this)">Forget</button>
<button data-id="{{ video.youtube_id }}" onclick="addSingle(this)">Add to queue</button> <button data-id="{{ video.source.youtube_id }}" onclick="addSingle(this)">Add to queue</button>
{% else %} {% else %}
<button data-id="{{ video.youtube_id }}" onclick="toIgnore(this)">Ignore</button> <button data-id="{{ video.source.youtube_id }}" onclick="toIgnore(this)">Ignore</button>
<button id="{{ video.youtube_id }}" data-id="{{ video.youtube_id }}" onclick="downloadNow(this)">Download now</button> <button id="{{ video.source.youtube_id }}" data-id="{{ video.source.youtube_id }}" onclick="downloadNow(this)">Download now</button>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View File

@ -80,6 +80,12 @@ class ArchivistViewConfig(View):
return hide_watched return hide_watched
def _get_show_ignore_only(self):
ignored_key = f"{self.user_id}:show_ignored_only"
show_ignored_only = self.user_conf.get_message(ignored_key)["status"]
return show_ignored_only
def config_builder(self, user_id): def config_builder(self, user_id):
"""build default context for every view""" """build default context for every view"""
self.user_id = user_id self.user_id = user_id
@ -92,6 +98,7 @@ class ArchivistViewConfig(View):
context["sort_order"] = self._get_sort_order() context["sort_order"] = self._get_sort_order()
context["view_style"] = self._get_view_style() context["view_style"] = self._get_view_style()
context["hide_watched"] = self._get_hide_watched() context["hide_watched"] = self._get_hide_watched()
context["show_ignored_only"] = self._get_show_ignore_only()
self.context = context self.context = context
@ -158,6 +165,7 @@ class ArchivistResultsView(ArchivistViewConfig):
search = SearchHandler(url, self.data) search = SearchHandler(url, self.data)
videos_hits = search.get_data() videos_hits = search.get_data()
self.pagination_handler.validate(search.max_hits) self.pagination_handler.validate(search.max_hits)
self.context["max_hits"] = search.max_hits
return videos_hits return videos_hits
@ -267,88 +275,45 @@ class AboutView(View):
return render(request, "home/about.html", context) return render(request, "home/about.html", context)
class DownloadView(View): class DownloadView(ArchivistResultsView):
"""resolves to /download/ """resolves to /download/
takes POST for downloading youtube links takes POST for downloading youtube links
""" """
view_origin = "downloads"
es_search = "/ta_download/_search"
def get(self, request): def get(self, request):
"""handle get requests""" """handle get request"""
user_id = request.user.id user_id = request.user.id
view_config = self.read_config(user_id)
page_get = int(request.GET.get("page", 0)) page_get = int(request.GET.get("page", 0))
pagination_handler = Pagination(page_get, user_id)
url = view_config["es_url"] + "/ta_download/_search" self.config_builder(user_id)
data = self.build_data( self.initiate_vars(page_get)
pagination_handler, view_config["show_ignored_only"] self._update_view_data()
)
search = SearchHandler(url, data)
videos_hits = search.get_data() self.context.update(
max_hits = search.max_hits {
if videos_hits:
all_video_hits = [i["source"] for i in videos_hits]
pagination_handler.validate(max_hits)
pagination = pagination_handler.pagination
else:
all_video_hits = False
pagination = False
add_form = AddToQueueForm()
context = {
"add_form": add_form,
"all_video_hits": all_video_hits,
"max_hits": max_hits,
"pagination": pagination,
"title": "Downloads", "title": "Downloads",
"colors": view_config["colors"], "add_form": AddToQueueForm(),
"show_ignored_only": view_config["show_ignored_only"], "all_video_hits": self.find_video_hits(),
"view_style": view_config["view_style"], "pagination": self.pagination_handler.pagination,
} }
return render(request, "home/downloads.html", context) )
return render(request, "home/downloads.html", self.context)
@staticmethod def _update_view_data(self):
def read_config(user_id): """update downloads view specific data dict"""
"""read config vars""" if self.context["show_ignored_only"]:
config_handler = AppConfig(user_id)
view_key = f"{user_id}:view:downloads"
view_style = RedisArchivist().get_message(view_key)["status"]
if not view_style:
view_style = config_handler.config["default_view"]["downloads"]
ignored = RedisArchivist().get_message(f"{user_id}:show_ignored_only")
show_ignored_only = ignored["status"]
es_url = config_handler.config["application"]["es_url"]
view_config = {
"es_url": es_url,
"colors": config_handler.colors,
"view_style": view_style,
"show_ignored_only": show_ignored_only,
}
return view_config
@staticmethod
def build_data(pagination_handler, show_ignored_only):
"""build data dict for search"""
page_size = pagination_handler.pagination["page_size"]
page_from = pagination_handler.pagination["page_from"]
if show_ignored_only:
filter_view = "ignore" filter_view = "ignore"
else: else:
filter_view = "pending" filter_view = "pending"
self.data.update(
data = { {
"size": page_size,
"from": page_from,
"query": {"term": {"status": {"value": filter_view}}}, "query": {"term": {"status": {"value": filter_view}}},
"sort": [{"timestamp": {"order": "asc"}}], "sort": [{"timestamp": {"order": "asc"}}],
} }
return data )
@staticmethod @staticmethod
def post(request): def post(request):