2017-08-15 19:14:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-12-06 19:40:04 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2020-02-07 05:15:36 +00:00
|
|
|
from django.forms import Form
|
|
|
|
from django.http import HttpResponseRedirect
|
2017-12-06 22:16:47 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2017-11-07 18:15:48 +00:00
|
|
|
from django.utils import timezone
|
2019-04-14 05:51:44 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2021-08-22 03:42:29 +00:00
|
|
|
from django.views.generic.base import RedirectView, TemplateView
|
2017-08-18 03:45:56 +00:00
|
|
|
from django.views.generic.detail import DetailView
|
2022-02-10 00:00:30 +00:00
|
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView, FormView
|
2017-11-02 10:39:32 +00:00
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
from babybuddy.mixins import LoginRequiredMixin, PermissionRequiredMixin
|
2018-08-04 20:00:51 +00:00
|
|
|
from babybuddy.views import BabyBuddyFilterView
|
2017-11-07 18:15:48 +00:00
|
|
|
from core import forms, models, timeline
|
2017-08-15 19:14:03 +00:00
|
|
|
|
2017-08-15 20:24:55 +00:00
|
|
|
|
2021-08-29 20:50:22 +00:00
|
|
|
def _prepare_timeline_context_data(context, date, child=None):
|
2022-02-10 00:00:30 +00:00
|
|
|
date = timezone.datetime.strptime(date, "%Y-%m-%d")
|
2021-08-29 20:50:22 +00:00
|
|
|
date = timezone.localtime(timezone.make_aware(date))
|
2022-02-10 00:00:30 +00:00
|
|
|
context["timeline_objects"] = timeline.get_objects(date, child)
|
|
|
|
context["date"] = date
|
|
|
|
context["date_previous"] = date - timezone.timedelta(days=1)
|
2021-08-29 20:50:22 +00:00
|
|
|
if date.date() < timezone.localdate():
|
2022-02-10 00:00:30 +00:00
|
|
|
context["date_next"] = date + timezone.timedelta(days=1)
|
2021-08-29 20:50:22 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class CoreAddView(PermissionRequiredMixin, SuccessMessageMixin, CreateView):
|
2017-12-06 19:40:04 +00:00
|
|
|
def get_success_message(self, cleaned_data):
|
2022-02-10 00:00:30 +00:00
|
|
|
cleaned_data["model"] = self.model._meta.verbose_name.title()
|
|
|
|
if "child" in cleaned_data:
|
|
|
|
self.success_message = _("%(model)s entry for %(child)s added!")
|
2017-12-06 19:40:04 +00:00
|
|
|
else:
|
2022-02-10 00:00:30 +00:00
|
|
|
self.success_message = _("%(model)s entry added!")
|
2017-12-06 19:40:04 +00:00
|
|
|
return self.success_message % cleaned_data
|
|
|
|
|
2020-01-29 17:45:26 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
"""
|
|
|
|
Check for and add "child" and "timer" from request query parameters.
|
|
|
|
- "child" may provide a slug for a Child instance.
|
|
|
|
- "timer" may provided an ID for a Timer instance.
|
|
|
|
|
|
|
|
These arguments are used in some add views to pre-fill initial data in
|
|
|
|
the form fields.
|
|
|
|
|
|
|
|
:return: Updated keyword arguments.
|
|
|
|
"""
|
|
|
|
kwargs = super(CoreAddView, self).get_form_kwargs()
|
2022-02-10 00:00:30 +00:00
|
|
|
for parameter in ["child", "timer"]:
|
2020-01-29 18:50:23 +00:00
|
|
|
value = self.request.GET.get(parameter, None)
|
|
|
|
if value:
|
|
|
|
kwargs.update({parameter: value})
|
2020-01-29 17:45:26 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
class CoreUpdateView(PermissionRequiredMixin, SuccessMessageMixin, UpdateView):
|
2017-12-06 19:40:04 +00:00
|
|
|
def get_success_message(self, cleaned_data):
|
2022-02-10 00:00:30 +00:00
|
|
|
cleaned_data["model"] = self.model._meta.verbose_name.title()
|
|
|
|
if "child" in cleaned_data:
|
|
|
|
self.success_message = _("%(model)s entry for %(child)s updated.")
|
2017-12-06 19:40:04 +00:00
|
|
|
else:
|
2022-02-10 00:00:30 +00:00
|
|
|
self.success_message = _("%(model)s entry updated.")
|
2017-12-06 19:40:04 +00:00
|
|
|
return self.success_message % cleaned_data
|
|
|
|
|
|
|
|
|
2022-01-16 03:32:18 +00:00
|
|
|
class CoreDeleteView(PermissionRequiredMixin, SuccessMessageMixin, DeleteView):
|
|
|
|
def get_success_message(self, cleaned_data):
|
2022-02-10 00:00:30 +00:00
|
|
|
return _("%(model)s entry deleted.") % {
|
|
|
|
"model": self.model._meta.verbose_name.title()
|
2019-11-09 21:50:28 +00:00
|
|
|
}
|
2017-12-06 19:40:04 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class ChildList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Child
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/child_list.html"
|
|
|
|
permission_required = ("core.view_child",)
|
2017-09-10 21:02:57 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("first_name", "last_name")
|
2017-08-16 13:12:49 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class ChildAdd(CoreAddView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Child
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_child",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.ChildForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:child-list")
|
|
|
|
success_message = _("%(first_name)s %(last_name)s added!")
|
2017-08-15 20:24:55 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class ChildDetail(PermissionRequiredMixin, DetailView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Child
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.view_child",)
|
2017-10-20 19:23:26 +00:00
|
|
|
|
2017-11-07 18:15:48 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(ChildDetail, self).get_context_data(**kwargs)
|
2022-02-10 00:00:30 +00:00
|
|
|
date = self.request.GET.get("date", str(timezone.localdate()))
|
2021-08-29 20:50:22 +00:00
|
|
|
_prepare_timeline_context_data(context, date, self.object)
|
2017-11-07 18:15:48 +00:00
|
|
|
return context
|
|
|
|
|
2017-10-20 19:23:26 +00:00
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class ChildUpdate(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Child
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_child",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.ChildForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:child-list")
|
2017-08-15 20:24:55 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class ChildDelete(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Child
|
|
|
|
form_class = forms.ChildDeleteForm
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/child_confirm_delete.html"
|
|
|
|
permission_required = ("core.delete_child",)
|
|
|
|
success_url = reverse_lazy("core:child-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
2020-01-29 20:16:57 +00:00
|
|
|
def get_success_message(self, cleaned_data):
|
2022-02-10 00:00:30 +00:00
|
|
|
"""This class cannot use `CoreDeleteView` because of the confirmation
|
2020-01-29 20:16:57 +00:00
|
|
|
step required so the success message must be overridden."""
|
2022-02-10 00:00:30 +00:00
|
|
|
success_message = _("%(model)s entry deleted.") % {
|
|
|
|
"model": self.model._meta.verbose_name.title()
|
2020-01-29 20:16:57 +00:00
|
|
|
}
|
|
|
|
return success_message % cleaned_data
|
|
|
|
|
2017-08-16 15:16:27 +00:00
|
|
|
|
2022-03-03 03:00:56 +00:00
|
|
|
class BreastpumpList(PermissionRequiredMixin, BabyBuddyFilterView):
|
|
|
|
model = models.Breastpump
|
|
|
|
template_name = "core/breastpump_list.html"
|
|
|
|
permission_required = ("core.view_breastpump",)
|
|
|
|
paginate_by = 10
|
|
|
|
filterset_fields = ("child",)
|
|
|
|
|
|
|
|
|
|
|
|
class BreastpumpAdd(CoreAddView):
|
|
|
|
model = models.Breastpump
|
|
|
|
permission_required = ("core.add_breastpump",)
|
|
|
|
form_class = forms.BreastpumpForm
|
|
|
|
success_url = reverse_lazy("core:breastpump-list")
|
|
|
|
success_message = _("%(model)s reading added!")
|
|
|
|
|
|
|
|
|
|
|
|
class BreastpumpUpdate(CoreUpdateView):
|
|
|
|
model = models.Breastpump
|
|
|
|
permission_required = ("core.change_breastpump",)
|
|
|
|
form_class = forms.BreastpumpForm
|
|
|
|
success_url = reverse_lazy("core:breastpump-list")
|
|
|
|
success_message = _("%(model)s reading for %(child)s updated.")
|
|
|
|
|
|
|
|
|
|
|
|
class BreastpumpDelete(CoreDeleteView):
|
|
|
|
model = models.Breastpump
|
|
|
|
permission_required = ("core.delete_breastpump",)
|
|
|
|
success_url = reverse_lazy("core:breastpump-list")
|
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class DiaperChangeList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.DiaperChange
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/diaperchange_list.html"
|
|
|
|
permission_required = ("core.view_diaperchange",)
|
2017-09-10 21:02:57 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child", "wet", "solid", "color")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class DiaperChangeAdd(CoreAddView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.DiaperChange
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_diaperchange",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.DiaperChangeForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:diaperchange-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class DiaperChangeUpdate(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.DiaperChange
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_diaperchange",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.DiaperChangeForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:diaperchange-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class DiaperChangeDelete(CoreDeleteView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.DiaperChange
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_diaperchange",)
|
|
|
|
success_url = reverse_lazy("core:diaperchange-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class FeedingList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Feeding
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/feeding_list.html"
|
|
|
|
permission_required = ("core.view_feeding",)
|
2017-09-10 21:02:57 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child", "type", "method")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class FeedingAdd(CoreAddView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Feeding
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_feeding",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.FeedingForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:feeding-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class FeedingUpdate(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Feeding
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_feeding",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.FeedingForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:feeding-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class FeedingDelete(CoreDeleteView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Feeding
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_feeding",)
|
|
|
|
success_url = reverse_lazy("core:feeding-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class NoteList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Note
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/note_list.html"
|
|
|
|
permission_required = ("core.view_note",)
|
2017-09-10 21:02:57 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class NoteAdd(CoreAddView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Note
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_note",)
|
2017-11-10 00:50:54 +00:00
|
|
|
form_class = forms.NoteForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:note-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class NoteUpdate(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Note
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_note",)
|
2020-02-16 22:43:51 +00:00
|
|
|
form_class = forms.NoteForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:note-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class NoteDelete(CoreDeleteView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Note
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_note",)
|
|
|
|
success_url = reverse_lazy("core:note-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class SleepList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Sleep
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/sleep_list.html"
|
|
|
|
permission_required = ("core.view_sleep",)
|
2017-09-10 21:02:57 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class SleepAdd(CoreAddView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Sleep
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_sleep",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.SleepForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:sleep-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class SleepUpdate(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Sleep
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_sleep",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.SleepForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:sleep-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class SleepDelete(CoreDeleteView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Sleep
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_sleep",)
|
|
|
|
success_url = reverse_lazy("core:sleep-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TemperatureList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2019-05-17 04:33:26 +00:00
|
|
|
model = models.Temperature
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/temperature_list.html"
|
|
|
|
permission_required = ("core.view_temperature",)
|
2019-05-17 04:33:26 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2019-05-17 04:33:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TemperatureAdd(CoreAddView):
|
|
|
|
model = models.Temperature
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_temperature",)
|
2019-05-17 04:33:26 +00:00
|
|
|
form_class = forms.TemperatureForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:temperature-list")
|
|
|
|
success_message = _("%(model)s reading added!")
|
2019-05-17 04:33:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TemperatureUpdate(CoreUpdateView):
|
|
|
|
model = models.Temperature
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_temperature",)
|
2020-02-16 22:43:51 +00:00
|
|
|
form_class = forms.TemperatureForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:temperature-list")
|
|
|
|
success_message = _("%(model)s reading for %(child)s updated.")
|
2019-05-17 04:33:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TemperatureDelete(CoreDeleteView):
|
|
|
|
model = models.Temperature
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_temperature",)
|
|
|
|
success_url = reverse_lazy("core:temperature-list")
|
2019-05-17 04:33:26 +00:00
|
|
|
|
|
|
|
|
2021-08-22 03:42:29 +00:00
|
|
|
class Timeline(LoginRequiredMixin, TemplateView):
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "timeline/timeline.html"
|
2021-08-22 03:42:29 +00:00
|
|
|
|
|
|
|
# Show the overall timeline or a child timeline if one Child instance.
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
children = models.Child.objects.count()
|
|
|
|
if children == 1:
|
|
|
|
return HttpResponseRedirect(
|
2022-02-10 00:00:30 +00:00
|
|
|
reverse("core:child", args={models.Child.objects.first().slug})
|
2021-08-22 03:42:29 +00:00
|
|
|
)
|
|
|
|
return super(Timeline, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(Timeline, self).get_context_data(**kwargs)
|
2022-02-10 00:00:30 +00:00
|
|
|
date = self.request.GET.get("date", str(timezone.localdate()))
|
2021-08-29 20:50:22 +00:00
|
|
|
_prepare_timeline_context_data(context, date)
|
2021-08-22 03:42:29 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TimerList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Timer
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/timer_list.html"
|
|
|
|
permission_required = ("core.view_timer",)
|
2017-09-10 21:02:57 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("active", "user")
|
2017-09-09 15:54:34 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TimerDetail(PermissionRequiredMixin, DetailView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Timer
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.view_timer",)
|
2017-08-18 03:45:56 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TimerAdd(PermissionRequiredMixin, CreateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Timer
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_timer",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.TimerForm
|
2017-08-17 01:57:01 +00:00
|
|
|
|
2017-08-18 00:42:47 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(TimerAdd, self).get_form_kwargs()
|
2022-02-10 00:00:30 +00:00
|
|
|
kwargs.update({"user": self.request.user})
|
2017-08-18 00:42:47 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-12-06 22:16:47 +00:00
|
|
|
def get_success_url(self):
|
2022-02-10 00:00:30 +00:00
|
|
|
return reverse("core:timer-detail", kwargs={"pk": self.object.pk})
|
2017-12-06 22:16:47 +00:00
|
|
|
|
2017-08-17 01:57:01 +00:00
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class TimerUpdate(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Timer
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_timer",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.TimerForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:timer-list")
|
2017-09-10 09:11:09 +00:00
|
|
|
|
2017-09-10 13:50:16 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(TimerUpdate, self).get_form_kwargs()
|
2022-02-10 00:00:30 +00:00
|
|
|
kwargs.update({"user": self.request.user})
|
2017-09-10 13:50:16 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-09-10 09:11:09 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
instance = self.get_object()
|
2022-02-10 00:00:30 +00:00
|
|
|
return reverse("core:timer-detail", kwargs={"pk": instance.pk})
|
2017-09-10 09:11:09 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TimerAddQuick(PermissionRequiredMixin, RedirectView):
|
2022-02-10 00:00:30 +00:00
|
|
|
http_method_names = ["post"]
|
|
|
|
permission_required = ("core.add_timer",)
|
2017-08-18 03:10:07 +00:00
|
|
|
|
2021-07-31 20:56:48 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2017-11-07 18:15:48 +00:00
|
|
|
instance = models.Timer.objects.create(user=request.user)
|
2020-01-29 06:15:55 +00:00
|
|
|
# Add child relationship if there is only Child instance.
|
|
|
|
if models.Child.count() == 1:
|
|
|
|
instance.child = models.Child.objects.first()
|
2017-08-18 03:10:07 +00:00
|
|
|
instance.save()
|
2017-08-18 04:48:26 +00:00
|
|
|
self.url = request.GET.get(
|
2022-02-10 00:00:30 +00:00
|
|
|
"next", reverse("core:timer-detail", args={instance.id})
|
|
|
|
)
|
2017-08-18 03:10:07 +00:00
|
|
|
return super(TimerAddQuick, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TimerRestart(PermissionRequiredMixin, RedirectView):
|
2022-02-10 00:00:30 +00:00
|
|
|
http_method_names = ["post"]
|
|
|
|
permission_required = ("core.change_timer",)
|
2017-09-09 16:47:41 +00:00
|
|
|
|
2021-07-31 20:16:50 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2022-02-10 00:00:30 +00:00
|
|
|
instance = models.Timer.objects.get(id=kwargs["pk"])
|
2017-09-09 16:47:41 +00:00
|
|
|
instance.restart()
|
2022-02-10 00:00:30 +00:00
|
|
|
messages.success(request, "{} restarted.".format(instance))
|
2017-09-09 16:47:41 +00:00
|
|
|
return super(TimerRestart, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2022-02-10 00:00:30 +00:00
|
|
|
return reverse("core:timer-detail", kwargs={"pk": kwargs["pk"]})
|
2017-09-09 16:47:41 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TimerStop(PermissionRequiredMixin, SuccessMessageMixin, RedirectView):
|
2022-02-10 00:00:30 +00:00
|
|
|
http_method_names = ["post"]
|
|
|
|
permission_required = ("core.change_timer",)
|
|
|
|
success_message = _("%(timer)s stopped.")
|
2017-09-08 18:36:22 +00:00
|
|
|
|
2021-07-31 20:16:50 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2022-02-10 00:00:30 +00:00
|
|
|
instance = models.Timer.objects.get(id=kwargs["pk"])
|
2017-09-09 16:47:41 +00:00
|
|
|
instance.stop()
|
2022-02-10 00:00:30 +00:00
|
|
|
messages.success(request, "{} stopped.".format(instance))
|
2017-09-08 18:36:22 +00:00
|
|
|
return super(TimerStop, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2022-02-10 00:00:30 +00:00
|
|
|
return reverse("core:timer-detail", kwargs={"pk": kwargs["pk"]})
|
2017-09-08 18:36:22 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class TimerDelete(CoreDeleteView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.Timer
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_timer",)
|
|
|
|
success_url = reverse_lazy("core:timer-list")
|
2017-08-18 04:55:57 +00:00
|
|
|
|
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
class TimerDeleteInactive(PermissionRequiredMixin, SuccessMessageMixin, FormView):
|
|
|
|
permission_required = ("core.delete_timer",)
|
2020-02-07 05:15:36 +00:00
|
|
|
form_class = Form
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/timer_confirm_delete_inactive.html"
|
|
|
|
success_url = reverse_lazy("core:timer-list")
|
|
|
|
success_message = _("All inactive timers deleted.")
|
2020-02-07 05:15:36 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super().get_context_data(**kwargs)
|
2022-02-10 00:00:30 +00:00
|
|
|
kwargs["timer_count"] = self.get_instances().count()
|
2020-02-07 05:15:36 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
# Redirect back to list if there are no inactive timers.
|
|
|
|
if self.get_instances().count() == 0:
|
2022-02-10 00:00:30 +00:00
|
|
|
messages.warning(request, _("No inactive timers exist."))
|
2020-02-07 05:15:36 +00:00
|
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.get_instances().delete()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_instances():
|
|
|
|
return models.Timer.objects.filter(active=False)
|
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class TummyTimeList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.TummyTime
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/tummytime_list.html"
|
|
|
|
permission_required = ("core.view_tummytime",)
|
2017-09-10 21:02:57 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class TummyTimeAdd(CoreAddView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.TummyTime
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_tummytime",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.TummyTimeForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:tummytime-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class TummyTimeUpdate(CoreUpdateView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.TummyTime
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_tummytime",)
|
2017-11-07 18:15:48 +00:00
|
|
|
form_class = forms.TummyTimeForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:tummytime-list")
|
2017-08-16 15:16:27 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class TummyTimeDelete(CoreDeleteView):
|
2017-11-07 18:15:48 +00:00
|
|
|
model = models.TummyTime
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_tummytime",)
|
|
|
|
success_url = reverse_lazy("core:tummytime-list")
|
2017-11-10 02:15:09 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class WeightList(PermissionRequiredMixin, BabyBuddyFilterView):
|
2017-11-10 02:15:09 +00:00
|
|
|
model = models.Weight
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/weight_list.html"
|
|
|
|
permission_required = ("core.view_weight",)
|
2017-11-10 02:15:09 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2017-11-10 02:15:09 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class WeightAdd(CoreAddView):
|
2017-11-10 02:15:09 +00:00
|
|
|
model = models.Weight
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_weight",)
|
2017-11-10 02:15:09 +00:00
|
|
|
form_class = forms.WeightForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:weight-list")
|
2017-11-10 02:15:09 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class WeightUpdate(CoreUpdateView):
|
2017-11-10 02:15:09 +00:00
|
|
|
model = models.Weight
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_weight",)
|
2020-02-16 22:43:51 +00:00
|
|
|
form_class = forms.WeightForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:weight-list")
|
2017-11-10 02:15:09 +00:00
|
|
|
|
|
|
|
|
2017-12-06 19:40:04 +00:00
|
|
|
class WeightDelete(CoreDeleteView):
|
2017-11-10 02:15:09 +00:00
|
|
|
model = models.Weight
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_weight",)
|
|
|
|
success_url = reverse_lazy("core:weight-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
2021-12-30 03:32:55 +00:00
|
|
|
|
2021-12-29 07:53:36 +00:00
|
|
|
class HeightList(PermissionRequiredMixin, BabyBuddyFilterView):
|
|
|
|
model = models.Height
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/height_list.html"
|
|
|
|
permission_required = ("core.view_height",)
|
2021-12-29 07:53:36 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeightAdd(CoreAddView):
|
|
|
|
model = models.Height
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_height",)
|
2021-12-29 07:53:36 +00:00
|
|
|
form_class = forms.HeightForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:height-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeightUpdate(CoreUpdateView):
|
|
|
|
model = models.Height
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_height",)
|
2021-12-29 07:53:36 +00:00
|
|
|
form_class = forms.HeightForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:height-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeightDelete(CoreDeleteView):
|
|
|
|
model = models.Height
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_height",)
|
|
|
|
success_url = reverse_lazy("core:height-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
2021-12-30 03:32:55 +00:00
|
|
|
|
2021-12-29 07:53:36 +00:00
|
|
|
class HeadCircumferenceList(PermissionRequiredMixin, BabyBuddyFilterView):
|
|
|
|
model = models.HeadCircumference
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/head_circumference_list.html"
|
|
|
|
permission_required = ("core.view_head_circumference",)
|
2021-12-29 07:53:36 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeadCircumferenceAdd(CoreAddView):
|
|
|
|
model = models.HeadCircumference
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/head_circumference_form.html"
|
|
|
|
permission_required = ("core.add_head_circumference",)
|
2021-12-29 07:53:36 +00:00
|
|
|
form_class = forms.HeadCircumferenceForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:head-circumference-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeadCircumferenceUpdate(CoreUpdateView):
|
|
|
|
model = models.HeadCircumference
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/head_circumference_form.html"
|
|
|
|
permission_required = ("core.change_head_circumference",)
|
2021-12-29 07:53:36 +00:00
|
|
|
form_class = forms.HeadCircumferenceForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:head-circumference-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeadCircumferenceDelete(CoreDeleteView):
|
|
|
|
model = models.HeadCircumference
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/head_circumference_delete.html"
|
|
|
|
permission_required = ("core.delete_head_circumference",)
|
|
|
|
success_url = reverse_lazy("core:head-circumference-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
2021-12-30 03:32:55 +00:00
|
|
|
|
2021-12-29 07:53:36 +00:00
|
|
|
class BMIList(PermissionRequiredMixin, BabyBuddyFilterView):
|
|
|
|
model = models.BMI
|
2022-02-10 00:00:30 +00:00
|
|
|
template_name = "core/bmi_list.html"
|
|
|
|
permission_required = ("core.view_bmi",)
|
2021-12-29 07:53:36 +00:00
|
|
|
paginate_by = 10
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child",)
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BMIAdd(CoreAddView):
|
|
|
|
model = models.BMI
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.add_bmi",)
|
2021-12-29 07:53:36 +00:00
|
|
|
form_class = forms.BMIForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:bmi-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BMIUpdate(CoreUpdateView):
|
|
|
|
model = models.BMI
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.change_bmi",)
|
2021-12-29 07:53:36 +00:00
|
|
|
form_class = forms.BMIForm
|
2022-02-10 00:00:30 +00:00
|
|
|
success_url = reverse_lazy("core:bmi-list")
|
2021-12-29 07:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BMIDelete(CoreDeleteView):
|
|
|
|
model = models.BMI
|
2022-02-10 00:00:30 +00:00
|
|
|
permission_required = ("core.delete_bmi",)
|
|
|
|
success_url = reverse_lazy("core:bmi-list")
|