kpsul/gestion/views.py
Aurélien Delobelle fe840f2003 Add club view / update profile view
Profile view
- Let the user see his information.
- List the clubs whose he is a member.

Profile edition view
- Renamed from previous "profile" view
- User can now change "occupation" field.

Club detail view
- Informations about a club.
- Accessible by staff members and "respos" of the club.
- List members, with subscription fee (if applicable).

Club admin
- Change memberships of clubs added.
2017-08-09 12:53:44 +02:00

150 lines
4.4 KiB
Python

"""
The common views of the different organisations.
- Authentication
- Profile
- Clubs
"""
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import AccessMixin
from django.contrib.auth.models import User
from django.contrib.auth.views import (
login as django_login, logout as django_logout
)
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic import DetailView, TemplateView
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from multi_form_view import MultiModelFormView
from .forms import ProfileForm, UserForm
from .models import Club
def login(request):
if request.user.is_authenticated():
return redirect("home")
context = {}
# Fetch the next page from the request data
if request.method == "GET" and 'next' in request.GET:
context['next'] = request.GET['next']
return render(request, "gestion/login_switch.html", context)
def login_ext(request):
if request.method == "POST" and "username" in request.POST:
try:
user = User.objects.get(username=request.POST["username"])
if user.profile.login_clipper:
return render(request, "gestion/error.html",
{"error_type": "use_clipper_login"})
if not user.has_usable_password() or user.password in ("", "!"):
return render(request, "gestion/error.html",
{"error_type": "no_password"})
except User.DoesNotExist:
pass
context = {}
# Fetch the next page from the request data
if request.method == "GET" and 'next' in request.GET:
context['next'] = request.GET['next']
if request.method == "POST" and 'next' in request.POST:
context['next'] = request.POST['next']
return django_login(request, template_name='gestion/login.html',
extra_context=context)
@login_required
def logout(request):
if request.user.profile.login_clipper:
return redirect("gestion:cas_logout")
else:
return django_logout(request)
@method_decorator(login_required, name='dispatch')
class ProfileView(TemplateView):
template_name = 'gestion/profile.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['memberships'] = self.get_memberships()
return context
def get_memberships(self):
"""Returns the clubs memberships of connected user.
A membership is an instance of 'ClubUser' model.
Returns:
List of (club, membership).
"""
qs = (
self.request.user.clubuser_set
.select_related('club')
.order_by('-is_respo', 'club__name')
)
return [(m.club, m) for m in qs]
profile = ProfileView.as_view()
@method_decorator(login_required, name='dispatch')
class EditProfileView(SuccessMessageMixin, MultiModelFormView):
template_name = 'gestion/profile_edit.html'
form_classes = {
'user': UserForm,
'profile': ProfileForm,
}
success_url = '/profile/'
success_message = _("Votre profil a été mis à jour avec succès !")
def get_objects(self):
user = self.request.user
return {
'user': user,
'profile': user.profile,
}
profile_edit = EditProfileView.as_view()
class ClubDetailView(AccessMixin, DetailView):
model = Club
def dispatch(self, request, *args, **kwargs):
if (request.user.is_authenticated and
self.get_object().is_respo(request.user)):
return super().dispatch(request, *args, **kwargs)
else:
return self.handle_no_permission()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['memberships'] = self.get_memberships()
return context
def get_memberships(self):
"""Returns the club' members with their membership.
A membership is an instance of 'ClubUser' model.
Returns:
List of (user, membership).
"""
qs = (
self.object.clubuser_set
.select_related('user')
.order_by('-is_respo', 'user__first_name')
)
print(qs)
return [(m.user, m) for m in qs]
club_detail = ClubDetailView.as_view()