consolidate min var initiate for simple views

This commit is contained in:
simon 2022-12-21 17:44:28 +07:00
parent 5244cddeb3
commit ae6bb4e757
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
1 changed files with 53 additions and 50 deletions

View File

@ -257,6 +257,18 @@ class ArchivistResultsView(ArchivistViewConfig):
self.context["pagination"] = self.pagination_handler.pagination self.context["pagination"] = self.pagination_handler.pagination
class MinView(View):
"""to inherit from for minimal config vars"""
@staticmethod
def get_min_context(request):
"""build minimal vars for context"""
return {
"colors": AppConfig(request.user.id).colors,
"version": settings.TA_VERSION,
}
class HomeView(ArchivistResultsView): class HomeView(ArchivistResultsView):
"""resolves to / """resolves to /
handle home page and video search post functionality handle home page and video search post functionality
@ -298,20 +310,23 @@ class HomeView(ArchivistResultsView):
self.data["query"] = query self.data["query"] = query
class LoginView(View): class LoginView(MinView):
"""resolves to /login/ """resolves to /login/
Greeting and login page Greeting and login page
""" """
SEC_IN_DAY = 60 * 60 * 24 SEC_IN_DAY = 60 * 60 * 24
@staticmethod def get(self, request):
def get(request):
"""handle get requests""" """handle get requests"""
failed = bool(request.GET.get("failed")) context = self.get_min_context(request)
colors = AppConfig(request.user.id).colors context.update(
form = CustomAuthForm() {
context = {"colors": colors, "form": form, "form_error": failed} "form": CustomAuthForm(),
"form_error": bool(request.GET.get("failed")),
}
)
return render(request, "home/login.html", context) return render(request, "home/login.html", context)
def post(self, request): def post(self, request):
@ -333,19 +348,15 @@ class LoginView(View):
return redirect("/login?failed=true") return redirect("/login?failed=true")
class AboutView(View): class AboutView(MinView):
"""resolves to /about/ """resolves to /about/
show helpful how to information show helpful how to information
""" """
@staticmethod def get(self, request):
def get(request):
"""handle http get""" """handle http get"""
context = { context = self.get_min_context(request)
"title": "About", context.update({"title": "About"})
"colors": AppConfig(request.user.id).colors,
"version": settings.TA_VERSION,
}
return render(request, "home/about.html", context) return render(request, "home/about.html", context)
@ -843,7 +854,7 @@ class PlaylistView(ArchivistResultsView):
return redirect("playlist") return redirect("playlist")
class VideoView(View): class VideoView(MinView):
"""resolves to /video/<video-id>/ """resolves to /video/<video-id>/
display details about a single video display details about a single video
""" """
@ -869,17 +880,18 @@ class VideoView(View):
request_type="video", request_id=video_id request_type="video", request_id=video_id
).get_progress() ).get_progress()
context = { context = self.get_min_context(request)
"video": video_data, context.update(
"playlist_nav": playlist_nav, {
"title": video_data.get("title"), "video": video_data,
"colors": config_handler.colors, "playlist_nav": playlist_nav,
"cast": config_handler.config["application"]["enable_cast"], "title": video_data.get("title"),
"version": settings.TA_VERSION, "cast": config_handler.config["application"]["enable_cast"],
"config": config_handler.config, "config": config_handler.config,
"position": time_parser(request.GET.get("t")), "position": time_parser(request.GET.get("t")),
"reindex": reindex.get("state"), "reindex": reindex.get("state"),
} }
)
return render(request, "home/video.html", context) return render(request, "home/video.html", context)
@staticmethod @staticmethod
@ -936,7 +948,7 @@ class SearchView(ArchivistResultsView):
return render(request, "home/search.html", self.context) return render(request, "home/search.html", self.context)
class SettingsView(View): class SettingsView(MinView):
"""resolves to /settings/ """resolves to /settings/
handle the settings page, display current settings, handle the settings page, display current settings,
take post request from the form to update settings take post request from the form to update settings
@ -944,28 +956,19 @@ class SettingsView(View):
def get(self, request): def get(self, request):
"""read and display current settings""" """read and display current settings"""
config_handler = AppConfig(request.user.id) context = self.get_min_context(request)
colors = config_handler.colors context.update(
{
available_backups = ElasticBackup().get_all_backup_files() "title": "Settings",
user_form = UserSettingsForm() "config": AppConfig(request.user.id).config,
app_form = ApplicationSettingsForm() "api_token": self.get_token(request),
scheduler_form = SchedulerSettingsForm() "available_backups": ElasticBackup().get_all_backup_files(),
snapshots = ElasticSnapshot().get_snapshot_stats() "user_form": UserSettingsForm(),
token = self.get_token(request) "app_form": ApplicationSettingsForm(),
"scheduler_form": SchedulerSettingsForm(),
context = { "snapshots": ElasticSnapshot().get_snapshot_stats(),
"title": "Settings", }
"config": config_handler.config, )
"api_token": token,
"colors": colors,
"available_backups": available_backups,
"user_form": user_form,
"app_form": app_form,
"scheduler_form": scheduler_form,
"snapshots": snapshots,
"version": settings.TA_VERSION,
}
return render(request, "home/settings.html", context) return render(request, "home/settings.html", context)