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-03-07 21:28:59 +00:00
|
|
|
import io
|
2020-04-01 16:47:56 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-03-07 21:28:59 +00:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from django.conf import settings
|
2020-07-30 10:50:48 +00:00
|
|
|
from django.contrib.auth import logout
|
2019-08-10 17:01:15 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-03-07 21:28:59 +00:00
|
|
|
from django.contrib.auth.models import User
|
2020-03-19 15:12:52 +00:00
|
|
|
from django.contrib.auth.views import LoginView
|
2020-08-06 11:07:22 +00:00
|
|
|
from django.db.models import Q, F
|
2020-04-05 03:17:28 +00:00
|
|
|
from django.shortcuts import redirect
|
2020-03-07 21:28:59 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-07-31 11:17:16 +00:00
|
|
|
from django.utils import timezone
|
2019-08-10 17:01:15 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-03-27 13:19:55 +00:00
|
|
|
from django.views.generic import CreateView, DetailView, UpdateView, TemplateView
|
2020-02-28 12:37:31 +00:00
|
|
|
from django.views.generic.edit import FormMixin
|
2019-08-15 19:49:59 +00:00
|
|
|
from django_tables2.views import SingleTableView
|
2020-02-17 18:25:33 +00:00
|
|
|
from rest_framework.authtoken.models import Token
|
2020-03-26 16:44:01 +00:00
|
|
|
from note.forms import ImageForm
|
2020-04-22 01:26:45 +00:00
|
|
|
from note.models import Alias, NoteUser
|
2020-04-05 16:37:04 +00:00
|
|
|
from note.models.transactions import Transaction, SpecialTransaction
|
2020-03-31 12:57:44 +00:00
|
|
|
from note.tables import HistoryTable, AliasTable
|
2020-07-30 10:50:48 +00:00
|
|
|
from note_kfet.middlewares import _set_current_user_and_ip
|
2020-03-20 13:43:35 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-07-25 17:40:30 +00:00
|
|
|
from permission.models import Role
|
2020-03-31 02:16:30 +00:00
|
|
|
from permission.views import ProtectQuerysetMixin
|
2020-03-20 17:13:34 +00:00
|
|
|
|
2020-07-31 07:41:22 +00:00
|
|
|
from .forms import ProfileForm, ClubForm, MembershipForm, CustomAuthenticationForm, UserForm, MembershipRolesForm
|
2020-07-25 17:40:30 +00:00
|
|
|
from .models import Club, Membership
|
2020-07-31 15:01:52 +00:00
|
|
|
from .tables import ClubTable, UserTable, MembershipTable, ClubManagerTable
|
2019-09-23 10:50:14 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-19 15:12:52 +00:00
|
|
|
class CustomLoginView(LoginView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Login view, where the user can select its permission mask.
|
|
|
|
"""
|
2020-03-19 15:12:52 +00:00
|
|
|
form_class = CustomAuthenticationForm
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2020-07-30 10:50:48 +00:00
|
|
|
logout(self.request)
|
|
|
|
_set_current_user_and_ip(form.get_user(), self.request.session, None)
|
2020-03-19 15:12:52 +00:00
|
|
|
self.request.session['permission_mask'] = form.cleaned_data['permission_mask'].rank
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Update the user information.
|
|
|
|
"""
|
2020-02-03 18:25:05 +00:00
|
|
|
model = User
|
2020-07-29 17:37:40 +00:00
|
|
|
form_class = UserForm
|
2020-02-03 18:25:05 +00:00
|
|
|
template_name = 'member/profile_update.html'
|
2020-02-27 19:56:06 +00:00
|
|
|
context_object_name = 'user_object'
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Update Profile")}
|
2020-07-29 17:37:40 +00:00
|
|
|
|
2020-03-03 13:25:16 +00:00
|
|
|
profile_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)
|
2020-04-05 02:26:42 +00:00
|
|
|
|
|
|
|
form = context['form']
|
|
|
|
form.fields['username'].widget.attrs.pop("autofocus", None)
|
|
|
|
form.fields['first_name'].widget.attrs.update({"autofocus": "autofocus"})
|
|
|
|
form.fields['first_name'].required = True
|
|
|
|
form.fields['last_name'].required = True
|
|
|
|
form.fields['email'].required = True
|
|
|
|
form.fields['email'].help_text = _("This address must be valid.")
|
|
|
|
|
2020-08-05 12:14:51 +00:00
|
|
|
context['profile_form'] = self.profile_form(instance=context['user_object'].profile,
|
|
|
|
data=self.request.POST if self.request.POST else None)
|
2020-08-05 21:19:17 +00:00
|
|
|
if not self.object.profile.report_frequency:
|
|
|
|
del context['profile_form'].fields["last_report"]
|
2020-08-05 12:14:51 +00:00
|
|
|
|
2020-02-03 18:25:05 +00:00
|
|
|
return context
|
|
|
|
|
2020-04-05 02:26:42 +00:00
|
|
|
def form_valid(self, form):
|
2020-02-17 10:21:05 +00:00
|
|
|
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))
|
2020-02-27 19:56:06 +00:00
|
|
|
if note.exists() and note.get().user != self.object:
|
2020-02-18 11:31:15 +00:00
|
|
|
form.add_error('username',
|
|
|
|
_("An alias with a similar name already exists."))
|
2020-04-05 02:26:42 +00:00
|
|
|
return super().form_invalid(form)
|
2020-02-17 10:21:05 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
profile_form = ProfileForm(
|
|
|
|
data=self.request.POST,
|
2020-02-27 19:56:06 +00:00
|
|
|
instance=self.object.profile,
|
2020-02-18 11:31:15 +00:00
|
|
|
)
|
2020-08-05 12:14:51 +00:00
|
|
|
profile_form.full_clean()
|
|
|
|
if not profile_form.is_valid():
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
|
|
|
new_username = form.data['username']
|
|
|
|
alias = Alias.objects.filter(name=new_username)
|
|
|
|
# Si le nouveau pseudo n'est pas un de nos alias,
|
|
|
|
# on supprime éventuellement un alias similaire pour le remplacer
|
|
|
|
if not alias.exists():
|
|
|
|
similar = Alias.objects.filter(
|
|
|
|
normalized_name=Alias.normalize(new_username))
|
|
|
|
if similar.exists():
|
|
|
|
similar.delete()
|
|
|
|
|
|
|
|
olduser = User.objects.get(pk=form.instance.pk)
|
|
|
|
|
|
|
|
user = form.save(commit=False)
|
|
|
|
profile = profile_form.save(commit=False)
|
|
|
|
profile.user = user
|
|
|
|
profile.save()
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
if olduser.email != user.email:
|
|
|
|
# If the user changed her/his email, then it is unvalidated and a confirmation link is sent.
|
|
|
|
user.profile.email_confirmed = False
|
|
|
|
user.profile.save()
|
|
|
|
user.profile.send_email_validation_link()
|
2020-04-05 02:37:29 +00:00
|
|
|
|
2020-02-03 18:25:05 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2020-04-05 06:01:51 +00:00
|
|
|
url = 'member:user_detail' if self.object.profile.registration_valid else 'registration:future_user_detail'
|
|
|
|
return reverse_lazy(url, args=(self.object.id,))
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-02-03 18:25:05 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class UserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
2019-09-23 10:54:08 +00:00
|
|
|
"""
|
2020-02-21 10:53:37 +00:00
|
|
|
Affiche les informations sur un utilisateur, sa note, ses clubs...
|
2019-09-23 10:54:08 +00:00
|
|
|
"""
|
2020-02-25 21:55:27 +00:00
|
|
|
model = User
|
|
|
|
context_object_name = "user_object"
|
|
|
|
template_name = "member/profile_detail.html"
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Profile detail")}
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-04-05 06:01:51 +00:00
|
|
|
def get_queryset(self, **kwargs):
|
|
|
|
"""
|
|
|
|
We can't display information of a not registered user.
|
|
|
|
"""
|
2020-08-02 06:57:16 +00:00
|
|
|
return super().get_queryset().filter(profile__registration_valid=True)
|
2020-04-05 06:01:51 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2019-08-14 14:42:05 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-02-25 21:55:27 +00:00
|
|
|
user = context['user_object']
|
2019-08-15 21:11:16 +00:00
|
|
|
history_list = \
|
2020-04-22 01:26:45 +00:00
|
|
|
Transaction.objects.all().filter(Q(source=user.note) | Q(destination=user.note))\
|
2020-07-25 15:25:57 +00:00
|
|
|
.order_by("-created_at")\
|
2020-03-30 23:03:30 +00:00
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Transaction, "view"))
|
2020-04-06 17:51:39 +00:00
|
|
|
history_table = HistoryTable(history_list, prefix='transaction-')
|
|
|
|
history_table.paginate(per_page=20, page=self.request.GET.get("transaction-page", 1))
|
|
|
|
context['history_list'] = history_table
|
|
|
|
|
2020-04-01 16:47:56 +00:00
|
|
|
club_list = Membership.objects.filter(user=user, date_end__gte=datetime.today())\
|
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Membership, "view"))
|
2020-04-06 17:51:39 +00:00
|
|
|
membership_table = MembershipTable(data=club_list, prefix='membership-')
|
|
|
|
membership_table.paginate(per_page=10, page=self.request.GET.get("membership-page", 1))
|
|
|
|
context['club_list'] = membership_table
|
2019-08-14 14:42:05 +00:00
|
|
|
return context
|
2019-08-11 22:30:29 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class UserListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
|
2019-09-23 10:54:08 +00:00
|
|
|
"""
|
2020-04-06 06:58:39 +00:00
|
|
|
Display user list, with a search bar
|
2019-09-23 10:54:08 +00:00
|
|
|
"""
|
2019-09-23 10:50:14 +00:00
|
|
|
model = User
|
|
|
|
table_class = UserTable
|
|
|
|
template_name = 'member/user_list.html'
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Search user")}
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
def get_queryset(self, **kwargs):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Filter the user list with the given pattern.
|
|
|
|
"""
|
2020-08-06 11:07:22 +00:00
|
|
|
qs = super().get_queryset().distinct("pk").annotate(alias=F("note__alias__name"))\
|
|
|
|
.annotate(normalized_alias=F("note__alias__normalized_name"))\
|
|
|
|
.filter(profile__registration_valid=True)
|
2020-04-01 18:14:16 +00:00
|
|
|
if "search" in self.request.GET:
|
|
|
|
pattern = self.request.GET["search"]
|
|
|
|
|
|
|
|
if not pattern:
|
|
|
|
return qs.none()
|
|
|
|
|
|
|
|
qs = qs.filter(
|
|
|
|
Q(first_name__iregex=pattern)
|
|
|
|
| Q(last_name__iregex=pattern)
|
|
|
|
| Q(profile__section__iregex=pattern)
|
2020-08-06 13:21:16 +00:00
|
|
|
| Q(username__iregex=pattern)
|
|
|
|
| Q(alias__iregex=pattern)
|
|
|
|
| Q(normalized_alias__iregex=Alias.normalize(pattern))
|
2020-04-01 18:14:16 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
qs = qs.none()
|
|
|
|
|
2020-04-01 18:56:24 +00:00
|
|
|
return qs[:20]
|
2019-09-23 10:50:14 +00:00
|
|
|
|
2020-03-27 13:19:55 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ProfileAliasView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
View and manage user aliases.
|
|
|
|
"""
|
2020-02-28 12:37:31 +00:00
|
|
|
model = User
|
2020-03-03 10:05:02 +00:00
|
|
|
template_name = 'member/profile_alias.html'
|
2020-02-28 12:37:31 +00:00
|
|
|
context_object_name = 'user_object'
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Note aliases")}
|
2020-03-27 13:19:55 +00:00
|
|
|
|
2020-03-07 21:28:59 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2020-02-28 12:37:31 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-25 17:00:40 +00:00
|
|
|
note = context['object'].note
|
2020-02-28 15:12:35 +00:00
|
|
|
context["aliases"] = AliasTable(note.alias_set.all())
|
2020-02-28 12:37:31 +00:00
|
|
|
return context
|
|
|
|
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class PictureUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, FormMixin, DetailView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Update profile picture of the user note.
|
|
|
|
"""
|
2020-03-04 15:34:12 +00:00
|
|
|
form_class = ImageForm
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Update note picture")}
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-03-30 23:03:30 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-07 21:28:59 +00:00
|
|
|
context['form'] = self.form_class(self.request.POST, self.request.FILES)
|
2020-03-04 15:34:12 +00:00
|
|
|
return context
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-03-04 15:34:12 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('member:user_detail', kwargs={'pk': self.object.id})
|
|
|
|
|
2020-03-07 21:28:59 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.get_form()
|
2020-03-04 15:34:12 +00:00
|
|
|
self.object = self.get_object()
|
|
|
|
if form.is_valid():
|
|
|
|
return self.form_valid(form)
|
|
|
|
else:
|
|
|
|
print('is_invalid')
|
|
|
|
print(form)
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2020-03-07 21:28:59 +00:00
|
|
|
def form_valid(self, form):
|
2020-03-06 19:07:15 +00:00
|
|
|
image_field = form.cleaned_data['image']
|
2020-03-05 22:32:01 +00:00
|
|
|
x = form.cleaned_data['x']
|
|
|
|
y = form.cleaned_data['y']
|
|
|
|
w = form.cleaned_data['width']
|
|
|
|
h = form.cleaned_data['height']
|
2020-03-06 19:07:15 +00:00
|
|
|
# image crop and resize
|
|
|
|
image_file = io.BytesIO(image_field.read())
|
2020-03-07 21:28:59 +00:00
|
|
|
# ext = image_field.name.split('.')[-1].lower()
|
|
|
|
# TODO: support GIF format
|
2020-03-06 19:07:15 +00:00
|
|
|
image = Image.open(image_file)
|
2020-03-07 21:28:59 +00:00
|
|
|
image = image.crop((x, y, x + w, y + h))
|
2020-03-06 19:07:15 +00:00
|
|
|
image_clean = image.resize((settings.PIC_WIDTH,
|
2020-03-07 21:28:59 +00:00
|
|
|
settings.PIC_RATIO * settings.PIC_WIDTH),
|
|
|
|
Image.ANTIALIAS)
|
2020-03-06 19:07:15 +00:00
|
|
|
image_file = io.BytesIO()
|
2020-03-07 21:28:59 +00:00
|
|
|
image_clean.save(image_file, "PNG")
|
2020-03-06 19:07:15 +00:00
|
|
|
image_field.file = image_file
|
|
|
|
# renaming
|
2020-03-07 16:58:41 +00:00
|
|
|
filename = "{}_pic.png".format(self.object.note.pk)
|
2020-03-06 19:07:15 +00:00
|
|
|
image_field.name = filename
|
|
|
|
self.object.note.display_image = image_field
|
2020-03-04 15:34:12 +00:00
|
|
|
self.object.note.save()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
2020-03-25 15:58:15 +00:00
|
|
|
|
2020-03-25 15:11:44 +00:00
|
|
|
class ProfilePictureUpdateView(PictureUpdateView):
|
|
|
|
model = User
|
|
|
|
template_name = 'member/profile_picture_update.html'
|
|
|
|
context_object_name = 'user_object'
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-03-25 15:58:15 +00:00
|
|
|
|
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-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Manage auth token")}
|
2020-02-17 18:25:33 +00:00
|
|
|
|
2020-02-17 22:30:55 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2020-03-31 02:16:30 +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-03-31 02:16:30 +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-18 20:14:29 +00:00
|
|
|
# ******************************* #
|
|
|
|
# CLUB #
|
|
|
|
# ******************************* #
|
2019-08-11 22:30:29 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ClubCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
2019-08-11 21:25:27 +00:00
|
|
|
"""
|
|
|
|
Create Club
|
|
|
|
"""
|
|
|
|
model = Club
|
|
|
|
form_class = ClubForm
|
2020-03-25 15:11:44 +00:00
|
|
|
success_url = reverse_lazy('member:club_list')
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Create new club")}
|
2020-03-25 15:58:15 +00:00
|
|
|
|
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-03-26 23:40:35 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ClubListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
|
2019-08-11 21:25:27 +00:00
|
|
|
"""
|
2019-09-23 10:54:08 +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
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Search club")}
|
2019-08-11 22:30:29 +00:00
|
|
|
|
2020-06-21 20:27:32 +00:00
|
|
|
def get_queryset(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Filter the user list with the given pattern.
|
|
|
|
"""
|
2020-07-25 16:18:53 +00:00
|
|
|
qs = super().get_queryset().distinct()
|
2020-06-21 20:27:32 +00:00
|
|
|
if "search" in self.request.GET:
|
|
|
|
pattern = self.request.GET["search"]
|
|
|
|
|
|
|
|
qs = qs.filter(
|
|
|
|
Q(name__iregex=pattern)
|
|
|
|
| Q(note__alias__name__iregex="^" + pattern)
|
|
|
|
| Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern))
|
|
|
|
)
|
|
|
|
|
|
|
|
return qs
|
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ClubDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Display details of a club
|
|
|
|
"""
|
2019-08-11 21:25:27 +00:00
|
|
|
model = Club
|
2020-02-18 11:31:15 +00:00
|
|
|
context_object_name = "club"
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Club detail")}
|
2019-08-14 16:47:46 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2019-08-15 19:49:59 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-31 21:54:14 +00:00
|
|
|
|
2019-08-15 19:49:59 +00:00
|
|
|
club = context["club"]
|
2020-04-02 12:50:28 +00:00
|
|
|
if PermissionBackend.check_perm(self.request.user, "member.change_club_membership_start", club):
|
2020-03-31 21:54:14 +00:00
|
|
|
club.update_membership_dates()
|
|
|
|
|
2020-07-31 15:01:52 +00:00
|
|
|
managers = Membership.objects.filter(club=self.object, roles__name="Bureau de club")\
|
|
|
|
.order_by('user__last_name').all()
|
|
|
|
context["managers"] = ClubManagerTable(data=managers, prefix="managers-")
|
|
|
|
|
2020-03-30 23:03:30 +00:00
|
|
|
club_transactions = Transaction.objects.all().filter(Q(source=club.note) | Q(destination=club.note))\
|
2020-04-22 01:26:45 +00:00
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Transaction, "view"))\
|
2020-07-25 15:25:57 +00:00
|
|
|
.order_by('-created_at')
|
2020-04-06 17:51:39 +00:00
|
|
|
history_table = HistoryTable(club_transactions, prefix="history-")
|
|
|
|
history_table.paginate(per_page=20, page=self.request.GET.get('history-page', 1))
|
|
|
|
context['history_list'] = history_table
|
2020-03-31 21:54:14 +00:00
|
|
|
club_member = Membership.objects.filter(
|
|
|
|
club=club,
|
2020-04-01 16:47:56 +00:00
|
|
|
date_end__gte=datetime.today(),
|
|
|
|
).filter(PermissionBackend.filter_queryset(self.request.user, Membership, "view"))
|
2020-04-01 01:42:19 +00:00
|
|
|
|
2020-04-06 17:51:39 +00:00
|
|
|
membership_table = MembershipTable(data=club_member, prefix="membership-")
|
2020-07-31 11:17:16 +00:00
|
|
|
membership_table.paginate(per_page=5, page=self.request.GET.get('membership-page', 1))
|
2020-04-06 17:51:39 +00:00
|
|
|
context['member_list'] = membership_table
|
2020-04-01 01:42:19 +00:00
|
|
|
|
2020-04-06 06:58:39 +00:00
|
|
|
# Check if the user has the right to create a membership, to display the button.
|
2020-04-01 01:42:19 +00:00
|
|
|
empty_membership = Membership(
|
|
|
|
club=club,
|
|
|
|
user=User.objects.first(),
|
|
|
|
date_start=datetime.now().date(),
|
|
|
|
date_end=datetime.now().date(),
|
|
|
|
fee=0,
|
|
|
|
)
|
|
|
|
context["can_add_members"] = PermissionBackend()\
|
|
|
|
.has_perm(self.request.user, "member.add_membership", empty_membership)
|
|
|
|
|
2019-08-15 19:49:59 +00:00
|
|
|
return context
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2020-03-27 13:19:55 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ClubAliasView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Manage aliases of a club.
|
|
|
|
"""
|
2020-03-25 17:00:40 +00:00
|
|
|
model = Club
|
|
|
|
template_name = 'member/club_alias.html'
|
|
|
|
context_object_name = 'club'
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Note aliases")}
|
2020-03-27 13:19:55 +00:00
|
|
|
|
2020-03-26 16:44:01 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
note = context['object'].note
|
|
|
|
context["aliases"] = AliasTable(note.alias_set.all())
|
|
|
|
return context
|
2020-03-25 17:00:40 +00:00
|
|
|
|
2020-03-25 15:58:15 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ClubUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Update the information of a club.
|
|
|
|
"""
|
2020-03-25 15:11:44 +00:00
|
|
|
model = Club
|
|
|
|
context_object_name = "club"
|
|
|
|
form_class = ClubForm
|
|
|
|
template_name = "member/club_form.html"
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Update club")}
|
2020-03-30 23:03:30 +00:00
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
def get_queryset(self, **kwargs):
|
|
|
|
qs = super().get_queryset(**kwargs)
|
|
|
|
|
|
|
|
# Don't update a WEI club through this view
|
|
|
|
if "wei" in settings.INSTALLED_APPS:
|
|
|
|
qs = qs.filter(weiclub=None)
|
|
|
|
|
|
|
|
return qs
|
|
|
|
|
2020-03-30 23:03:30 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy("member:club_detail", kwargs={"pk": self.object.pk})
|
2020-03-25 15:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClubPictureUpdateView(PictureUpdateView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Update the profile picture of a club.
|
|
|
|
"""
|
2020-03-25 15:11:44 +00:00
|
|
|
model = Club
|
|
|
|
template_name = 'member/club_picture_update.html'
|
|
|
|
context_object_name = 'club'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('member:club_detail', kwargs={'pk': self.object.id})
|
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Add a membership to a club.
|
|
|
|
"""
|
2019-08-14 16:47:46 +00:00
|
|
|
model = Membership
|
|
|
|
form_class = MembershipForm
|
|
|
|
template_name = 'member/add_members.html'
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Add new member to the club")}
|
2020-02-18 11:31:15 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2019-08-14 16:47:46 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-04-05 19:56:56 +00:00
|
|
|
form = context['form']
|
|
|
|
|
|
|
|
if "club_pk" in self.kwargs:
|
2020-04-06 06:58:39 +00:00
|
|
|
# We create a new membership.
|
2020-04-05 19:56:56 +00:00
|
|
|
club = Club.objects.filter(PermissionBackend.filter_queryset(self.request.user, Club, "view"))\
|
2020-04-22 01:26:45 +00:00
|
|
|
.get(pk=self.kwargs["club_pk"], weiclub=None)
|
2020-04-05 19:56:56 +00:00
|
|
|
form.fields['credit_amount'].initial = club.membership_fee_paid
|
2020-04-05 20:35:56 +00:00
|
|
|
|
2020-08-04 18:04:41 +00:00
|
|
|
c = club
|
|
|
|
clubs_renewal = []
|
|
|
|
additional_fee_renewal = 0
|
|
|
|
while c.parent_club is not None:
|
|
|
|
c = c.parent_club
|
|
|
|
clubs_renewal.append(c)
|
2020-08-06 10:50:24 +00:00
|
|
|
additional_fee_renewal += c.membership_fee_paid
|
2020-08-04 18:04:41 +00:00
|
|
|
context["clubs_renewal"] = clubs_renewal
|
|
|
|
context["additional_fee_renewal"] = additional_fee_renewal
|
|
|
|
|
2020-04-06 06:58:39 +00:00
|
|
|
# If the concerned club is the BDE, then we add the option that Société générale pays the membership.
|
2020-04-05 20:35:56 +00:00
|
|
|
if club.name != "BDE":
|
|
|
|
del form.fields['soge']
|
|
|
|
else:
|
|
|
|
fee = 0
|
|
|
|
bde = Club.objects.get(name="BDE")
|
|
|
|
fee += bde.membership_fee_paid
|
|
|
|
kfet = Club.objects.get(name="Kfet")
|
|
|
|
fee += kfet.membership_fee_paid
|
|
|
|
context["total_fee"] = "{:.02f}".format(fee / 100, )
|
2020-04-05 19:56:56 +00:00
|
|
|
else:
|
2020-04-06 06:58:39 +00:00
|
|
|
# This is a renewal. Fields can be pre-completed.
|
2020-08-04 18:04:41 +00:00
|
|
|
context["renewal"] = True
|
|
|
|
|
2020-04-05 19:56:56 +00:00
|
|
|
old_membership = self.get_queryset().get(pk=self.kwargs["pk"])
|
|
|
|
club = old_membership.club
|
|
|
|
user = old_membership.user
|
2020-08-04 18:04:41 +00:00
|
|
|
|
|
|
|
c = club
|
|
|
|
clubs_renewal = []
|
|
|
|
additional_fee_renewal = 0
|
|
|
|
while c.parent_club is not None:
|
|
|
|
c = c.parent_club
|
2020-08-06 12:11:55 +00:00
|
|
|
if c.membership_start and not Membership.objects.filter(
|
2020-08-04 18:04:41 +00:00
|
|
|
club=c,
|
|
|
|
user=user,
|
|
|
|
date_start__gte=c.membership_start,
|
|
|
|
).exists():
|
|
|
|
clubs_renewal.append(c)
|
|
|
|
additional_fee_renewal += c.membership_fee_paid if user.profile.paid else c.membership_fee_unpaid
|
|
|
|
context["clubs_renewal"] = clubs_renewal
|
|
|
|
context["additional_fee_renewal"] = additional_fee_renewal
|
|
|
|
|
2020-04-05 19:56:56 +00:00
|
|
|
form.fields['user'].initial = user
|
|
|
|
form.fields['user'].disabled = True
|
|
|
|
form.fields['date_start'].initial = old_membership.date_end + timedelta(days=1)
|
2020-08-04 18:04:41 +00:00
|
|
|
form.fields['credit_amount'].initial = (club.membership_fee_paid if user.profile.paid
|
2020-08-06 10:50:24 +00:00
|
|
|
else club.membership_fee_unpaid) + additional_fee_renewal
|
2020-04-05 19:56:56 +00:00
|
|
|
form.fields['last_name'].initial = user.last_name
|
|
|
|
form.fields['first_name'].initial = user.first_name
|
|
|
|
|
2020-04-06 06:58:39 +00:00
|
|
|
# If this is a renewal of a BDE membership, Société générale can pays, if it is not yet done
|
2020-08-04 18:04:41 +00:00
|
|
|
if (club.name != "BDE" and club.name != "Kfet") or user.profile.soge:
|
2020-04-05 20:35:56 +00:00
|
|
|
del form.fields['soge']
|
|
|
|
else:
|
|
|
|
fee = 0
|
|
|
|
bde = Club.objects.get(name="BDE")
|
2020-08-04 18:04:41 +00:00
|
|
|
if not Membership.objects.filter(
|
|
|
|
club=bde,
|
|
|
|
user=user,
|
|
|
|
date_start__gte=bde.membership_start,
|
|
|
|
).exists():
|
|
|
|
fee += bde.membership_fee_paid if user.profile.paid else bde.membership_fee_unpaid
|
2020-04-05 20:35:56 +00:00
|
|
|
kfet = Club.objects.get(name="Kfet")
|
2020-08-04 18:04:41 +00:00
|
|
|
if not Membership.objects.filter(
|
|
|
|
club=kfet,
|
|
|
|
user=user,
|
|
|
|
date_start__gte=bde.membership_start,
|
|
|
|
).exists():
|
|
|
|
fee += kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid
|
2020-04-05 20:35:56 +00:00
|
|
|
context["total_fee"] = "{:.02f}".format(fee / 100, )
|
|
|
|
|
2020-03-25 16:42:54 +00:00
|
|
|
context['club'] = club
|
2020-02-21 17:28:21 +00:00
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
return context
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2020-03-31 21:54:14 +00:00
|
|
|
def form_valid(self, form):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Create membership, check that all is good, make transactions
|
|
|
|
"""
|
|
|
|
# Get the club that is concerned by the membership
|
2020-04-05 19:56:56 +00:00
|
|
|
if "club_pk" in self.kwargs:
|
|
|
|
club = Club.objects.filter(PermissionBackend.filter_queryset(self.request.user, Club, "view")) \
|
|
|
|
.get(pk=self.kwargs["club_pk"])
|
|
|
|
user = form.instance.user
|
|
|
|
else:
|
|
|
|
old_membership = self.get_queryset().get(pk=self.kwargs["pk"])
|
|
|
|
club = old_membership.club
|
|
|
|
user = old_membership.user
|
|
|
|
|
2020-03-31 21:54:14 +00:00
|
|
|
form.instance.club = club
|
2020-04-01 01:42:19 +00:00
|
|
|
|
2020-04-06 06:58:39 +00:00
|
|
|
# Get form data
|
2020-04-05 16:37:04 +00:00
|
|
|
credit_type = form.cleaned_data["credit_type"]
|
|
|
|
credit_amount = form.cleaned_data["credit_amount"]
|
|
|
|
last_name = form.cleaned_data["last_name"]
|
|
|
|
first_name = form.cleaned_data["first_name"]
|
|
|
|
bank = form.cleaned_data["bank"]
|
2020-08-04 18:04:41 +00:00
|
|
|
soge = form.cleaned_data["soge"] and not user.profile.soge and (club.name == "BDE" or club.name == "Kfet")
|
2020-04-05 20:35:56 +00:00
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
# If Société générale pays, then we store that information but the payment must be controlled by treasurers
|
|
|
|
# later. The membership transaction will be invalidated.
|
2020-04-05 20:35:56 +00:00
|
|
|
if soge:
|
2020-04-22 01:26:45 +00:00
|
|
|
credit_type = None
|
|
|
|
form.instance._soge = True
|
2020-04-05 16:37:04 +00:00
|
|
|
|
|
|
|
if credit_type is None:
|
|
|
|
credit_amount = 0
|
|
|
|
|
2020-08-04 18:04:41 +00:00
|
|
|
fee = 0
|
|
|
|
c = club
|
2020-08-06 12:11:55 +00:00
|
|
|
while c is not None and c.membership_start:
|
2020-08-04 18:04:41 +00:00
|
|
|
if not Membership.objects.filter(
|
|
|
|
club=c,
|
|
|
|
user=user,
|
|
|
|
date_start__gte=c.membership_start,
|
|
|
|
).exists():
|
|
|
|
fee += c.membership_fee_paid if user.profile.paid else c.membership_fee_unpaid
|
|
|
|
c = c.parent_club
|
|
|
|
|
2020-04-05 16:37:04 +00:00
|
|
|
if user.note.balance + credit_amount < fee and not Membership.objects.filter(
|
2020-04-01 16:47:56 +00:00
|
|
|
club__name="Kfet",
|
2020-04-01 15:00:06 +00:00
|
|
|
user=user,
|
|
|
|
date_start__lte=datetime.now().date(),
|
|
|
|
date_end__gte=datetime.now().date(),
|
|
|
|
).exists():
|
|
|
|
# Users without a valid Kfet membership can't have a negative balance.
|
|
|
|
# TODO Send a notification to the user (with a mail?) to tell her/him to credit her/his note
|
|
|
|
form.add_error('user',
|
|
|
|
_("This user don't have enough money to join this club, and can't have a negative balance."))
|
2020-04-05 14:18:56 +00:00
|
|
|
return super().form_invalid(form)
|
2020-04-01 15:00:06 +00:00
|
|
|
|
2020-04-01 01:42:19 +00:00
|
|
|
if Membership.objects.filter(
|
|
|
|
user=form.instance.user,
|
|
|
|
club=club,
|
2020-04-01 15:00:06 +00:00
|
|
|
date_start__lte=form.instance.date_start,
|
|
|
|
date_end__gte=form.instance.date_start,
|
2020-04-01 01:42:19 +00:00
|
|
|
).exists():
|
|
|
|
form.add_error('user', _('User is already a member of the club'))
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
2020-04-05 19:56:56 +00:00
|
|
|
if club.membership_start and form.instance.date_start < club.membership_start:
|
2020-04-01 01:42:19 +00:00
|
|
|
form.add_error('user', _("The membership must start after {:%m-%d-%Y}.")
|
|
|
|
.format(form.instance.club.membership_start))
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
2020-04-05 19:56:56 +00:00
|
|
|
if club.membership_end and form.instance.date_start > club.membership_end:
|
2020-04-01 02:07:55 +00:00
|
|
|
form.add_error('user', _("The membership must begin before {:%m-%d-%Y}.")
|
2020-04-01 01:42:19 +00:00
|
|
|
.format(form.instance.club.membership_start))
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
2020-04-06 06:58:39 +00:00
|
|
|
# Now, all is fine, the membership can be created.
|
|
|
|
|
2020-08-04 18:04:41 +00:00
|
|
|
if club.name == "BDE" or club.name == "Kfet":
|
2020-04-22 14:25:09 +00:00
|
|
|
# When we renew the BDE membership, we update the profile section.
|
|
|
|
# We could automate that and remove the section field from the Profile model,
|
|
|
|
# but with this way users can customize their section as they want.
|
|
|
|
user.profile.section = user.profile.section_generated
|
|
|
|
user.profile.save()
|
|
|
|
|
2020-04-06 06:58:39 +00:00
|
|
|
# Credit note before the membership is created.
|
2020-04-05 16:37:04 +00:00
|
|
|
if credit_amount > 0:
|
2020-04-05 19:56:56 +00:00
|
|
|
if not last_name or not first_name or (not bank and credit_type.special_type == "Chèque"):
|
2020-04-05 16:37:04 +00:00
|
|
|
if not last_name:
|
|
|
|
form.add_error('last_name', _("This field is required."))
|
|
|
|
if not first_name:
|
|
|
|
form.add_error('first_name', _("This field is required."))
|
2020-04-05 19:56:56 +00:00
|
|
|
if not bank and credit_type.special_type == "Chèque":
|
2020-04-05 16:37:04 +00:00
|
|
|
form.add_error('bank', _("This field is required."))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2020-07-31 07:49:43 +00:00
|
|
|
transaction = SpecialTransaction(
|
2020-04-05 16:37:04 +00:00
|
|
|
source=credit_type,
|
|
|
|
destination=user.note,
|
|
|
|
quantity=1,
|
|
|
|
amount=credit_amount,
|
|
|
|
reason="Crédit " + credit_type.special_type + " (Adhésion " + club.name + ")",
|
|
|
|
last_name=last_name,
|
|
|
|
first_name=first_name,
|
|
|
|
bank=bank,
|
|
|
|
valid=True,
|
|
|
|
)
|
2020-07-31 07:49:43 +00:00
|
|
|
transaction._force_save = True
|
|
|
|
transaction.save()
|
2020-04-05 16:37:04 +00:00
|
|
|
|
2020-08-04 18:04:41 +00:00
|
|
|
form.instance._force_renew_parent = True
|
|
|
|
|
2020-04-14 02:46:52 +00:00
|
|
|
ret = super().form_valid(form)
|
|
|
|
|
2020-08-05 10:22:35 +00:00
|
|
|
if club.name == "BDE":
|
|
|
|
member_role = Role.objects.filter(Q(name="Adhérent BDE") | Q(name="Membre de club")).all()
|
|
|
|
elif club.name == "Kfet":
|
|
|
|
member_role = Role.objects.filter(Q(name="Adhérent Kfet") | Q(name="Membre de club")).all()
|
|
|
|
else:
|
|
|
|
member_role = Role.objects.filter(name="Membre de club").all()
|
2020-07-31 07:41:22 +00:00
|
|
|
form.instance.roles.set(member_role)
|
2020-07-31 07:49:43 +00:00
|
|
|
form.instance._force_save = True
|
2020-07-31 07:41:22 +00:00
|
|
|
form.instance.save()
|
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
# If Société générale pays, then we assume that this is the BDE membership, and we auto-renew the
|
|
|
|
# Kfet membership.
|
2020-04-05 20:35:56 +00:00
|
|
|
if soge:
|
2020-08-04 18:04:41 +00:00
|
|
|
# If not already done, create BDE and Kfet memberships
|
|
|
|
bde = Club.objects.get(name="BDE")
|
2020-04-05 20:35:56 +00:00
|
|
|
kfet = Club.objects.get(name="Kfet")
|
|
|
|
|
2020-08-04 18:04:41 +00:00
|
|
|
soge_clubs = [bde, kfet]
|
|
|
|
for club in soge_clubs:
|
|
|
|
fee = club.membership_fee_paid if user.profile.paid else club.membership_fee_unpaid
|
|
|
|
|
|
|
|
# Get current membership, to get the end date
|
|
|
|
old_membership = Membership.objects.filter(
|
|
|
|
club=club,
|
|
|
|
user=user,
|
|
|
|
).order_by("-date_start")
|
|
|
|
|
|
|
|
if old_membership.filter(date_start__gte=club.membership_start).exists():
|
|
|
|
# Membership is already renewed
|
|
|
|
continue
|
|
|
|
|
|
|
|
membership = Membership(
|
|
|
|
club=club,
|
|
|
|
user=user,
|
|
|
|
fee=fee,
|
|
|
|
date_start=max(old_membership.first().date_end + timedelta(days=1), club.membership_start)
|
|
|
|
if old_membership.exists() else form.instance.date_start,
|
|
|
|
)
|
|
|
|
membership._force_save = True
|
|
|
|
membership._soge = True
|
|
|
|
membership.save()
|
|
|
|
membership.refresh_from_db()
|
|
|
|
if old_membership.exists():
|
|
|
|
membership.roles.set(old_membership.get().roles.all())
|
2020-08-05 10:22:35 +00:00
|
|
|
elif c.name == "BDE":
|
|
|
|
membership.roles.set(Role.objects.filter(Q(name="Adhérent BDE") | Q(name="Membre de club")).all())
|
|
|
|
elif c.name == "Kfet":
|
|
|
|
membership.roles.set(Role.objects.filter(Q(name="Adhérent Kfet") | Q(name="Membre de club")).all())
|
2020-08-04 18:04:41 +00:00
|
|
|
membership.save()
|
2020-04-05 20:35:56 +00:00
|
|
|
|
2020-04-14 02:46:52 +00:00
|
|
|
return ret
|
2020-04-01 01:42:19 +00:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('member:club_detail', kwargs={'pk': self.object.club.id})
|
|
|
|
|
|
|
|
|
|
|
|
class ClubManageRolesView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
Manage the roles of a user in a club
|
|
|
|
"""
|
2020-04-01 01:42:19 +00:00
|
|
|
model = Membership
|
2020-07-31 07:41:22 +00:00
|
|
|
form_class = MembershipRolesForm
|
2020-04-01 01:42:19 +00:00
|
|
|
template_name = 'member/add_members.html'
|
2020-07-30 15:30:21 +00:00
|
|
|
extra_context = {"title": _("Manage roles of an user in the club")}
|
2020-04-01 01:42:19 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
club = self.object.club
|
|
|
|
context['club'] = club
|
|
|
|
return context
|
|
|
|
|
2020-04-05 19:56:56 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
2020-07-25 17:59:04 +00:00
|
|
|
|
|
|
|
club = self.object.club
|
2020-07-30 14:36:44 +00:00
|
|
|
form.fields['roles'].queryset = Role.objects.filter(Q(weirole__isnull=not hasattr(club, 'weiclub'))
|
2020-07-25 17:59:04 +00:00
|
|
|
& (Q(for_club__isnull=True) | Q(for_club=club))).all()
|
|
|
|
|
2020-04-05 19:56:56 +00:00
|
|
|
return form
|
2020-03-31 21:54:14 +00:00
|
|
|
|
|
|
|
def get_success_url(self):
|
2020-07-30 14:36:44 +00:00
|
|
|
return reverse_lazy('member:user_detail', kwargs={'pk': self.object.user.id})
|
2020-07-31 11:17:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClubMembersListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
|
|
|
|
model = Membership
|
|
|
|
table_class = MembershipTable
|
|
|
|
template_name = "member/club_members.html"
|
|
|
|
extra_context = {"title": _("Members of the club")}
|
|
|
|
|
|
|
|
def get_queryset(self, **kwargs):
|
|
|
|
qs = super().get_queryset().filter(club_id=self.kwargs["pk"])
|
|
|
|
|
|
|
|
if 'search' in self.request.GET:
|
|
|
|
pattern = self.request.GET['search']
|
|
|
|
qs = qs.filter(
|
2020-08-01 14:07:47 +00:00
|
|
|
Q(user__first_name__iregex='^' + pattern)
|
|
|
|
| Q(user__last_name__iregex='^' + pattern)
|
|
|
|
| Q(user__note__alias__normalized_name__iregex='^' + Alias.normalize(pattern))
|
2020-07-31 11:17:16 +00:00
|
|
|
)
|
|
|
|
|
2020-07-31 15:01:52 +00:00
|
|
|
only_active = "only_active" not in self.request.GET or self.request.GET["only_active"] != '0'
|
2020-07-31 11:17:16 +00:00
|
|
|
|
|
|
|
if only_active:
|
|
|
|
qs = qs.filter(date_start__lte=timezone.now().today(), date_end__gte=timezone.now().today())
|
|
|
|
|
2020-07-31 15:01:52 +00:00
|
|
|
if "roles" in self.request.GET:
|
|
|
|
if not self.request.GET["roles"]:
|
|
|
|
return qs.none()
|
|
|
|
roles_str = self.request.GET["roles"].replace(' ', '').split(',')
|
|
|
|
roles_int = map(int, roles_str)
|
|
|
|
qs = qs.filter(roles__in=roles_int)
|
|
|
|
|
2020-07-31 11:17:16 +00:00
|
|
|
qs = qs.order_by('-date_start', 'user__username')
|
|
|
|
|
|
|
|
return qs.distinct()
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-07-31 15:01:52 +00:00
|
|
|
club = Club.objects.filter(
|
2020-07-31 11:17:16 +00:00
|
|
|
PermissionBackend.filter_queryset(self.request.user, Club, "view")
|
|
|
|
).get(pk=self.kwargs["pk"])
|
2020-07-31 15:01:52 +00:00
|
|
|
context["club"] = club
|
|
|
|
|
|
|
|
applicable_roles = Role.objects.filter(Q(weirole__isnull=not hasattr(club, 'weiclub'))
|
|
|
|
& (Q(for_club__isnull=True) | Q(for_club=club))).all()
|
|
|
|
context["applicable_roles"] = applicable_roles
|
|
|
|
|
|
|
|
context["only_active"] = "only_active" not in self.request.GET or self.request.GET["only_active"] != '0'
|
|
|
|
|
2020-08-01 14:07:47 +00:00
|
|
|
return context
|