admin.py 3.25 KB
Newer Older
Thom Wiggers's avatar
Thom Wiggers committed
1
from django.contrib import admin
2
3
from django.contrib.auth.models import Permission
from django.forms import BaseInlineFormSet, ModelForm
4

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

9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
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')


24
25
class CommitteeMembershipInline(admin.StackedInline):
    model = models.CommitteeMembership
26
    formset = CommitteeMembershipInlineFormSet
27
28
29
    can_delete = False
    ordering = ('since',)
    extra = 0
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    # 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'))
48
49


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

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

63
64
65
66
67
68
69
    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
70
71
72
73
74
75
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.exclude(board__is_board=True)


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

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

Thom Wiggers's avatar
Thom Wiggers committed
85
86

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


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