refactor channel and channel-id views inheritance, #116

This commit is contained in:
simon 2021-12-27 15:23:52 +07:00
parent fb512b26ea
commit 30cf54c49c
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
4 changed files with 81 additions and 178 deletions

View File

@ -51,8 +51,8 @@
</div> </div>
<h2>Total matching channels: {{ max_hits }}</h2> <h2>Total matching channels: {{ max_hits }}</h2>
<div class="channel-list {{ view_style }}"> <div class="channel-list {{ view_style }}">
{% if channels %} {% if results %}
{% for channel in channels %} {% for channel in results %}
<div class="channel-item {{ view_style }}"> <div class="channel-item {{ view_style }}">
<div class="channel-banner {{ view_style }}"> <div class="channel-banner {{ view_style }}">
<a href="{% url 'channel_id' channel.source.channel_id %}"> <a href="{% url 'channel_id' channel.source.channel_id %}">

View File

@ -110,8 +110,8 @@
</div> </div>
</div> </div>
<div class="video-list {{ view_style }}"> <div class="video-list {{ view_style }}">
{% if videos %} {% if results %}
{% for video in videos %} {% for video in results %}
<div class="video-item {{ view_style }}"> <div class="video-item {{ view_style }}">
<a href="#player" data-src="/media/{{ video.source.media_url }}" data-thumb="/cache/{{ video.source.vid_thumb_url }}" data-title="{{ video.source.title }}" data-channel="{{ video.source.channel.channel_name }}" data-channel-id="{{ video.source.channel.channel_id }}" data-id="{{ video.source.youtube_id }}" onclick="createPlayer(this)"> <a href="#player" data-src="/media/{{ video.source.media_url }}" data-thumb="/cache/{{ video.source.vid_thumb_url }}" data-title="{{ video.source.title }}" data-channel="{{ video.source.channel.channel_name }}" data-channel-id="{{ video.source.channel.channel_id }}" data-id="{{ video.source.youtube_id }}" onclick="createPlayer(this)">
<div class="video-thumb-wrap {{ view_style }}"> <div class="video-thumb-wrap {{ view_style }}">

View File

