mybuddy/babybuddy/views.py

106 lines
3.5 KiB
Python
Raw Normal View History

2017-11-09 12:24:32 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
2017-12-02 21:20:15 +00:00
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm
2017-11-09 12:24:32 +00:00
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-12-02 21:20:15 +00:00
from .forms import UserForm, UserPasswordForm, 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:
2017-12-03 21:52:27 +00:00
self.url = reverse('babybuddy:welcome')
2017-11-09 12:24:32 +00:00
elif children == 1:
self.url = reverse(
2017-12-03 21:52:27 +00:00
'dashboard:dashboard-child', args={Child.objects.first().slug})
2017-11-09 12:24:32 +00:00
else:
2017-12-03 21:52:27 +00:00
self.url = reverse('dashboard:dashboard')
2017-11-09 12:24:32 +00:00
return super(RootRouter, self).get_redirect_url(self, *args, **kwargs)
2017-12-02 21:20:15 +00:00
class UserPassword(LoginRequiredMixin, View):
"""
Handles user password changes.
"""
form_class = UserPasswordForm
template_name = 'babybuddy/user_password_form.html'
def get(self, request):
return render(request, self.template_name, {
'form': self.form_class(request.user)
})
def post(self, request):
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user)
return redirect('/')
return render(request, self.template_name, {'form': form})
class UserResetAPIKey(LoginRequiredMixin, View):
"""
Resets the API key of the logged in user.
"""
def get(self, request):
request.user.settings.api_key(reset=True)
return redirect('babybuddy:user-settings')
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()
2017-12-03 21:52:27 +00:00
return redirect('babybuddy:user-settings')
2017-11-11 22:27:42 +00:00
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'