Sith/club/views.py

174 lines
5.6 KiB
Python
Raw Normal View History

2016-02-04 07:59:03 +00:00
from django import forms
from django.shortcuts import render
from django.views.generic import ListView, DetailView
2016-02-04 07:59:03 +00:00
from django.views.generic.edit import UpdateView, CreateView
from django.forms import CheckboxSelectMultiple
2016-02-05 15:59:42 +00:00
from django.core.exceptions import ValidationError
2016-09-02 19:21:57 +00:00
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.utils import timezone
2016-02-04 07:59:03 +00:00
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
from club.models import Club, Membership
from sith.settings import SITH_MAXIMUM_FREE_ROLE, SITH_MAIN_BOARD_GROUP
2016-07-27 15:23:02 +00:00
class ClubListView(ListView):
2016-02-08 16:09:52 +00:00
"""
List the Clubs
"""
model = Club
template_name = 'club/club_list.jinja'
2016-07-27 15:23:02 +00:00
class ClubView(DetailView):
2016-02-08 16:09:52 +00:00
"""
Front page of a Club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_detail.jinja'
2016-09-02 07:23:21 +00:00
def get_context_data(self, **kwargs):
kwargs = super(ClubView, self).get_context_data(**kwargs)
kwargs['tab'] = "infos"
return kwargs
2016-07-19 17:03:16 +00:00
class ClubToolsView(CanEditMixin, DetailView):
2016-05-09 09:49:01 +00:00
"""
Tools page of a Club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_tools.jinja'
2016-09-02 07:23:21 +00:00
def get_context_data(self, **kwargs):
kwargs = super(ClubToolsView, self).get_context_data(**kwargs)
kwargs['tab'] = "tools"
return kwargs
2016-02-04 07:59:03 +00:00
class ClubMemberForm(forms.ModelForm):
2016-02-08 16:09:52 +00:00
"""
Form handling the members of a club
"""
2016-02-04 07:59:03 +00:00
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = Membership
2016-09-02 07:23:21 +00:00
fields = ['user', 'role', 'description']
2016-02-04 07:59:03 +00:00
def clean(self):
2016-02-08 16:09:52 +00:00
"""
Validates the permissions
e.g.: the president can add anyone anywhere, but a member can not make someone become president
"""
2016-02-05 15:59:42 +00:00
ret = super(ClubMemberForm, self).clean()
ms = self.instance.club.get_membership_for(self._user)
if (self.cleaned_data['role'] <= SITH_MAXIMUM_FREE_ROLE or
2016-03-24 10:55:39 +00:00
(ms is not None and ms.role >= self.cleaned_data['role']) or
2016-03-31 08:36:00 +00:00
self._user.is_in_group(SITH_MAIN_BOARD_GROUP) or
2016-03-24 10:55:39 +00:00
self._user.is_superuser):
2016-02-05 15:59:42 +00:00
return ret
raise ValidationError("You do not have the permission to do that")
2016-02-04 07:59:03 +00:00
2016-02-08 16:09:52 +00:00
def save(self, *args, **kwargs):
"""
Overloaded to return the club, and not to a Membership object that has no view
"""
ret = super(ClubMemberForm, self).save(*args, **kwargs)
return self.instance.club
2016-02-05 15:59:42 +00:00
class ClubMembersView(CanViewMixin, UpdateView):
2016-02-08 16:09:52 +00:00
"""
View of a club's members
"""
model = Club
pk_url_kwarg = "club_id"
2016-02-04 07:59:03 +00:00
form_class = ClubMemberForm
template_name = 'club/club_members.jinja'
2016-02-04 07:59:03 +00:00
def get_form(self):
2016-02-08 16:09:52 +00:00
"""
Here we get a Membership object, but the view handles Club object.
That's why the save method of ClubMemberForm is overridden.
"""
2016-02-05 15:59:42 +00:00
form = super(ClubMembersView, self).get_form()
2016-02-04 07:59:03 +00:00
if 'user' in form.data and form.data.get('user') != '': # Load an existing membership if possible
form.instance = Membership.objects.filter(club=self.object).filter(user=form.data.get('user')).filter(end_date=None).first()
if form.instance is None: # Instanciate a new membership
form.instance = Membership(club=self.object, user=self.request.user)
form.initial = {'user': self.request.user}
2016-02-05 15:59:42 +00:00
form._user = self.request.user
2016-02-04 07:59:03 +00:00
return form
2016-09-02 07:23:21 +00:00
def get_context_data(self, **kwargs):
kwargs = super(ClubMembersView, self).get_context_data(**kwargs)
kwargs['tab'] = "members"
return kwargs
2016-09-02 19:21:57 +00:00
class ClubOldMembersView(CanViewMixin, DetailView):
"""
Old members of a club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_old_members.jinja'
def get_context_data(self, **kwargs):
kwargs = super(ClubOldMembersView, self).get_context_data(**kwargs)
kwargs['tab'] = "elderlies"
return kwargs
2016-02-08 16:09:52 +00:00
class ClubEditView(CanEditMixin, UpdateView):
"""
Edit a Club's main informations (for the club's members)
"""
model = Club
pk_url_kwarg = "club_id"
fields = ['address']
template_name = 'club/club_edit.jinja'
2016-09-02 07:23:21 +00:00
def get_context_data(self, **kwargs):
kwargs = super(ClubEditView, self).get_context_data(**kwargs)
kwargs['tab'] = "edit"
return kwargs
class ClubEditPropView(CanEditPropMixin, UpdateView):
2016-02-08 16:09:52 +00:00
"""
Edit the properties of a Club object (for the Sith admins)
"""
model = Club
pk_url_kwarg = "club_id"
2016-04-12 08:00:15 +00:00
fields = ['name', 'unix_name', 'parent']
template_name = 'club/club_edit_prop.jinja'
2016-09-02 07:23:21 +00:00
def get_context_data(self, **kwargs):
kwargs = super(ClubEditPropView, self).get_context_data(**kwargs)
kwargs['tab'] = "props"
return kwargs
2016-05-03 10:06:03 +00:00
class ClubCreateView(CanEditPropMixin, CreateView):
"""
Create a club (for the Sith admin)
"""
model = Club
pk_url_kwarg = "club_id"
fields = ['name', 'unix_name', 'parent']
template_name = 'club/club_edit_prop.jinja'
2016-09-02 19:21:57 +00:00
class MembershipSetOldView(CanEditMixin, DetailView):
"""
Set a membership as beeing old
"""
model = Membership
pk_url_kwarg = "membership_id"
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.end_date = timezone.now()
self.object.save()
return HttpResponseRedirect(reverse('club:club_members', args=self.args, kwargs={'club_id': self.object.club.id}))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return HttpResponseRedirect(reverse('club:club_members', args=self.args, kwargs={'club_id': self.object.club.id}))