@ -37,7 +37,7 @@ urlpatterns = [
path("progress/", login_required(progress), name="progress"), path("progress/", login_required(progress), name="progress"),
path("channel/", login_required(ChannelView.as_view()), name="channel"), path("channel/", login_required(ChannelView.as_view()), name="channel"),
path( path(
"channel/<slug:channel_id_detail>/", "channel/<slug:channel_id>/",
login_required(ChannelIdView.as_view()), login_required(ChannelIdView.as_view()),
name="channel_id", name="channel_id",
), ),

View File

@ -86,21 +86,27 @@ class ArchivistViewConfig(View):
return show_ignored_only return show_ignored_only
def _get_show_subed_only(self):
sub_only_key = f"{self.user_id}:show_subed_only"
show_subed_only = self.user_conf.get_message(sub_only_key)["status"]
return show_subed_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
self.user_conf = RedisArchivist() self.user_conf = RedisArchivist()
self.default_conf = AppConfig().config self.default_conf = AppConfig().config
context = {} self.context = {
context["colors"] = self.default_conf["application"]["colors"] "colors": self.default_conf["application"]["colors"],
context["sort_by"] = self._get_sort_by() "sort_by": self._get_sort_by(),
context["sort_order"] = self._get_sort_order() "sort_order": self._get_sort_order(),
context["view_style"] = self._get_view_style() "view_style": self._get_view_style(),
context["hide_watched"] = self._get_hide_watched() "hide_watched": self._get_hide_watched(),
context["show_ignored_only"] = self._get_show_ignore_only() "show_ignored_only": self._get_show_ignore_only(),
"show_subed_only": self._get_show_subed_only(),
self.context = context }
class ArchivistResultsView(ArchivistViewConfig): class ArchivistResultsView(ArchivistViewConfig):
@ -335,201 +341,98 @@ class DownloadView(ArchivistResultsView):
return redirect("downloads", permanent=True) return redirect("downloads", permanent=True)
class ChannelIdView(View): class ChannelIdView(ArchivistResultsView):
"""resolves to /channel/<channel-id>/ """resolves to /channel/<channel-id>/
display single channel page from channel_id display single channel page from channel_id
""" """
def get(self, request, channel_id_detail): view_origin = "home"
"""get method""" es_search = "/ta_video/_search"
# es_url, colors, view_style = self.read_config()
view_config = self.read_config(user_id=request.user.id)
context = self.get_channel_videos(
request, channel_id_detail, view_config
)
context.update(view_config)
return render(request, "home/channel_id.html", context)
@staticmethod def get(self, request, channel_id):
def read_config(user_id): """get request"""
"""read config file""" user_id = request.user.id
config_handler = AppConfig(user_id)
config = config_handler.config
view_key = f"{user_id}:view:home"
view_style = RedisArchivist().get_message(view_key)["status"]
if not view_style:
view_style = config_handler.config["default_view"]["home"]
sort_by = RedisArchivist().get_message(f"{user_id}:sort_by")["status"]
if not sort_by:
sort_by = config["archive"]["sort_by"]
sort_order_key = f"{user_id}:sort_order"
sort_order = RedisArchivist().get_message(sort_order_key)["status"]
if not sort_order:
sort_order = config["archive"]["sort_order"]
hide_watched_key = f"{user_id}:hide_watched"
hide_watched = RedisArchivist().get_message(hide_watched_key)["status"]
view_config = {
"colors": config_handler.colors,
"es_url": config["application"]["es_url"],
"view_style": view_style,
"sort_by": sort_by,
"sort_order": sort_order,
"hide_watched": hide_watched,
}
return view_config
def get_channel_videos(self, request, channel_id_detail, view_config):
"""get channel from video index"""
page_get = int(request.GET.get("page", 0)) page_get = int(request.GET.get("page", 0))
pagination_handler = Pagination(page_get, request.user.id) self.initiate_vars(page_get, user_id)
# get data self._update_view_data(channel_id)
url = view_config["es_url"] + "/ta_video/_search" self.find_results()
data = self.build_data(
pagination_handler, channel_id_detail, view_config if self.context["results"]:
) channel_info = self.context["results"][0]["source"]["channel"]
search = SearchHandler(url, data)
videos_hits = search.get_data()
max_hits = search.max_hits
if max_hits:
channel_info = videos_hits[0]["source"]["channel"]
channel_name = channel_info["channel_name"] channel_name = channel_info["channel_name"]
pagination_handler.validate(max_hits)
pagination = pagination_handler.pagination
else: else:
# get details from channel index when when no hits channel_info, channel_name = self.get_channel_info(channel_id)
channel_info, channel_name = self.get_channel_info(
channel_id_detail, view_config["es_url"]
)
videos_hits = False
pagination = False
context = { self.context.update(
"channel_info": channel_info, {
"videos": videos_hits,
"max_hits": max_hits,
"pagination": pagination,
"title": "Channel: " + channel_name, "title": "Channel: " + channel_name,
"channel_info": channel_info,
} }
)
return context return render(request, "home/channel_id.html", self.context)
@staticmethod def _update_view_data(self, channel_id):
def build_data(pagination_handler, channel_id_detail, view_config): """update view specific data dict"""
"""build data dict for search""" query = {
sort_by = view_config["sort_by"]
sort_order = view_config["sort_order"]
# overwrite sort_by to match key
if sort_by == "views":
sort_by = "stats.view_count"
elif sort_by == "likes":
sort_by = "stats.like_count"
elif sort_by == "downloaded":
sort_by = "date_downloaded"
data = {
"size": pagination_handler.pagination["page_size"],
"from": pagination_handler.pagination["page_from"],
"query": {
"bool": { "bool": {
"must": [ "must": [
{ {"term": {"channel.channel_id": {"value": channel_id}}}
"term": {
"channel.channel_id": {
"value": channel_id_detail
}
}
}
] ]
} }
},
"sort": [{sort_by: {"order": sort_order}}],
} }
if view_config["hide_watched"]: self.data["query"] = query
if self.context["hide_watched"]:
to_append = {"term": {"player.watched": {"value": False}}} to_append = {"term": {"player.watched": {"value": False}}}
data["query"]["bool"]["must"].append(to_append) self.data["query"]["bool"]["must"].append(to_append)
return data def get_channel_info(self, channel_id):
"""fallback channel info if no videos found"""
@staticmethod es_url = self.default_conf["application"]["es_url"]
def get_channel_info(channel_id_detail, es_url): url = f"{es_url}/ta_channel/_doc/{channel_id}"
"""get channel info from channel index if no videos""" search = SearchHandler(url, data=False)
url = f"{es_url}/ta_channel/_doc/{channel_id_detail}"
data = False
search = SearchHandler(url, data)
channel_data = search.get_data() channel_data = search.get_data()
channel_info = channel_data[0]["source"] channel_info = channel_data[0]["source"]
channel_name = channel_info["channel_name"] channel_name = channel_info["channel_name"]
return channel_info, channel_name return channel_info, channel_name
class ChannelView(View): class ChannelView(ArchivistResultsView):
"""resolves to /channel/ """resolves to /channel/
handle functionality for channel overview page, subscribe to channel, handle functionality for channel overview page, subscribe to channel,
search as you type for channel name search as you type for channel name
""" """
view_origin = "channel"
es_search = "/ta_channel/_search"
def get(self, request): def get(self, request):
"""handle http get requests""" """handle get request"""
user_id = request.user.id user_id = request.user.id
view_config = self.read_config(user_id=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)
# get self.initiate_vars(page_get, user_id)
url = view_config["es_url"] + "/ta_channel/_search" self._update_view_data()
data = { self.find_results()
"size": pagination_handler.pagination["page_size"], self.context.update(
"from": pagination_handler.pagination["page_from"], {
"query": {"match_all": {}},
"sort": [{"channel_name.keyword": {"order": "asc"}}],
}
if view_config["show_subed_only"]:
data["query"] = {"term": {"channel_subscribed": {"value": True}}}
search = SearchHandler(url, data)
channel_hits = search.get_data()
pagination_handler.validate(search.max_hits)
search_form = ChannelSearchForm()
subscribe_form = SubscribeToChannelForm()
context = {
"search_form": search_form,
"subscribe_form": subscribe_form,
"channels": channel_hits,
"max_hits": search.max_hits,
"pagination": pagination_handler.pagination,
"show_subed_only": view_config["show_subed_only"],
"title": "Channels", "title": "Channels",
"colors": view_config["colors"], "search_form": ChannelSearchForm(),
"view_style": view_config["view_style"], "subscribe_form": SubscribeToChannelForm(),
} }
return render(request, "home/channel.html", context) )
@staticmethod return render(request, "home/channel.html", self.context)
def read_config(user_id):
"""read config file"""
config_handler = AppConfig(user_id)
view_key = f"{user_id}:view:channel"
view_style = RedisArchivist().get_message(view_key)["status"]
if not view_style:
view_style = config_handler.config["default_view"]["channel"]
sub_only_key = f"{user_id}:show_subed_only" def _update_view_data(self):
show_subed_only = RedisArchivist().get_message(sub_only_key)["status"] """update view data dict"""
self.data["sort"] = [{"channel_name.keyword": {"order": "asc"}}]
view_config = { if self.context["show_subed_only"]:
"es_url": config_handler.config["application"]["es_url"], self.data["query"] = {
"view_style": view_style, "term": {"channel_subscribed": {"value": True}}
"show_subed_only": show_subed_only,
"colors": config_handler.colors,
} }
return view_config
@staticmethod @staticmethod
def post(request): def post(request):
"""handle http post requests""" """handle http post requests"""