From 94c4fb650b076005feb66a714dd810504a615cb4 Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 20 Nov 2024 21:27:56 +0100 Subject: [PATCH] fix: Refactors views to use static for script paths Changes hardcoded script URL paths to use Django's static function for better resource management and maintainability. Improves code readability by breaking lines, adopting PEP 8 style guidelines. Simplifies model imports and uses consistent string formatting for various UI elements. --- dreams/views.py | 102 ++++++++++++++++++++++++++++++++++++++---------- mood/views.py | 33 ++++++++-------- 2 files changed, 97 insertions(+), 38 deletions(-) diff --git a/dreams/views.py b/dreams/views.py index d87ccaf..48bbddf 100644 --- a/dreams/views.py +++ b/dreams/views.py @@ -1,9 +1,15 @@ - -from django.views.generic import ListView, UpdateView, DetailView, CreateView, DeleteView +from django.views.generic import ( + ListView, + UpdateView, + DetailView, + CreateView, + DeleteView, +) from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import get_object_or_404 from django.urls import reverse_lazy from django.http import HttpResponseRedirect +from django.templatetags.static import static from .models import Dream, DreamTheme, DreamMedia, Theme from .forms import DreamForm @@ -11,6 +17,7 @@ from .forms import DreamForm from common.helpers import get_upload_path from msgio.models import NotificationDailySchedule, Notification + class DreamListView(LoginRequiredMixin, ListView): template_name = "dreams/dream_list.html" model = Dream @@ -19,11 +26,13 @@ class DreamListView(LoginRequiredMixin, ListView): context = super().get_context_data(**kwargs) context["title"] = "Dream List" context["subtitle"] = "A list of the dreams you have entered so far." - context["buttons"] = [(reverse_lazy("dreams:dream_create"), "New Dream", "plus")] + context["buttons"] = [ + (reverse_lazy("dreams:dream_create"), "New Dream", "plus") + ] return context def get_queryset(self): - return Dream.objects.filter(user=self.request.user).order_by('timestamp') + return Dream.objects.filter(user=self.request.user).order_by("timestamp") class DreamViewView(LoginRequiredMixin, DetailView): @@ -34,7 +43,13 @@ class DreamViewView(LoginRequiredMixin, DetailView): context = super().get_context_data(**kwargs) context["title"] = "View Dream" context["subtitle"] = "View the details of your dream." - context["buttons"] = [(reverse_lazy("dreams:dream_edit", kwargs={"id": self.kwargs["id"]}), "Edit Dream", "pen")] + context["buttons"] = [ + ( + reverse_lazy("dreams:dream_edit", kwargs={"id": self.kwargs["id"]}), + "Edit Dream", + "pen", + ) + ] return context def get_object(self): @@ -50,7 +65,7 @@ class DreamCreateView(LoginRequiredMixin, CreateView): context = super().get_context_data(**kwargs) context["title"] = "Create Dream" context["subtitle"] = "What did you dream?" - context["scripts"] = ["frontend/js/dropdown-to-buttons.js"] + context["scripts"] = [static("frontend/js/dropdown-to-buttons.js")] return context def form_valid(self, form): @@ -82,8 +97,14 @@ class DreamEditView(LoginRequiredMixin, UpdateView): context = super().get_context_data(**kwargs) context["title"] = "Update Dream" context["subtitle"] = "Change details of a dream you entered before." - context["scripts"] = ["frontend/js/dropdown-to-buttons.js"] - context["buttons"] = [(reverse_lazy("dreams:dream_delete", kwargs={"id": self.kwargs["id"]}), "Delete Dream", "trash-alt")] + context["scripts"] = [static("frontend/js/dropdown-to-buttons.js")] + context["buttons"] = [ + ( + reverse_lazy("dreams:dream_delete", kwargs={"id": self.kwargs["id"]}), + "Delete Dream", + "trash-alt", + ) + ] return context def get_object(self): @@ -129,7 +150,9 @@ class ThemeListView(LoginRequiredMixin, ListView): context = super().get_context_data(**kwargs) context["title"] = "Themes" context["subtitle"] = "The themes you have defined for your dreams." - context["buttons"] = [(reverse_lazy("dreams:theme_create"), "Create Theme", "pen")] + context["buttons"] = [ + (reverse_lazy("dreams:theme_create"), "Create Theme", "pen") + ] return context def get_queryset(self): @@ -145,9 +168,20 @@ class ThemeEditView(LoginRequiredMixin, UpdateView): context = super().get_context_data(**kwargs) context["title"] = "Edit Theme" context["subtitle"] = "Make changes to the theme." - context["scripts"] = ["colorfield/jscolor/jscolor.js", "colorfield/colorfield.js", "frontend/js/fontawesome-iconpicker.min.js", "frontend/js/iconpicker-loader.js"] - context["styles"] = ["frontend/css/fontawesome-iconpicker.min.css"] - context["buttons"] = [(reverse_lazy("dreams:theme_delete", kwargs={"id": self.kwargs["id"]}), "Delete Theme", "trash-alt")] + context["scripts"] = [ + static("colorfield/jscolor/jscolor.js"), + static("colorfield/colorfield.js"), + static("frontend/js/fontawesome-iconpicker.min.js"), + static("frontend/js/iconpicker-loader.js"), + ] + context["styles"] = [static("frontend/css/fontawesome-iconpicker.min.css")] + context["buttons"] = [ + ( + reverse_lazy("dreams:theme_delete", kwargs={"id": self.kwargs["id"]}), + "Delete Theme", + "trash-alt", + ) + ] return context def get_object(self): @@ -166,8 +200,13 @@ class ThemeCreateView(LoginRequiredMixin, CreateView): context = super().get_context_data(**kwargs) context["title"] = "Create Theme" context["subtitle"] = "Add a new theme for your dreams." - context["scripts"] = ["colorfield/jscolor/jscolor.js", "colorfield/colorfield.js", "frontend/js/fontawesome-iconpicker.min.js", "frontend/js/iconpicker-loader.js"] - context["styles"] = ["frontend/css/fontawesome-iconpicker.min.css"] + context["scripts"] = [ + static("colorfield/jscolor/jscolor.js"), + static("colorfield/colorfield.js"), + static("frontend/js/fontawesome-iconpicker.min.js"), + static("frontend/js/iconpicker-loader.js"), + ] + context["styles"] = [static("frontend/css/fontawesome-iconpicker.min.css")] return context def form_valid(self, form): @@ -199,11 +238,15 @@ class NotificationListView(LoginRequiredMixin, ListView): context = super().get_context_data(**kwargs) context["title"] = "Notifications" context["subtitle"] = "The daily reminders you have set up." - context["buttons"] = [(reverse_lazy("dreams:notification_create"), "New Notification", "plus")] + context["buttons"] = [ + (reverse_lazy("dreams:notification_create"), "New Notification", "plus") + ] return context def get_queryset(self): - return NotificationDailySchedule.objects.filter(notification__recipient=self.request.user, notification__app="dreams") + return NotificationDailySchedule.objects.filter( + notification__recipient=self.request.user, notification__app="dreams" + ) class NotificationCreateView(LoginRequiredMixin, CreateView): @@ -218,7 +261,11 @@ class NotificationCreateView(LoginRequiredMixin, CreateView): return context def form_valid(self, form): - notification = Notification.objects.create(content="What did you dream tonight? Go to %KUMIFYURL% to document your dreams!", recipient=self.request.user, app="dreams") + notification = Notification.objects.create( + content="What did you dream tonight? Go to %KUMIFYURL% to document your dreams!", + recipient=self.request.user, + app="dreams", + ) obj = form.save(commit=False) obj.notification = notification return super().form_valid(form) @@ -236,14 +283,23 @@ class NotificationEditView(LoginRequiredMixin, UpdateView): context = super().get_context_data(**kwargs) context["title"] = "Edit Notification" context["subtitle"] = "Change the time of a daily notification." - context["buttons"] = [(reverse_lazy("dreams:notification_delete", args=[self.kwargs["id"]]), "Delete Notification")] + context["buttons"] = [ + ( + reverse_lazy("dreams:notification_delete", args=[self.kwargs["id"]]), + "Delete Notification", + ) + ] return context def get_success_url(self): return reverse_lazy("dreams:notification_list") def get_object(self): - return get_object_or_404(NotificationDailySchedule, notification__recipient=self.request.user, id=self.kwargs["id"]) + return get_object_or_404( + NotificationDailySchedule, + notification__recipient=self.request.user, + id=self.kwargs["id"], + ) class NotificationDeleteView(LoginRequiredMixin, DeleteView): @@ -251,7 +307,11 @@ class NotificationDeleteView(LoginRequiredMixin, DeleteView): model = NotificationDailySchedule def get_object(self): - return get_object_or_404(NotificationDailySchedule, notification__recipient=self.request.user, id=self.kwargs["id"]) + return get_object_or_404( + NotificationDailySchedule, + notification__recipient=self.request.user, + id=self.kwargs["id"], + ) def delete(self, request, *args, **kwargs): self.object = self.get_object() @@ -260,4 +320,4 @@ class NotificationDeleteView(LoginRequiredMixin, DeleteView): return HttpResponseRedirect(success_url) def get_success_url(self): - return reverse_lazy("dreams:notification_list") \ No newline at end of file + return reverse_lazy("dreams:notification_list") diff --git a/mood/views.py b/mood/views.py index 3c64ae5..d72e26f 100644 --- a/mood/views.py +++ b/mood/views.py @@ -14,8 +14,7 @@ from django.http import HttpResponseRedirect, HttpResponse from django.utils import timezone from django.views.decorators.clickjacking import xframe_options_sameorigin from django.utils.decorators import method_decorator -from django.db.models import Count -from django.db.models.functions import TruncDate +from django.contrib.staticfiles.templatetags.staticfiles import static from .models import Status, Activity, Mood, StatusMedia, StatusActivity from .forms import StatusForm @@ -185,12 +184,12 @@ class ActivityEditView(LoginRequiredMixin, UpdateView): context["title"] = "Edit Activity" context["subtitle"] = "Make changes to the activity." context["scripts"] = [ - "colorfield/jscolor/jscolor.js", - "colorfield/colorfield.js", - "frontend/js/fontawesome-iconpicker.min.js", - "frontend/js/iconpicker-loader.js", + static("colorfield/jscolor/jscolor.js"), + static("colorfield/colorfield.js"), + static("frontend/js/fontawesome-iconpicker.min.js"), + static("frontend/js/iconpicker-loader.js"), ] - context["styles"] = ["frontend/css/fontawesome-iconpicker.min.css"] + context["styles"] = [static("frontend/css/fontawesome-iconpicker.min.css")] context["buttons"] = [ ( reverse_lazy("mood:activity_delete", kwargs={"id": self.kwargs["id"]}), @@ -217,12 +216,12 @@ class ActivityCreateView(LoginRequiredMixin, CreateView): context["title"] = "Create Activity" context["subtitle"] = "Add a new activity." context["scripts"] = [ - "colorfield/jscolor/jscolor.js", - "colorfield/colorfield.js", - "frontend/js/fontawesome-iconpicker.min.js", - "frontend/js/iconpicker-loader.js", + static("colorfield/jscolor/jscolor.js"), + static("colorfield/colorfield.js"), + static("frontend/js/fontawesome-iconpicker.min.js"), + static("frontend/js/iconpicker-loader.js"), ] - context["styles"] = ["frontend/css/fontawesome-iconpicker.min.css"] + context["styles"] = [static("frontend/css/fontawesome-iconpicker.min.css")] return context def form_valid(self, form): @@ -294,12 +293,12 @@ class MoodCreateView(LoginRequiredMixin, CreateView): context["title"] = "Create Activity" context["subtitle"] = "Add a new activity." context["scripts"] = [ - "colorfield/jscolor/jscolor.js", - "colorfield/colorfield.js", - "frontend/js/fontawesome-iconpicker.min.js", - "frontend/js/iconpicker-loader.js", + static("colorfield/jscolor/jscolor.js"), + static("colorfield/colorfield.js"), + static("frontend/js/fontawesome-iconpicker.min.js"), + static("frontend/js/iconpicker-loader.js"), ] - context["styles"] = ["frontend/css/fontawesome-iconpicker.min.css"] + context["styles"] = [static("frontend/css/fontawesome-iconpicker.min.css")] return context def form_valid(self, form):