2017-08-18 13:02:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-05-20 21:40:09 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2017-11-06 21:24:21 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2017-08-18 13:02:31 +00:00
|
|
|
from django.urls import reverse
|
2017-11-09 12:24:32 +00:00
|
|
|
from django.views.generic.base import TemplateView
|
2017-08-18 13:02:31 +00:00
|
|
|
from django.views.generic.detail import DetailView
|
|
|
|
|
2018-05-20 21:40:09 +00:00
|
|
|
from babybuddy.mixins import PermissionRequired403Mixin
|
2017-08-18 13:02:31 +00:00
|
|
|
from core.models import Child
|
|
|
|
|
|
|
|
|
2017-11-06 21:24:21 +00:00
|
|
|
class Dashboard(LoginRequiredMixin, TemplateView):
|
|
|
|
# TODO: Use .card-deck in this template once BS4 is finalized.
|
|
|
|
template_name = 'dashboard/dashboard.html'
|
|
|
|
|
2017-08-18 13:02:31 +00:00
|
|
|
# Show the overall dashboard or a child dashboard if one Child instance.
|
|
|
|
def get(self, request, *args, **kwargs):
|
2017-08-24 12:04:54 +00:00
|
|
|
children = Child.objects.count()
|
|
|
|
if children == 0:
|
2017-12-03 21:52:27 +00:00
|
|
|
return HttpResponseRedirect(reverse('babybuddy:welcome'))
|
2017-08-24 12:04:54 +00:00
|
|
|
elif children == 1:
|
2017-11-06 21:24:21 +00:00
|
|
|
return HttpResponseRedirect(
|
2017-12-03 21:52:27 +00:00
|
|
|
reverse(
|
|
|
|
'dashboard:dashboard-child',
|
|
|
|
args={Child.objects.first().slug}
|
|
|
|
)
|
|
|
|
)
|
2017-11-06 21:24:21 +00:00
|
|
|
return super(Dashboard, self).get(request, *args, **kwargs)
|
2017-08-18 13:02:31 +00:00
|
|
|
|
2017-08-24 17:09:41 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(Dashboard, self).get_context_data(**kwargs)
|
2019-07-08 04:11:27 +00:00
|
|
|
context['objects'] = Child.objects.all() \
|
|
|
|
.order_by('last_name', 'first_name', 'id')
|
2017-08-24 17:09:41 +00:00
|
|
|
return context
|
|
|
|
|
2017-08-18 13:02:31 +00:00
|
|
|
|
2018-05-20 21:40:09 +00:00
|
|
|
class ChildDashboard(PermissionRequired403Mixin, DetailView):
|
2017-08-18 13:02:31 +00:00
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
2018-05-20 21:40:09 +00:00
|
|
|
raise_exception = True
|
2017-08-18 13:02:31 +00:00
|
|
|
template_name = 'dashboard/child.html'
|