2017-11-09 12:24:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2017-11-11 22:27:42 +00:00
|
|
|
from django.shortcuts import redirect, render
|
2017-11-09 12:24:32 +00:00
|
|
|
from django.urls import reverse
|
2017-11-11 22:27:42 +00:00
|
|
|
|
|
|
|
from django.views.generic import View
|
2017-11-09 12:24:32 +00:00
|
|
|
from django.views.generic.base import TemplateView, RedirectView
|
|
|
|
|
2017-11-11 22:27:42 +00:00
|
|
|
from .forms import UserForm, UserSettingsForm
|
2017-11-09 12:24:32 +00:00
|
|
|
from core.models import Child
|
|
|
|
|
|
|
|
|
|
|
|
class RootRouter(LoginRequiredMixin, RedirectView):
|
2017-11-11 22:27:42 +00:00
|
|
|
"""
|
|
|
|
Redirects to the welcome page if no children are in the database, a child
|
|
|
|
dashboard if only one child is in the database, and the dashboard page if
|
|
|
|
more than one child is in the database.
|
|
|
|
"""
|
2017-11-09 12:24:32 +00:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
children = Child.objects.count()
|
|
|
|
if children == 0:
|
|
|
|
self.url = reverse('welcome')
|
|
|
|
elif children == 1:
|
|
|
|
self.url = reverse(
|
|
|
|
'dashboard-child', args={Child.objects.first().slug})
|
|
|
|
else:
|
|
|
|
self.url = reverse('dashboard')
|
|
|
|
return super(RootRouter, self).get_redirect_url(self, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2017-11-11 22:27:42 +00:00
|
|
|
class UserSettings(LoginRequiredMixin, View):
|
|
|
|
"""
|
|
|
|
Handles both the User and Settings models.
|
|
|
|
Based on this SO answer: https://stackoverflow.com/a/45056835.
|
|
|
|
"""
|
|
|
|
form_user_class = UserForm
|
|
|
|
form_settings_class = UserSettingsForm
|
|
|
|
template_name = 'babybuddy/user_settings_form.html'
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return render(request, self.template_name, {
|
|
|
|
'form_user': self.form_user_class(instance=request.user),
|
|
|
|
'form_settings': self.form_settings_class(
|
|
|
|
instance=request.user.settings)
|
|
|
|
})
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
form_user = self.form_user_class(
|
|
|
|
instance=request.user,
|
|
|
|
data=request.POST)
|
|
|
|
form_settings = self.form_settings_class(
|
|
|
|
instance=request.user.settings,
|
|
|
|
data=request.POST)
|
|
|
|
if form_user.is_valid() and form_settings.is_valid():
|
|
|
|
user = form_user.save(commit=False)
|
|
|
|
user_settings = form_settings.save(commit=False)
|
|
|
|
user.settings = user_settings
|
|
|
|
user.save()
|
|
|
|
return redirect('user-settings')
|
|
|
|
return render(request, self.template_name, {
|
|
|
|
'user_form': form_user,
|
|
|
|
'settings_form': form_settings
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-11-09 12:24:32 +00:00
|
|
|
class Welcome(LoginRequiredMixin, TemplateView):
|
2017-11-11 22:27:42 +00:00
|
|
|
"""
|
|
|
|
Basic introduction to Baby Buddy (meant to be shown when no data is in the
|
|
|
|
database).
|
|
|
|
"""
|
2017-11-09 12:24:32 +00:00
|
|
|
template_name = 'babybuddy/welcome.html'
|