2017-11-09 12:24:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-12-06 18:03:26 +00:00
|
|
|
from django.contrib import messages
|
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-12-11 02:36:25 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2017-11-11 22:27:42 +00:00
|
|
|
from django.shortcuts import redirect, render
|
2017-12-11 02:36:25 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2021-11-28 19:05:56 +00:00
|
|
|
from django.utils import translation
|
2019-04-14 21:42:58 +00:00
|
|
|
from django.utils.text import format_lazy
|
2020-02-14 17:22:25 +00:00
|
|
|
from django.utils.translation import gettext as _, gettext_lazy
|
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-11 02:36:25 +00:00
|
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
2020-02-14 20:35:02 +00:00
|
|
|
from django.views.i18n import set_language
|
2017-12-11 02:36:25 +00:00
|
|
|
|
|
|
|
from django_filters.views import FilterView
|
2017-11-09 12:24:32 +00:00
|
|
|
|
2017-12-11 02:36:25 +00:00
|
|
|
from babybuddy import forms
|
2021-09-17 02:34:33 +00:00
|
|
|
from babybuddy.mixins import LoginRequiredMixin, PermissionRequiredMixin, \
|
|
|
|
StaffOnlyMixin
|
2017-11-09 12:24:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RootRouter(LoginRequiredMixin, RedirectView):
|
2017-11-11 22:27:42 +00:00
|
|
|
"""
|
2018-05-20 21:40:09 +00:00
|
|
|
Redirects to the site dashboard.
|
2017-11-11 22:27:42 +00:00
|
|
|
"""
|
2017-11-09 12:24:32 +00:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2018-05-20 15:54:28 +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)
|
|
|
|
|
|
|
|
|
2018-08-04 20:00:51 +00:00
|
|
|
class BabyBuddyFilterView(FilterView):
|
|
|
|
"""
|
|
|
|
Disables "strictness" for django-filter. It is unclear from the
|
|
|
|
documentation exactly what this does...
|
|
|
|
"""
|
|
|
|
# TODO Figure out the correct way to use this.
|
|
|
|
strict = False
|
|
|
|
|
2021-08-02 20:32:49 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
children = {
|
|
|
|
o.child for o in context['object_list'] if hasattr(o, "child")
|
|
|
|
}
|
|
|
|
if len(children) == 1:
|
|
|
|
context['unique_child'] = True
|
|
|
|
return context
|
|
|
|
|
2018-08-04 20:00:51 +00:00
|
|
|
|
|
|
|
class UserList(StaffOnlyMixin, BabyBuddyFilterView):
|
2017-12-11 02:36:25 +00:00
|
|
|
model = User
|
|
|
|
template_name = 'babybuddy/user_list.html'
|
2017-12-11 22:27:29 +00:00
|
|
|
ordering = 'username'
|
2017-12-11 02:36:25 +00:00
|
|
|
paginate_by = 10
|
2018-08-04 20:00:51 +00:00
|
|
|
filterset_fields = ('username', 'first_name', 'last_name', 'email')
|
2017-12-11 02:36:25 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class UserAdd(StaffOnlyMixin, PermissionRequiredMixin, SuccessMessageMixin,
|
2017-12-13 18:49:10 +00:00
|
|
|
CreateView):
|
2017-12-11 02:36:25 +00:00
|
|
|
model = User
|
|
|
|
template_name = 'babybuddy/user_form.html'
|
|
|
|
permission_required = ('admin.add_user',)
|
|
|
|
form_class = forms.UserAddForm
|
|
|
|
success_url = reverse_lazy('babybuddy:user-list')
|
2019-04-14 21:42:58 +00:00
|
|
|
success_message = gettext_lazy('User %(username)s added!')
|
2017-12-11 02:36:25 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class UserUpdate(StaffOnlyMixin, PermissionRequiredMixin,
|
2018-05-20 21:40:09 +00:00
|
|
|
SuccessMessageMixin, UpdateView):
|
2017-12-11 02:36:25 +00:00
|
|
|
model = User
|
|
|
|
template_name = 'babybuddy/user_form.html'
|
|
|
|
permission_required = ('admin.change_user',)
|
|
|
|
form_class = forms.UserUpdateForm
|
|
|
|
success_url = reverse_lazy('babybuddy:user-list')
|
2019-04-14 21:42:58 +00:00
|
|
|
success_message = gettext_lazy('User %(username)s updated.')
|
2017-12-11 02:36:25 +00:00
|
|
|
|
|
|
|
|
2021-09-17 02:34:33 +00:00
|
|
|
class UserDelete(StaffOnlyMixin, PermissionRequiredMixin,
|
2017-12-13 18:49:10 +00:00
|
|
|
DeleteView):
|
2017-12-11 02:36:25 +00:00
|
|
|
model = User
|
|
|
|
template_name = 'babybuddy/user_confirm_delete.html'
|
|
|
|
permission_required = ('admin.delete_user',)
|
|
|
|
success_url = reverse_lazy('babybuddy:user-list')
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
2019-04-14 21:42:58 +00:00
|
|
|
success_message = format_lazy(gettext_lazy(
|
|
|
|
'User {user} deleted.'), user=self.get_object()
|
|
|
|
)
|
2017-12-11 02:36:25 +00:00
|
|
|
messages.success(request, success_message)
|
|
|
|
return super(UserDelete, self).delete(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2017-12-02 21:20:15 +00:00
|
|
|
class UserPassword(LoginRequiredMixin, View):
|
|
|
|
"""
|
|
|
|
Handles user password changes.
|
|
|
|
"""
|
2017-12-11 02:36:25 +00:00
|
|
|
form_class = forms.UserPasswordForm
|
2017-12-02 21:20:15 +00:00
|
|
|
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)
|
2019-04-14 04:50:35 +00:00
|
|
|
messages.success(request, _('Password updated.'))
|
2017-12-02 21:20:15 +00:00
|
|
|
return render(request, self.template_name, {'form': form})
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2017-12-11 02:36:25 +00:00
|
|
|
form_user_class = forms.UserForm
|
|
|
|
form_settings_class = forms.UserSettingsForm
|
2017-11-11 22:27:42 +00:00
|
|
|
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):
|
2021-06-22 04:27:45 +00:00
|
|
|
if request.POST.get('api_key_regenerate'):
|
|
|
|
request.user.settings.api_key(reset=True)
|
|
|
|
messages.success(request, _('User API key regenerated.'))
|
|
|
|
return redirect('babybuddy:user-settings')
|
|
|
|
|
2017-11-11 22:27:42 +00:00
|
|
|
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()
|
2021-11-28 19:05:56 +00:00
|
|
|
translation.activate(user.settings.language)
|
2019-04-14 04:50:35 +00:00
|
|
|
messages.success(request, _('Settings saved!'))
|
2021-11-28 19:05:56 +00:00
|
|
|
translation.deactivate()
|
2020-01-04 16:09:28 +00:00
|
|
|
return set_language(request)
|
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'
|