kumify/dreams/views.py

324 lines
11 KiB
Python
Raw Normal View History

from django.views.generic import (
ListView,
UpdateView,
DetailView,
CreateView,
DeleteView,
)
2021-01-01 16:49:39 +00:00
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
2021-01-01 16:49:39 +00:00
from .models import Dream, DreamTheme, DreamMedia, Theme
from .forms import DreamForm
from common.helpers import get_upload_path
2021-01-01 16:49:39 +00:00
from msgio.models import NotificationDailySchedule, Notification
2021-01-01 16:49:39 +00:00
class DreamListView(LoginRequiredMixin, ListView):
template_name = "dreams/dream_list.html"
model = Dream
def get_context_data(self, **kwargs):
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")
]
2021-01-01 16:49:39 +00:00
return context
def get_queryset(self):
return Dream.objects.filter(user=self.request.user).order_by("timestamp")
2021-01-01 16:49:39 +00:00
class DreamViewView(LoginRequiredMixin, DetailView):
template_name = "dreams/dream_view.html"
model = Dream
def get_context_data(self, **kwargs):
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",
)
]
2021-01-01 16:49:39 +00:00
return context
def get_object(self):
return get_object_or_404(Dream, user=self.request.user, id=self.kwargs["id"])
class DreamCreateView(LoginRequiredMixin, CreateView):
template_name = "dreams/dream_edit.html"
form_class = DreamForm
model = Dream
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Create Dream"
context["subtitle"] = "What did you dream?"
context["scripts"] = [static("frontend/js/dropdown-to-buttons.js")]
2021-01-01 16:49:39 +00:00
return context
def form_valid(self, form):
form.instance.user = self.request.user
ret = super().form_valid(form)
for theme in form.cleaned_data["themes"]:
if theme.user == self.request.user:
DreamTheme.objects.create(theme=theme, dream=form.instance)
for attachment in form.cleaned_data["uploads"]:
dba = DreamMedia(dream=form.instance)
dba.media.save(get_upload_path(form.instance, attachment.name), attachment)
dba.save()
2021-01-01 16:49:39 +00:00
return ret
def get_success_url(self):
return reverse_lazy("dreams:dream_view", kwargs={"id": self.object.id})
class DreamEditView(LoginRequiredMixin, UpdateView):
template_name = "dreams/dream_edit.html"
form_class = DreamForm
model = Dream
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Update Dream"
context["subtitle"] = "Change details of a dream you entered before."
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",
)
]
2021-01-01 16:49:39 +00:00
return context
def get_object(self):
return get_object_or_404(Dream, user=self.request.user, id=self.kwargs["id"])
def form_valid(self, form):
for theme in form.cleaned_data["themes"]:
if theme.user == self.request.user:
if theme not in form.instance.theme_set:
2021-01-01 16:49:39 +00:00
DreamTheme.objects.create(theme=theme, dream=form.instance)
for dreamtheme in form.instance.dreamtheme_set.all():
if dreamtheme.theme not in form.cleaned_data["themes"]:
2021-01-01 16:49:39 +00:00
dreamtheme.delete()
for attachment in form.cleaned_data["uploads"]:
dba = DreamMedia(dream=form.instance)
dba.media.save(get_upload_path(form.instance, attachment.name), attachment)
dba.save()
2021-01-01 16:49:39 +00:00
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy("dreams:dream_view", kwargs={"id": self.object.id})
class DreamDeleteView(LoginRequiredMixin, DeleteView):
template_name = "dreams/dream_delete.html"
model = Dream
def get_object(self):
return get_object_or_404(Dream, user=self.request.user, id=self.kwargs["id"])
def get_success_url(self):
return reverse_lazy("dreams:dream_list")
class ThemeListView(LoginRequiredMixin, ListView):
template_name = "dreams/theme_list.html"
model = Theme
def get_context_data(self, **kwargs):
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")
]
2021-01-01 16:49:39 +00:00
return context
def get_queryset(self):
return Theme.objects.filter(user=self.request.user)
class ThemeEditView(LoginRequiredMixin, UpdateView):
template_name = "dreams/theme_edit.html"
model = Theme
fields = ["name", "icon", "color"]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Edit Theme"
context["subtitle"] = "Make changes to the theme."
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",
)
]
2021-01-01 16:49:39 +00:00
return context
def get_object(self):
return get_object_or_404(Theme, user=self.request.user, id=self.kwargs["id"])
def get_success_url(self):
return reverse_lazy("dreams:theme_list")
class ThemeCreateView(LoginRequiredMixin, CreateView):
template_name = "dreams/theme_edit.html"
model = Theme
fields = ["name", "icon", "color"]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Create Theme"
context["subtitle"] = "Add a new theme for your dreams."
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")]
2021-01-01 16:49:39 +00:00
return context
def form_valid(self, form):
obj = form.save(commit=False)
obj.user = self.request.user
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy("dreams:theme_list")
class ThemeDeleteView(LoginRequiredMixin, DeleteView):
template_name = "dreams/theme_delete.html"
model = Theme
def get_object(self):
return get_object_or_404(Theme, user=self.request.user, id=self.kwargs["id"])
def get_success_url(self):
return reverse_lazy("dreams:theme_list")
class NotificationListView(LoginRequiredMixin, ListView):
template_name = "dreams/notification_list.html"
model = NotificationDailySchedule
fields = ["time"]
def get_context_data(self, **kwargs):
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")
]
2021-01-01 16:49:39 +00:00
return context
def get_queryset(self):
return NotificationDailySchedule.objects.filter(
notification__recipient=self.request.user, notification__app="dreams"
)
2021-01-01 16:49:39 +00:00
class NotificationCreateView(LoginRequiredMixin, CreateView):
template_name = "dreams/notification_edit.html"
model = NotificationDailySchedule
fields = ["time"]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Create Notification"
context["subtitle"] = "Add a new daily notification."
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",
)
2021-01-01 16:49:39 +00:00
obj = form.save(commit=False)
obj.notification = notification
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy("dreams:notification_list")
class NotificationEditView(LoginRequiredMixin, UpdateView):
template_name = "dreams/notification_edit.html"
model = NotificationDailySchedule
fields = ["time"]
def get_context_data(self, **kwargs):
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",
)
]
2021-01-01 16:49:39 +00:00
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"],
)
2021-01-01 16:49:39 +00:00
class NotificationDeleteView(LoginRequiredMixin, DeleteView):
template_name = "dreams/notification_delete.html"
model = NotificationDailySchedule
def get_object(self):
return get_object_or_404(
NotificationDailySchedule,
notification__recipient=self.request.user,
id=self.kwargs["id"],
)
2021-01-01 16:49:39 +00:00
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
self.object.notification.delete()
return HttpResponseRedirect(success_url)
def get_success_url(self):
return reverse_lazy("dreams:notification_list")