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.
This commit is contained in:
Kumi 2024-11-20 21:27:56 +01:00
parent c0d5e15687
commit 94c4fb650b
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 97 additions and 38 deletions

View file

@ -1,9 +1,15 @@
from django.views.generic import (
from django.views.generic import ListView, UpdateView, DetailView, CreateView, DeleteView ListView,
UpdateView,
DetailView,
CreateView,
DeleteView,
)
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.templatetags.static import static
from .models import Dream, DreamTheme, DreamMedia, Theme from .models import Dream, DreamTheme, DreamMedia, Theme
from .forms import DreamForm from .forms import DreamForm
@ -11,6 +17,7 @@ from .forms import DreamForm
from common.helpers import get_upload_path from common.helpers import get_upload_path
from msgio.models import NotificationDailySchedule, Notification from msgio.models import NotificationDailySchedule, Notification
class DreamListView(LoginRequiredMixin, ListView): class DreamListView(LoginRequiredMixin, ListView):
template_name = "dreams/dream_list.html" template_name = "dreams/dream_list.html"
model = Dream model = Dream
@ -19,11 +26,13 @@ class DreamListView(LoginRequiredMixin, ListView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Dream List" context["title"] = "Dream List"
context["subtitle"] = "A list of the dreams you have entered so far." 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 return context
def get_queryset(self): 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): class DreamViewView(LoginRequiredMixin, DetailView):
@ -34,7 +43,13 @@ class DreamViewView(LoginRequiredMixin, DetailView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "View Dream" context["title"] = "View Dream"
context["subtitle"] = "View the details of your 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 return context
def get_object(self): def get_object(self):
@ -50,7 +65,7 @@ class DreamCreateView(LoginRequiredMixin, CreateView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Create Dream" context["title"] = "Create Dream"
context["subtitle"] = "What did you 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 return context
def form_valid(self, form): def form_valid(self, form):
@ -82,8 +97,14 @@ class DreamEditView(LoginRequiredMixin, UpdateView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Update Dream" context["title"] = "Update Dream"
context["subtitle"] = "Change details of a dream you entered before." context["subtitle"] = "Change details of a dream you entered before."
context["scripts"] = ["frontend/js/dropdown-to-buttons.js"] 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")] context["buttons"] = [
(
reverse_lazy("dreams:dream_delete", kwargs={"id": self.kwargs["id"]}),
"Delete Dream",
"trash-alt",
)
]
return context return context
def get_object(self): def get_object(self):
@ -129,7 +150,9 @@ class ThemeListView(LoginRequiredMixin, ListView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Themes" context["title"] = "Themes"
context["subtitle"] = "The themes you have defined for your dreams." 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 return context
def get_queryset(self): def get_queryset(self):
@ -145,9 +168,20 @@ class ThemeEditView(LoginRequiredMixin, UpdateView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Edit Theme" context["title"] = "Edit Theme"
context["subtitle"] = "Make changes to the 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["scripts"] = [
context["styles"] = ["frontend/css/fontawesome-iconpicker.min.css"] static("colorfield/jscolor/jscolor.js"),
context["buttons"] = [(reverse_lazy("dreams:theme_delete", kwargs={"id": self.kwargs["id"]}), "Delete Theme", "trash-alt")] 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 return context
def get_object(self): def get_object(self):
@ -166,8 +200,13 @@ class ThemeCreateView(LoginRequiredMixin, CreateView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Create Theme" context["title"] = "Create Theme"
context["subtitle"] = "Add a new theme for your dreams." 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["scripts"] = [
context["styles"] = ["frontend/css/fontawesome-iconpicker.min.css"] 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 return context
def form_valid(self, form): def form_valid(self, form):
@ -199,11 +238,15 @@ class NotificationListView(LoginRequiredMixin, ListView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Notifications" context["title"] = "Notifications"
context["subtitle"] = "The daily reminders you have set up." 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 return context
def get_queryset(self): 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): class NotificationCreateView(LoginRequiredMixin, CreateView):
@ -218,7 +261,11 @@ class NotificationCreateView(LoginRequiredMixin, CreateView):
return context return context
def form_valid(self, form): 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 = form.save(commit=False)
obj.notification = notification obj.notification = notification
return super().form_valid(form) return super().form_valid(form)
@ -236,14 +283,23 @@ class NotificationEditView(LoginRequiredMixin, UpdateView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["title"] = "Edit Notification" context["title"] = "Edit Notification"
context["subtitle"] = "Change the time of a daily 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 return context
def get_success_url(self): def get_success_url(self):
return reverse_lazy("dreams:notification_list") return reverse_lazy("dreams:notification_list")
def get_object(self): 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): class NotificationDeleteView(LoginRequiredMixin, DeleteView):
@ -251,7 +307,11 @@ class NotificationDeleteView(LoginRequiredMixin, DeleteView):
model = NotificationDailySchedule model = NotificationDailySchedule
def get_object(self): 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): def delete(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
@ -260,4 +320,4 @@ class NotificationDeleteView(LoginRequiredMixin, DeleteView):
return HttpResponseRedirect(success_url) return HttpResponseRedirect(success_url)
def get_success_url(self): def get_success_url(self):
return reverse_lazy("dreams:notification_list") return reverse_lazy("dreams:notification_list")

View file

@ -14,8 +14,7 @@ from django.http import HttpResponseRedirect, HttpResponse
from django.utils import timezone from django.utils import timezone
from django.views.decorators.clickjacking import xframe_options_sameorigin from django.views.decorators.clickjacking import xframe_options_sameorigin
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.db.models import Count from django.contrib.staticfiles.templatetags.staticfiles import static
from django.db.models.functions import TruncDate
from .models import Status, Activity, Mood, StatusMedia, StatusActivity from .models import Status, Activity, Mood, StatusMedia, StatusActivity
from .forms import StatusForm from .forms import StatusForm
@ -185,12 +184,12 @@ class ActivityEditView(LoginRequiredMixin, UpdateView):
context["title"] = "Edit Activity" context["title"] = "Edit Activity"
context["subtitle"] = "Make changes to the activity." context["subtitle"] = "Make changes to the activity."
context["scripts"] = [ context["scripts"] = [
"colorfield/jscolor/jscolor.js", static("colorfield/jscolor/jscolor.js"),
"colorfield/colorfield.js", static("colorfield/colorfield.js"),
"frontend/js/fontawesome-iconpicker.min.js", static("frontend/js/fontawesome-iconpicker.min.js"),
"frontend/js/iconpicker-loader.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"] = [ context["buttons"] = [
( (
reverse_lazy("mood:activity_delete", kwargs={"id": self.kwargs["id"]}), reverse_lazy("mood:activity_delete", kwargs={"id": self.kwargs["id"]}),
@ -217,12 +216,12 @@ class ActivityCreateView(LoginRequiredMixin, CreateView):
context["title"] = "Create Activity" context["title"] = "Create Activity"
context["subtitle"] = "Add a new activity." context["subtitle"] = "Add a new activity."
context["scripts"] = [ context["scripts"] = [
"colorfield/jscolor/jscolor.js", static("colorfield/jscolor/jscolor.js"),
"colorfield/colorfield.js", static("colorfield/colorfield.js"),
"frontend/js/fontawesome-iconpicker.min.js", static("frontend/js/fontawesome-iconpicker.min.js"),
"frontend/js/iconpicker-loader.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 return context
def form_valid(self, form): def form_valid(self, form):
@ -294,12 +293,12 @@ class MoodCreateView(LoginRequiredMixin, CreateView):
context["title"] = "Create Activity" context["title"] = "Create Activity"
context["subtitle"] = "Add a new activity." context["subtitle"] = "Add a new activity."
context["scripts"] = [ context["scripts"] = [
"colorfield/jscolor/jscolor.js", static("colorfield/jscolor/jscolor.js"),
"colorfield/colorfield.js", static("colorfield/colorfield.js"),
"frontend/js/fontawesome-iconpicker.min.js", static("frontend/js/fontawesome-iconpicker.min.js"),
"frontend/js/iconpicker-loader.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 return context
def form_valid(self, form): def form_valid(self, form):