1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-19 05:52:21 +00:00
nk20/apps/member/views.py

710 lines
27 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-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
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-03-07 21:28:59 +00:00
from django.db.models import Q
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
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
from django_tables2.views import SingleTableView
2020-02-17 18:25:33 +00:00
from rest_framework.authtoken.models import Token
from note.forms import ImageForm
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
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
from permission.views import ProtectQuerysetMixin
2020-03-20 17:13:34 +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):
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)
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
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-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)
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-03-03 13:25:16 +00:00
context['profile_form'] = self.profile_form(instance=context['user_object'].profile)
2020-02-03 18:25:05 +00:00
return context
def form_valid(self, 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))
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."))
return super().form_invalid(form)
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-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)
# 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
olduser = User.objects.get(pk=form.instance.pk)
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()
if olduser.email != user.email:
2020-04-06 06:58:39 +00:00
# If the user changed her/his email, then it is unvalidated and a confirmation link is sent.
user.profile.email_confirmed = False
2020-08-01 08:48:17 +00:00
user.profile.save()
user.profile.send_email_validation_link()
2020-02-03 18:25:05 +00:00
return super().form_valid(form)
def get_success_url(self, **kwargs):
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
class UserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
"""
2020-02-21 10:53:37 +00:00
Affiche les informations sur un utilisateur, sa note, ses clubs...
"""
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
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-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2020-02-25 21:55:27 +00:00
user = context['user_object']
history_list = \
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
return context
2019-08-11 22:30:29 +00:00
2020-02-18 11:31:15 +00:00
class UserListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
"""
2020-04-06 06:58:39 +00:00
Display user list, with a search bar
"""
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")}
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-07-25 15:25:57 +00:00
qs = super().get_queryset().distinct().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-04-16 21:31:36 +00:00
| Q(username__iregex="^" + pattern)
2020-04-01 18:14:16 +00:00
| Q(note__alias__name__iregex="^" + pattern)
| Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern))
)
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
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
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):
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']
# 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
image = Image.open(image_file)
2020-03-07 21:28:59 +00:00
image = image.crop((x, y, x + w, y + h))
image_clean = image.resize((settings.PIC_WIDTH,
2020-03-07 21:28:59 +00:00
settings.PIC_RATIO * settings.PIC_WIDTH),
Image.ANTIALIAS)
image_file = io.BytesIO()
2020-03-07 21:28:59 +00:00
image_clean.save(image_file, "PNG")
image_field.file = image_file
# renaming
2020-03-07 16:58:41 +00:00
filename = "{}_pic.png".format(self.object.note.pk)
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
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):
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)
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
class ClubCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
2019-08-11 21:25:27 +00:00
"""
Create Club
"""
model = Club
form_class = ClubForm
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
class ClubListView(ProtectQuerysetMixin, 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
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
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")}
2020-02-18 11:31:15 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2020-03-31 21:54:14 +00:00
club = context["club"]
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))\
.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-")
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)
return context
2020-03-27 13:19:55 +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
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
class ClubUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
2020-04-06 06:58:39 +00:00
"""
Update the information of a club.
"""
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
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})
class ClubPictureUpdateView(PictureUpdateView):
2020-04-06 06:58:39 +00:00
"""
Update the profile picture of a club.
"""
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
class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
2020-04-06 06:58:39 +00:00
"""
Add a membership to a club.
"""
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):
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"))\
.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-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.
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-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
form.fields['user'].initial = user
form.fields['user'].disabled = True
form.fields['date_start'].initial = old_membership.date_end + timedelta(days=1)
form.fields['credit_amount'].initial = club.membership_fee_paid if user.profile.paid \
else club.membership_fee_unpaid
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
if club.name != "BDE" or user.profile.soge:
del form.fields['soge']
else:
fee = 0
bde = Club.objects.get(name="BDE")
fee += bde.membership_fee_paid if user.profile.paid else bde.membership_fee_unpaid
kfet = Club.objects.get(name="Kfet")
fee += kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid
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
return context
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"]
soge = form.cleaned_data["soge"] and not user.profile.soge and club.name == "BDE"
# 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.
if soge:
credit_type = None
form.instance._soge = True
2020-04-05 16:37:04 +00:00
if credit_type is None:
credit_amount = 0
if user.profile.paid:
fee = club.membership_fee_paid
else:
fee = club.membership_fee_unpaid
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",
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.
# Club 2 = Kfet (hard-code :'( )
# 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 01:42:19 +00:00
if club.parent_club is not None:
if not Membership.objects.filter(user=form.instance.user, club=club.parent_club).exists():
form.add_error('user', _('User is not a member of the parent club') + ' ' + club.parent_club.name)
return super().form_invalid(form)
if Membership.objects.filter(
user=form.instance.user,
club=club,
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:
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.
if club.name == "BDE":
# 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)
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,
)
transaction._force_save = True
transaction.save()
2020-04-05 16:37:04 +00:00
2020-04-14 02:46:52 +00:00
ret = super().form_valid(form)
member_role = Role.objects.filter(name="Membre de club").all()
form.instance.roles.set(member_role)
form.instance._force_save = True
form.instance.save()
# If Société générale pays, then we assume that this is the BDE membership, and we auto-renew the
# Kfet membership.
if soge:
kfet = Club.objects.get(name="Kfet")
kfet_fee = kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid
2020-04-06 06:58:39 +00:00
# Get current membership, to get the end date
old_membership = Membership.objects.filter(
club__name="Kfet",
user=user,
date_start__lte=datetime.today(),
date_end__gte=datetime.today(),
)
membership = Membership(
club=kfet,
user=user,
fee=kfet_fee,
date_start=old_membership.get().date_end + timedelta(days=1)
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())
else:
membership.roles.add(Role.objects.get(name="Adhérent Kfet"))
membership.save()
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
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)
club = self.object.club
form.fields['roles'].queryset = Role.objects.filter(Q(weirole__isnull=not hasattr(club, 'weiclub'))
& (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):
return reverse_lazy('member:user_detail', kwargs={'pk': self.object.user.id})
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 15:01:52 +00:00
only_active = "only_active" not in self.request.GET or self.request.GET["only_active"] != '0'
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)
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(
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