1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-11-27 02:43:01 +00:00
nk20/apps/member/views.py

266 lines
8.7 KiB
Python
Raw Normal View History

2020-02-18 20:30:26 +00:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
2019-08-10 17:01:15 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
2020-02-18 20:30:26 +00:00
2020-02-08 19:39:37 +00:00
from dal import autocomplete
2019-08-10 17:01:15 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
2020-02-17 22:30:55 +00:00
from django.shortcuts import redirect
2019-08-10 17:01:15 +00:00
from django.utils.translation import gettext_lazy as _
2020-02-18 20:14:29 +00:00
from django.views.generic import CreateView, DetailView, UpdateView, TemplateView
2019-09-23 10:50:14 +00:00
from django.contrib.auth.models import User
2019-08-11 14:22:52 +00:00
from django.urls import reverse_lazy
from django.db.models import Q
from django_tables2.views import SingleTableView
2020-02-17 18:25:33 +00:00
from rest_framework.authtoken.models import Token
2020-02-18 20:14:29 +00:00
from note.models import Alias, NoteUser
from note.models.transactions import Transaction
from note.tables import HistoryTable
from .models import Profile, Club, Membership
2020-02-18 11:31:15 +00:00
from .forms import SignUpForm, ProfileForm, ClubForm, MembershipForm, MemberFormSet, FormSetHelper
from .tables import ClubTable, UserTable
2019-09-23 10:50:14 +00:00
from .filters import UserFilter, UserFilterFormHelper
2020-02-18 11:31:15 +00:00
2019-08-11 22:30:29 +00:00
class UserCreateView(CreateView):
2019-08-11 14:22:52 +00:00
"""
Une vue pour inscrire un utilisateur et lui créer un profile
"""
form_class = SignUpForm
2019-08-11 14:22:52 +00:00
success_url = reverse_lazy('login')
2020-02-18 11:31:15 +00:00
template_name = 'member/signup.html'
second_form = ProfileForm
2019-08-11 14:22:52 +00:00
2020-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2020-02-03 18:24:23 +00:00
context["profile_form"] = self.second_form()
2019-08-11 14:22:52 +00:00
return context
2019-08-11 15:39:05 +00:00
def form_valid(self, form):
profile_form = ProfileForm(self.request.POST)
if form.is_valid() and profile_form.is_valid():
user = form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
2019-08-11 15:39:05 +00:00
return super().form_valid(form)
2019-08-11 21:25:27 +00:00
2020-02-18 11:31:15 +00:00
class UserUpdateView(LoginRequiredMixin, UpdateView):
2020-02-03 18:25:05 +00:00
model = User
2020-02-18 11:31:15 +00:00
fields = ['first_name', 'last_name', 'username', 'email']
2020-02-03 18:25:05 +00:00
template_name = 'member/profile_update.html'
second_form = ProfileForm
2020-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
2020-02-03 18:25:05 +00:00
context = super().get_context_data(**kwargs)
context['user_modified'] = context['user']
context['user'] = self.request.user
2020-02-18 11:31:15 +00:00
context["profile_form"] = self.second_form(
instance=context['user_modified'].profile)
2020-02-21 10:17:14 +00:00
context['title'] = _("Update Profile")
2020-02-03 18:25:05 +00:00
return context
def get_form(self, form_class=None):
2020-02-17 10:36:46 +00:00
form = super().get_form(form_class)
if 'username' not in form.data:
return form
new_username = form.data['username']
2020-02-17 10:36:46 +00:00
# Si l'utilisateur cherche à modifier son pseudo, le nouveau pseudo ne doit pas être proche d'un alias existant
2020-02-18 11:31:15 +00:00
note = NoteUser.objects.filter(
alias__normalized_name=Alias.normalize(new_username))
if note.exists() and note.get().user != self.request.user:
2020-02-18 11:31:15 +00:00
form.add_error('username',
_("An alias with a similar name already exists."))
return form
2020-02-03 18:25:05 +00:00
def form_valid(self, form):
2020-02-18 11:31:15 +00:00
profile_form = ProfileForm(
data=self.request.POST,
instance=self.request.user.profile,
)
2020-02-03 18:25:05 +00:00
if form.is_valid() and profile_form.is_valid():
new_username = form.data['username']
alias = Alias.objects.filter(name=new_username)
2020-02-17 10:36:46 +00:00
# Si le nouveau pseudo n'est pas un de nos alias, on supprime éventuellement un alias similaire pour le remplacer
if not alias.exists():
2020-02-18 11:31:15 +00:00
similar = Alias.objects.filter(
normalized_name=Alias.normalize(new_username))
if similar.exists():
similar.delete()
2020-02-17 10:36:46 +00:00
user = form.save(commit=False)
2020-02-18 11:31:15 +00:00
profile = profile_form.save(commit=False)
2020-02-03 18:25:05 +00:00
profile.user = user
profile.save()
2020-02-17 10:36:46 +00:00
user.save()
2020-02-03 18:25:05 +00:00
return super().form_valid(form)
def get_success_url(self, **kwargs):
2020-02-18 11:31:15 +00:00
if kwargs:
return reverse_lazy('member:user_detail',
kwargs={'pk': kwargs['id']})
2020-02-03 18:25:05 +00:00
else:
2020-02-18 11:31:15 +00:00
return reverse_lazy('member:user_detail', args=(self.object.id, ))
2020-02-03 18:25:05 +00:00
2020-02-18 11:31:15 +00:00
class UserDetailView(LoginRequiredMixin, DetailView):
"""
2020-02-21 10:53:37 +00:00
Affiche les informations sur un utilisateur, sa note, ses clubs...
"""
2019-08-11 22:30:29 +00:00
model = Profile
context_object_name = "profile"
2020-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user = context['profile'].user
history_list = \
Transaction.objects.all().filter(Q(source=user.note) | Q(destination=user.note))
context['history_list'] = HistoryTable(history_list)
club_list = \
Membership.objects.all().filter(user=user).only("club")
context['club_list'] = ClubTable(club_list)
2020-02-21 10:53:37 +00:00
context['title'] = _("Account #%(id)s: %(username)s") % {
'id': user.pk,
'username': user.username,
}
return context
2019-08-11 22:30:29 +00:00
2020-02-18 11:31:15 +00:00
class UserListView(LoginRequiredMixin, SingleTableView):
"""
Affiche la liste des utilisateurs, avec une fonction de recherche statique
"""
2019-09-23 10:50:14 +00:00
model = User
table_class = UserTable
template_name = 'member/user_list.html'
filter_class = UserFilter
formhelper_class = UserFilterFormHelper
2020-02-18 11:31:15 +00:00
def get_queryset(self, **kwargs):
2019-09-23 10:50:14 +00:00
qs = super().get_queryset()
2020-02-18 11:31:15 +00:00
self.filter = self.filter_class(self.request.GET, queryset=qs)
2019-09-23 10:50:14 +00:00
self.filter.form.helper = self.formhelper_class()
return self.filter.qs
2020-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
2019-09-23 10:50:14 +00:00
context = super().get_context_data(**kwargs)
context["filter"] = self.filter
return context
2020-02-17 20:32:08 +00:00
class ManageAuthTokens(LoginRequiredMixin, TemplateView):
2020-02-17 18:25:33 +00:00
"""
2020-02-17 20:32:08 +00:00
Affiche le jeton d'authentification, et permet de le regénérer
2020-02-17 18:25:33 +00:00
"""
2020-02-17 20:32:08 +00:00
model = Token
template_name = "member/manage_auth_tokens.html"
2020-02-17 18:25:33 +00:00
2020-02-17 22:30:55 +00:00
def get(self, request, *args, **kwargs):
2020-02-18 11:31:15 +00:00
if 'regenerate' in request.GET and Token.objects.filter(
user=request.user).exists():
2020-02-17 18:25:33 +00:00
Token.objects.get(user=self.request.user).delete()
2020-02-18 11:31:15 +00:00
return redirect(reverse_lazy('member:auth_token') + "?show",
permanent=True)
2020-02-17 18:25:33 +00:00
2020-02-17 22:30:55 +00:00
return super().get(request, *args, **kwargs)
2020-02-17 20:32:08 +00:00
2020-02-17 22:30:55 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2020-02-18 11:31:15 +00:00
context['token'] = Token.objects.get_or_create(
user=self.request.user)[0]
2020-02-17 18:25:33 +00:00
return context
2020-02-18 11:31:15 +00:00
2020-02-08 19:39:37 +00:00
class UserAutocomplete(autocomplete.Select2QuerySetView):
"""
2020-02-08 20:40:32 +00:00
Auto complete users by usernames
2020-02-08 19:39:37 +00:00
"""
def get_queryset(self):
2020-02-08 20:40:32 +00:00
"""
Quand une personne cherche un utilisateur par pseudo, une requête est envoyée sur l'API dédiée à l'auto-complétion.
Cette fonction récupère la requête, et renvoie la liste filtrée des utilisateurs par pseudos.
"""
# Un utilisateur non connecté n'a accès à aucune information
if not self.request.user.is_authenticated:
return User.objects.none()
2020-02-08 19:39:37 +00:00
qs = User.objects.all()
if self.q:
qs = qs.filter(username__regex=self.q)
return qs
2020-02-18 11:31:15 +00:00
2020-02-18 20:14:29 +00:00
# ******************************* #
# CLUB #
# ******************************* #
2019-08-11 22:30:29 +00:00
2020-02-18 11:31:15 +00:00
class ClubCreateView(LoginRequiredMixin, CreateView):
2019-08-11 21:25:27 +00:00
"""
Create Club
"""
model = Club
form_class = ClubForm
2020-02-18 11:31:15 +00:00
def form_valid(self, form):
2019-08-11 21:25:27 +00:00
return super().form_valid(form)
2020-02-18 11:31:15 +00:00
class ClubListView(LoginRequiredMixin, SingleTableView):
2019-08-11 21:25:27 +00:00
"""
List existing Clubs
2019-08-11 21:25:27 +00:00
"""
model = Club
2019-08-15 19:49:32 +00:00
table_class = ClubTable
2019-08-11 22:30:29 +00:00
2020-02-18 11:31:15 +00:00
class ClubDetailView(LoginRequiredMixin, DetailView):
2019-08-11 21:25:27 +00:00
model = Club
2020-02-18 11:31:15 +00:00
context_object_name = "club"
2020-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
club = context["club"]
club_transactions = \
Transaction.objects.all().filter(Q(source=club.note) | Q(destination=club.note))
2019-08-15 21:11:52 +00:00
context['history_list'] = HistoryTable(club_transactions)
club_member = \
Membership.objects.all().filter(club=club)
2019-08-15 21:11:52 +00:00
# TODO: consider only valid Membership
context['member_list'] = club_member
return context
2020-02-18 11:31:15 +00:00
class ClubAddMemberView(LoginRequiredMixin, CreateView):
model = Membership
form_class = MembershipForm
template_name = 'member/add_members.html'
2020-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['formset'] = MemberFormSet()
context['helper'] = FormSetHelper()
return context
2020-02-18 11:31:15 +00:00
def post(self, request, *args, **kwargs):
2020-02-18 20:47:03 +00:00
return
# TODO: Implement POST
# formset = MembershipFormset(request.POST)
# if formset.is_valid():
# return self.form_valid(formset)
# else:
# return self.form_invalid(formset)
2020-02-18 11:31:15 +00:00
def form_valid(self, formset):
formset.save()
return super().form_valid(formset)