views.py 3.68 KB
Newer Older
Skia's avatar
Skia committed
1
from django import forms
2
from django.shortcuts import render
3
from django.views.generic import ListView, DetailView
Skia's avatar
Skia committed
4
5
from django.views.generic.edit import UpdateView, CreateView
from django.forms import CheckboxSelectMultiple
Skia's avatar
Skia committed
6
from django.core.exceptions import ValidationError
Skia's avatar
Skia committed
7

8

Skia's avatar
Skia committed
9
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
10
from club.models import Club, Membership
11
from sith.settings import SITH_MAXIMUM_FREE_ROLE, SITH_MAIN_BOARD_GROUP
12

Skia's avatar
Skia committed
13
class ClubListView(ListView):
Skia's avatar
Skia committed
14
15
16
    """
    List the Clubs
    """
17
18
19
    model = Club
    template_name = 'club/club_list.jinja'

Skia's avatar
Skia committed
20
class ClubView(DetailView):
Skia's avatar
Skia committed
21
22
23
    """
    Front page of a Club
    """
24
25
26
27
    model = Club
    pk_url_kwarg = "club_id"
    template_name = 'club/club_detail.jinja'

Skia's avatar
Skia committed
28
class ClubToolsView(CanEditMixin, DetailView):
Skia's avatar
Skia committed
29
30
31
32
33
34
35
    """
    Tools page of a Club
    """
    model = Club
    pk_url_kwarg = "club_id"
    template_name = 'club/club_tools.jinja'

Skia's avatar
Skia committed
36
class ClubMemberForm(forms.ModelForm):
Skia's avatar
Skia committed
37
38
39
    """
    Form handling the members of a club
    """
Skia's avatar
Skia committed
40
41
42
43
44
45
46
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = Membership
        fields = ['user', 'role']

    def clean(self):
Skia's avatar
Skia committed
47
48
49
50
        """
        Validates the permissions
        e.g.: the president can add anyone anywhere, but a member can not make someone become president
        """
Skia's avatar
Skia committed
51
52
        ret = super(ClubMemberForm, self).clean()
        ms = self.instance.club.get_membership_for(self._user)
53
        if (self.cleaned_data['role'] <= SITH_MAXIMUM_FREE_ROLE or
Skia's avatar
Skia committed
54
            (ms is not None and ms.role >= self.cleaned_data['role']) or
Skia's avatar
Skia committed
55
            self._user.is_in_group(SITH_MAIN_BOARD_GROUP) or
Skia's avatar
Skia committed
56
            self._user.is_superuser):
Skia's avatar
Skia committed
57
58
            return ret
        raise ValidationError("You do not have the permission to do that")
Skia's avatar
Skia committed
59

Skia's avatar
Skia committed
60
61
62
63
64
65
66
    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

Skia's avatar
Skia committed
67
class ClubMembersView(CanViewMixin, UpdateView):
Skia's avatar
Skia committed
68
69
70
    """
    View of a club's members
    """
71
72
    model = Club
    pk_url_kwarg = "club_id"
Skia's avatar
Skia committed
73
    form_class = ClubMemberForm
74
75
    template_name = 'club/club_members.jinja'

Skia's avatar
Skia committed
76
    def get_form(self):
Skia's avatar
Skia committed
77
78
79
80
        """
        Here we get a Membership object, but the view handles Club object.
        That's why the save method of ClubMemberForm is overridden.
        """
Skia's avatar
Skia committed
81
        form = super(ClubMembersView, self).get_form()
Skia's avatar
Skia committed
82
83
84
85
86
        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}
Skia's avatar
Skia committed
87
        form._user = self.request.user
Skia's avatar
Skia committed
88
89
        return form

Skia's avatar
Skia committed
90
91
92
93
94
95
96
97
98
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'

99
class ClubEditPropView(CanEditPropMixin, UpdateView):
Skia's avatar
Skia committed
100
101
102
    """
    Edit the properties of a Club object (for the Sith admins)
    """
103
104
    model = Club
    pk_url_kwarg = "club_id"
Skia's avatar
Skia committed
105
    fields = ['name', 'unix_name', 'parent']
106
    template_name = 'club/club_edit_prop.jinja'
107

Skia's avatar
Skia committed
108
109
110
111
112
113
114
115
116
117

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'