2019-08-10 17:01:15 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-08-11 21:25:27 +00:00
|
|
|
from django.views.generic import CreateView, ListView, DetailView
|
2019-08-11 15:39:05 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2019-08-11 14:22:52 +00:00
|
|
|
from django.contrib.auth.forms import UserCreationForm
|
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
|
2019-08-15 19:49:59 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
from django_tables2.views import SingleTableView
|
|
|
|
|
2019-08-11 21:25:27 +00:00
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
from .models import Profile, Club, Membership
|
2020-02-02 14:42:39 +00:00
|
|
|
from .forms import SignUpForm, ProfileForm, ClubForm,MembershipForm, MemberFormSet,FormSetHelper
|
2019-09-23 10:50:14 +00:00
|
|
|
from .tables import ClubTable,UserTable
|
|
|
|
from .filters import UserFilter, UserFilterFormHelper
|
|
|
|
|
|
|
|
|
2019-08-15 19:49:59 +00:00
|
|
|
from note.models.transactions import Transaction
|
2019-08-15 21:11:52 +00:00
|
|
|
from note.tables import HistoryTable
|
2019-09-23 10:50:14 +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
|
|
|
|
"""
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2020-02-02 14:42:39 +00:00
|
|
|
form_class = SignUpForm
|
2019-08-11 14:22:52 +00:00
|
|
|
success_url = reverse_lazy('login')
|
|
|
|
template_name ='member/signup.html'
|
2020-02-02 14:42:39 +00:00
|
|
|
second_form = ProfileForm
|
2019-08-11 14:22:52 +00:00
|
|
|
|
|
|
|
def get_context_data(self,**kwargs):
|
2019-08-14 16:47:46 +00:00
|
|
|
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):
|
2020-02-02 14:42:39 +00:00
|
|
|
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
|
|
|
|
2019-08-11 22:30:29 +00:00
|
|
|
class UserDetailView(LoginRequiredMixin,DetailView):
|
2019-09-23 10:54:08 +00:00
|
|
|
"""
|
|
|
|
Affiche les informations sur un utilisateur, sa note, ses clubs ...
|
|
|
|
"""
|
2019-08-11 22:30:29 +00:00
|
|
|
model = Profile
|
2019-08-15 21:11:16 +00:00
|
|
|
context_object_name = "profile"
|
2019-08-14 14:42:05 +00:00
|
|
|
def get_context_data(slef,**kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2019-08-15 21:11:16 +00:00
|
|
|
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)
|
2019-08-14 14:42:05 +00:00
|
|
|
return context
|
2019-08-11 22:30:29 +00:00
|
|
|
|
2019-09-23 10:50:14 +00:00
|
|
|
class UserListView(LoginRequiredMixin,SingleTableView):
|
2019-09-23 10:54:08 +00:00
|
|
|
"""
|
|
|
|
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
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2019-09-23 10:50:14 +00:00
|
|
|
def get_queryset(self,**kwargs):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
self.filter = self.filter_class(self.request.GET,queryset=qs)
|
|
|
|
self.filter.form.helper = self.formhelper_class()
|
|
|
|
return self.filter.qs
|
|
|
|
|
|
|
|
def get_context_data(self,**kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["filter"] = self.filter
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
###################################
|
|
|
|
############## CLUB ###############
|
|
|
|
###################################
|
2019-08-11 22:30:29 +00:00
|
|
|
|
2019-08-11 21:25:27 +00:00
|
|
|
class ClubCreateView(LoginRequiredMixin,CreateView):
|
|
|
|
"""
|
|
|
|
Create Club
|
|
|
|
"""
|
|
|
|
model = Club
|
|
|
|
form_class = ClubForm
|
|
|
|
|
|
|
|
def form_valid(self,form):
|
|
|
|
return super().form_valid(form)
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2019-08-15 19:49:32 +00:00
|
|
|
class ClubListView(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
|
2019-08-11 22:30:29 +00:00
|
|
|
|
2019-08-11 21:25:27 +00:00
|
|
|
class ClubDetailView(LoginRequiredMixin,DetailView):
|
|
|
|
model = Club
|
2019-08-15 19:49:59 +00:00
|
|
|
context_object_name="club"
|
2019-08-14 16:47:46 +00:00
|
|
|
|
2019-08-15 19:49:59 +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)
|
2019-08-15 19:49:59 +00:00
|
|
|
club_member = \
|
|
|
|
Membership.objects.all().filter(club=club)
|
2019-08-15 21:11:52 +00:00
|
|
|
# TODO: consider only valid Membership
|
2019-08-15 19:49:59 +00:00
|
|
|
context['member_list'] = club_member
|
|
|
|
return context
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
class ClubAddMemberView(LoginRequiredMixin,CreateView):
|
|
|
|
model = Membership
|
|
|
|
form_class = MembershipForm
|
|
|
|
template_name = 'member/add_members.html'
|
|
|
|
def get_context_data(self,**kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['formset'] = MemberFormSet()
|
|
|
|
context['helper'] = FormSetHelper()
|
|
|
|
return context
|
2019-09-23 10:54:08 +00:00
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
def post(self,request,*args,**kwargs):
|
|
|
|
formset = MembershipFormset(request.POST)
|
|
|
|
if formset.is_valid():
|
|
|
|
return self.form_valid(formset)
|
|
|
|
else:
|
|
|
|
return self.form_invalid(formset)
|
|
|
|
|
|
|
|
def form_valid(self,formset):
|
|
|
|
formset.save()
|
|
|
|
return super().form_valid(formset)
|