2020-04-11 01:37:06 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-04-14 02:46:52 +00:00
|
|
|
from datetime import datetime, date
|
2020-04-11 22:06:20 +00:00
|
|
|
|
2020-04-11 21:02:12 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-04-11 22:06:20 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models import Q
|
2020-04-12 00:30:48 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-04-12 00:43:22 +00:00
|
|
|
from django.views.generic import DetailView, UpdateView, CreateView
|
2020-04-14 02:46:52 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-04-11 21:02:12 +00:00
|
|
|
from django_tables2 import SingleTableView
|
2020-04-12 00:43:22 +00:00
|
|
|
from member.models import Membership, Club
|
|
|
|
from note.models import Transaction, NoteClub
|
2020-04-11 22:06:20 +00:00
|
|
|
from note.tables import HistoryTable
|
|
|
|
from permission.backends import PermissionBackend
|
2020-04-11 21:02:12 +00:00
|
|
|
from permission.views import ProtectQuerysetMixin
|
2020-04-11 01:37:06 +00:00
|
|
|
|
2020-04-16 22:48:54 +00:00
|
|
|
from .models import WEIClub, WEIRegistration, WEIMembership, Bus, BusTeam, WEIRole
|
2020-04-14 01:41:26 +00:00
|
|
|
from .forms import WEIForm, WEIRegistrationForm, BusForm, BusTeamForm, WEIMembershipForm
|
2020-04-13 19:16:52 +00:00
|
|
|
from .tables import WEITable, WEIRegistrationTable, BusTable, BusTeamTable, WEIMembershipTable
|
2020-04-11 21:02:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WEIListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
|
|
|
|
"""
|
|
|
|
List existing WEI
|
|
|
|
"""
|
|
|
|
model = WEIClub
|
|
|
|
table_class = WEITable
|
2020-04-11 22:06:20 +00:00
|
|
|
|
|
|
|
|
2020-04-12 00:43:22 +00:00
|
|
|
class WEICreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
|
|
|
"""
|
|
|
|
Create WEI
|
|
|
|
"""
|
|
|
|
model = WEIClub
|
|
|
|
form_class = WEIForm
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.requires_membership = True
|
|
|
|
form.instance.parent_club = Club.objects.get(name="Kfet")
|
|
|
|
ret = super().form_valid(form)
|
|
|
|
NoteClub.objects.create(club=form.instance)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
|
|
|
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.pk})
|
|
|
|
|
|
|
|
|
2020-04-11 22:06:20 +00:00
|
|
|
class WEIDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
|
|
|
"""
|
|
|
|
View WEI information
|
|
|
|
"""
|
|
|
|
model = WEIClub
|
|
|
|
context_object_name = "club"
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
club = context["club"]
|
|
|
|
if PermissionBackend.check_perm(self.request.user, "member.change_club_membership_start", club):
|
|
|
|
club.update_membership_dates()
|
|
|
|
|
|
|
|
club_transactions = Transaction.objects.all().filter(Q(source=club.note) | Q(destination=club.note)) \
|
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Transaction, "view")).order_by('-id')
|
|
|
|
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-04-12 02:29:44 +00:00
|
|
|
|
|
|
|
club_member = WEIMembership.objects.filter(
|
2020-04-11 22:06:20 +00:00
|
|
|
club=club,
|
|
|
|
date_end__gte=datetime.today(),
|
2020-04-12 02:29:44 +00:00
|
|
|
).filter(PermissionBackend.filter_queryset(self.request.user, WEIMembership, "view"))
|
2020-04-13 19:16:52 +00:00
|
|
|
membership_table = WEIMembershipTable(data=club_member, prefix="membership-")
|
2020-04-11 22:06:20 +00:00
|
|
|
membership_table.paginate(per_page=20, page=self.request.GET.get('membership-page', 1))
|
|
|
|
context['member_list'] = membership_table
|
|
|
|
|
2020-04-13 19:16:52 +00:00
|
|
|
pre_registrations = WEIRegistration.objects.filter(
|
2020-04-16 22:48:54 +00:00
|
|
|
PermissionBackend.filter_queryset(self.request.user, WEIRegistration, "view")).filter(
|
|
|
|
membership=None,
|
|
|
|
wei=club
|
|
|
|
)
|
2020-04-12 02:29:44 +00:00
|
|
|
pre_registrations_table = WEIRegistrationTable(data=pre_registrations, prefix="pre-registration-")
|
|
|
|
pre_registrations_table.paginate(per_page=20, page=self.request.GET.get('membership-page', 1))
|
|
|
|
context['pre_registrations'] = pre_registrations_table
|
|
|
|
|
2020-04-13 03:02:16 +00:00
|
|
|
buses = Bus.objects.filter(PermissionBackend.filter_queryset(self.request.user, Bus, "view"))\
|
|
|
|
.filter(wei=self.object)
|
|
|
|
bus_table = BusTable(data=buses, prefix="bus-")
|
|
|
|
context['buses'] = bus_table
|
|
|
|
|
2020-04-11 22:06:20 +00:00
|
|
|
# Check if the user has the right to create a membership, to display the button.
|
|
|
|
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-04-12 00:30:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WEIUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
|
|
|
"""
|
|
|
|
Update the information of the WEI.
|
|
|
|
"""
|
|
|
|
model = WEIClub
|
|
|
|
context_object_name = "club"
|
|
|
|
form_class = WEIForm
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.pk})
|
2020-04-12 01:31:08 +00:00
|
|
|
|
|
|
|
|
2020-04-13 03:02:16 +00:00
|
|
|
class BusCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
|
|
|
"""
|
|
|
|
Create Bus
|
|
|
|
"""
|
|
|
|
model = Bus
|
|
|
|
form_class = BusForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["club"] = WEIClub.objects.get(pk=self.kwargs["pk"])
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
form.fields["wei"].initial = WEIClub.objects.get(pk=self.kwargs["pk"])
|
|
|
|
return form
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
2020-04-13 04:01:27 +00:00
|
|
|
return reverse_lazy("wei:manage_bus", kwargs={"pk": self.object.pk})
|
|
|
|
|
|
|
|
|
|
|
|
class BusUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
|
|
|
"""
|
|
|
|
Update Bus
|
|
|
|
"""
|
|
|
|
model = Bus
|
|
|
|
form_class = BusForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["club"] = self.object.wei
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
form.fields["wei"].disabled = True
|
|
|
|
return form
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
|
|
|
return reverse_lazy("wei:manage_bus", kwargs={"pk": self.object.pk})
|
|
|
|
|
|
|
|
|
|
|
|
class BusManageView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
|
|
|
"""
|
|
|
|
Manage Bus
|
|
|
|
"""
|
|
|
|
model = Bus
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["club"] = self.object.wei
|
|
|
|
|
|
|
|
bus = self.object
|
|
|
|
teams = BusTeam.objects.filter(PermissionBackend.filter_queryset(self.request.user, BusTeam, "view"))\
|
|
|
|
.filter(bus=bus)
|
2020-04-13 19:16:52 +00:00
|
|
|
teams_table = BusTeamTable(data=teams, prefix="team-")
|
2020-04-13 04:01:27 +00:00
|
|
|
context["teams"] = teams_table
|
|
|
|
|
2020-04-13 19:16:52 +00:00
|
|
|
memberships = WEIMembership.objects.filter(PermissionBackend.filter_queryset(
|
|
|
|
self.request.user, WEIMembership, "view")).filter(bus=bus)
|
|
|
|
memberships_table = WEIMembershipTable(data=memberships, prefix="membership-")
|
|
|
|
memberships_table.paginate(per_page=20, page=self.request.GET.get("membership-page", 1))
|
|
|
|
context["memberships"] = memberships_table
|
|
|
|
|
2020-04-13 04:01:27 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class BusTeamCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
|
|
|
"""
|
|
|
|
Create BusTeam
|
|
|
|
"""
|
|
|
|
model = BusTeam
|
|
|
|
form_class = BusTeamForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
bus = Bus.objects.get(pk=self.kwargs["pk"])
|
|
|
|
context["club"] = bus.wei
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
form.fields["bus"].initial = Bus.objects.get(pk=self.kwargs["pk"])
|
|
|
|
return form
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
|
|
|
return reverse_lazy("wei:manage_bus", kwargs={"pk": self.object.bus.pk})
|
2020-04-13 03:02:16 +00:00
|
|
|
|
|
|
|
|
2020-04-13 19:16:52 +00:00
|
|
|
class BusTeamUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
|
|
|
"""
|
|
|
|
Update Bus team
|
|
|
|
"""
|
|
|
|
model = BusTeam
|
|
|
|
form_class = BusTeamForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["club"] = self.object.bus.wei
|
|
|
|
context["bus"] = self.object.bus
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
form.fields["bus"].disabled = True
|
|
|
|
return form
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
|
|
|
return reverse_lazy("wei:manage_bus_team", kwargs={"pk": self.object.pk})
|
|
|
|
|
|
|
|
|
|
|
|
class BusTeamManageView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
|
|
|
"""
|
|
|
|
Manage Bus team
|
|
|
|
"""
|
|
|
|
model = BusTeam
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["bus"] = self.object.bus
|
|
|
|
context["club"] = self.object.bus.wei
|
|
|
|
|
|
|
|
memberships = WEIMembership.objects.filter(PermissionBackend.filter_queryset(
|
|
|
|
self.request.user, WEIMembership, "view")).filter(team=self.object)
|
|
|
|
memberships_table = WEIMembershipTable(data=memberships, prefix="membership-")
|
|
|
|
memberships_table.paginate(per_page=20, page=self.request.GET.get("membership-page", 1))
|
|
|
|
context["memberships"] = memberships_table
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2020-04-12 01:31:08 +00:00
|
|
|
class WEIRegisterView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
|
|
|
"""
|
|
|
|
Register to the WEI
|
|
|
|
"""
|
|
|
|
model = WEIRegistration
|
|
|
|
form_class = WEIRegistrationForm
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
form.fields["user"].initial = self.request.user
|
2020-04-16 22:48:54 +00:00
|
|
|
del form.fields["first_year"]
|
2020-04-12 01:31:08 +00:00
|
|
|
return form
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2020-04-12 02:29:44 +00:00
|
|
|
form.instance.wei = WEIClub.objects.get(pk=self.kwargs["wei_pk"])
|
2020-04-16 22:48:54 +00:00
|
|
|
form.instance.first_year = False
|
2020-04-12 02:29:44 +00:00
|
|
|
return super().form_valid(form)
|
2020-04-12 01:31:08 +00:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
2020-04-12 02:29:44 +00:00
|
|
|
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.wei.pk})
|
2020-04-12 02:39:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WEIUpdateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
|
|
|
"""
|
|
|
|
Update a registration for the WEI
|
|
|
|
"""
|
|
|
|
model = WEIRegistration
|
|
|
|
form_class = WEIRegistrationForm
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
del form.fields["user"]
|
|
|
|
return form
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
|
|
|
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.wei.pk})
|
2020-04-14 01:41:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
|
|
|
"""
|
|
|
|
Validate WEI Registration
|
|
|
|
"""
|
|
|
|
model = WEIMembership
|
|
|
|
form_class = WEIMembershipForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
registration = WEIRegistration.objects.get(pk=self.kwargs["pk"])
|
|
|
|
context["registration"] = registration
|
|
|
|
context["club"] = registration.wei
|
|
|
|
context["fee"] = registration.wei.membership_fee_paid if registration.user.profile.paid \
|
|
|
|
else registration.wei.membership_fee_unpaid
|
2020-04-14 02:46:52 +00:00
|
|
|
context["kfet_member"] = Membership.objects.filter(
|
|
|
|
club__name="Kfet",
|
|
|
|
user=registration.user,
|
|
|
|
date_start__lte=datetime.now().date(),
|
|
|
|
date_end__gte=datetime.now().date(),
|
|
|
|
).exists()
|
2020-04-14 01:41:26 +00:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
2020-04-16 22:48:54 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
if WEIRegistration.objects.get(pk=self.kwargs["pk"]).first_year:
|
|
|
|
del form.fields["roles"]
|
|
|
|
return form
|
|
|
|
|
2020-04-14 02:46:52 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
Create membership, check that all is good, make transactions
|
|
|
|
"""
|
|
|
|
registration = WEIRegistration.objects.get(pk=self.kwargs["pk"])
|
|
|
|
club = registration.wei
|
|
|
|
user = registration.user
|
|
|
|
|
|
|
|
membership = form.instance
|
|
|
|
membership.user = user
|
|
|
|
membership.club = club
|
|
|
|
membership.date_start = min(date.today(), club.date_start)
|
|
|
|
membership.registration = registration
|
|
|
|
|
|
|
|
if user.profile.paid:
|
|
|
|
fee = club.membership_fee_paid
|
|
|
|
else:
|
|
|
|
fee = club.membership_fee_unpaid
|
|
|
|
|
|
|
|
if not registration.soge_credit and user.note.balance < fee:
|
|
|
|
# Users must have money before registering to the WEI.
|
|
|
|
# TODO Send a notification to the user (with a mail?) to tell her/him to credit her/his note
|
|
|
|
form.add_error('bus',
|
|
|
|
_("This user don't have enough money to join this club, and can't have a negative balance."))
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
2020-04-16 22:48:54 +00:00
|
|
|
if not registration.caution_check and not registration.first_year:
|
2020-04-14 02:46:52 +00:00
|
|
|
form.add_error('bus', _("This user didn't give her/his caution check."))
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Now, all is fine, the membership can be created.
|
|
|
|
|
2020-04-16 22:48:54 +00:00
|
|
|
if registration.first_year:
|
|
|
|
membership = form.instance
|
|
|
|
membership.save()
|
|
|
|
membership.refresh_from_db()
|
|
|
|
membership.roles.set(WEIRole.objects.filter(name="1A").all())
|
|
|
|
membership.save()
|
|
|
|
|
2020-04-14 02:46:52 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
2020-04-14 01:41:26 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
self.object.refresh_from_db()
|
2020-04-14 02:46:52 +00:00
|
|
|
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.club.pk})
|