admin.py 3.7 KB
Newer Older
1
from django.contrib import admin, messages
2
3
from django.contrib.auth.models import Permission
from django.forms import BaseInlineFormSet, ModelForm
4

5
from activemembers.forms import CommitteeMembershipForm
6
from django.utils.translation import ugettext_lazy as _
7
from utils.translation import TranslatedModelAdmin
Thom Wiggers's avatar
Thom Wiggers committed
8
9
from . import models

10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
class CommitteeMembershipInlineFormSet(BaseInlineFormSet):
    """
    Solely here for performance reasons.

    Needed because the `__str__()` of `CommitteeMembership` (which is displayed
    above each inline form) uses the username, name of the member and name of
    the committee.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.queryset = self.queryset.select_related('member', 'committee')


25
26
class CommitteeMembershipInline(admin.StackedInline):
    model = models.CommitteeMembership
27
    formset = CommitteeMembershipInlineFormSet
28
29
30
    can_delete = False
    ordering = ('since',)
    extra = 0
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    # TODO: replace this with `autocomplete_fields` in Django 2.0
    raw_id_fields = ('member',)


class CommitteeForm(ModelForm):
    """
    Solely here for performance reasons.

    Needed because the `__str__()` of `Permission` (which is displayed in the
    permissions selection box) also prints the corresponding app and
    `content_type` for each permission.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['permissions'].queryset = (Permission
                                               .objects
                                               .select_related('content_type'))
49
50


Thom Wiggers's avatar
Thom Wiggers committed
51
@admin.register(models.Committee)
52
class CommitteeAdmin(TranslatedModelAdmin):
53
    inlines = (CommitteeMembershipInline,)
54
    form = CommitteeForm
55
56
    list_display = ('name', 'since', 'until', 'active', 'email')
    list_filter = ('until', 'active',)
Joren Vrancken's avatar
Joren Vrancken committed
57
    search_fields = ('name', 'description')
58
    filter_horizontal = ('permissions',)
Thom Wiggers's avatar
Thom Wiggers committed
59

60
    fields = ('name', 'description', 'photo', 'permissions', 'since',
61
62
              'until', 'contact_mailinglist', 'contact_email',
              'wiki_namespace', 'active')
63

64
65
66
67
68
69
70
    def email(self, instance):
        if instance.contact_email:
            return instance.contact_email
        elif instance.contact_mailinglist:
            return instance.contact_mailinglist.name + '@thalia.nu'
        return None

Thom Wiggers's avatar
Thom Wiggers committed
71
72
73
74
75
76
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.exclude(board__is_board=True)


@admin.register(models.Board)
77
class BoardAdmin(TranslatedModelAdmin):
78
    inlines = (CommitteeMembershipInline,)
79
    form = CommitteeForm
Thom Wiggers's avatar
Thom Wiggers committed
80
    exclude = ('is_board',)
81
    filter_horizontal = ('permissions',)
Thom Wiggers's avatar
Thom Wiggers committed
82

83
84
    fields = ('name', 'description', 'photo', 'permissions',
              'contact_mailinglist', 'contact_email', 'since', 'until',)
85

Thom Wiggers's avatar
Thom Wiggers committed
86
87

@admin.register(models.CommitteeMembership)
88
class CommitteeMembershipAdmin(TranslatedModelAdmin):
89
    form = CommitteeMembershipForm
90
    list_display = ('member', 'committee', 'since', 'until', 'chair', 'role')
91
    list_filter = ('committee',)
92
    list_select_related = ('member', 'committee',)
93
94
    search_fields = ('member__first_name', 'member__last_name',
                     'member__email')
95

96
97
98
99
100
101
102
    def changelist_view(self, request, extra_context=None):
        self.message_user(request, _('Do not edit existing memberships if the '
                                     'chair of a committee has changed, add a '
                                     'new committeemembership instead.'),
                          messages.WARNING)
        return super().changelist_view(request, extra_context)

103
104

@admin.register(models.Mentorship)
105
106
class MentorshipAdmin(admin.ModelAdmin):
    list_select_related = ('member',)