views.py 2.45 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
Skia's avatar
Skia committed
11
from subscription.views import SubscriberMixin
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

class ClubListView(CanViewMixin, ListView):
    model = Club
    template_name = 'club/club_list.jinja'

class ClubView(CanViewMixin, DetailView):
    model = Club
    pk_url_kwarg = "club_id"
    template_name = 'club/club_detail.jinja'

class ClubEditView(CanEditMixin, UpdateView):
    model = Club
    pk_url_kwarg = "club_id"
    fields = ['address']
    template_name = 'club/club_edit.jinja'

Skia's avatar
Skia committed
28
29
30
31
32
33
34
35
class ClubMemberForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = Membership
        fields = ['user', 'role']

    def clean(self):
Skia's avatar
Skia committed
36
37
38
39
40
        ret = super(ClubMemberForm, self).clean()
        ms = self.instance.club.get_membership_for(self._user)
        if ms is not None and ms.role >= self.cleaned_data['role']:
            return ret
        raise ValidationError("You do not have the permission to do that")
Skia's avatar
Skia committed
41

Skia's avatar
Skia committed
42
class ClubMembersView(CanViewMixin, UpdateView):
43
44
    model = Club
    pk_url_kwarg = "club_id"
Skia's avatar
Skia committed
45
    form_class = ClubMemberForm
46
47
    template_name = 'club/club_members.jinja'

Skia's avatar
Skia committed
48
49
50
51
52
    def __init__(self, *args, **kwargs):
        super(ClubMembersView, self).__init__(*args, **kwargs)
    # TODO FIXME: error forbidden when adding new member to club, because self.object changes to the Membership object
    # somewhere!!!

Skia's avatar
Skia committed
53
    def get_form(self):
Skia's avatar
Skia committed
54
        form = super(ClubMembersView, self).get_form()
Skia's avatar
Skia committed
55
56
57
58
59
        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
60
        form._user = self.request.user
Skia's avatar
Skia committed
61
62
        return form

63
64
65
66
67
class ClubEditPropView(CanEditPropMixin, UpdateView):
    model = Club
    pk_url_kwarg = "club_id"
    fields = ['name', 'address', 'parent']
    template_name = 'club/club_edit_prop.jinja'
68