admin.py 2.38 KB
Newer Older
Thom Wiggers's avatar
Thom Wiggers committed
1
from django.contrib import admin
Thom Wiggers's avatar
Thom Wiggers committed
2
from django.forms import ModelForm
3

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

9

Thom Wiggers's avatar
Thom Wiggers committed
10
11
12
13
14
15
16
17
18
19
20
21
22
class CommitteeMembershipInlineForm(ModelForm):
    """
    Form for the Committee Membership inline

    Doesn't do anything fancy, but we need it for speed.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Get the related fields in advance
        self.fields['member'].queryset = Member.objects.select_related('user')


23
24
class CommitteeMembershipInline(admin.StackedInline):
    model = models.CommitteeMembership
Thom Wiggers's avatar
Thom Wiggers committed
25
    form = CommitteeMembershipInlineForm
26
27
28
29
30
    can_delete = False
    ordering = ('since',)
    extra = 0


Thom Wiggers's avatar
Thom Wiggers committed
31
@admin.register(models.Committee)
32
class CommitteeAdmin(TranslatedModelAdmin):
33
    inlines = (CommitteeMembershipInline,)
34
35
    list_display = ('name', 'since', 'until', 'active', 'email')
    list_filter = ('until', 'active',)
Joren Vrancken's avatar
Joren Vrancken committed
36
    search_fields = ('name', 'description')
37
    filter_horizontal = ('permissions',)
Thom Wiggers's avatar
Thom Wiggers committed
38

39
    fields = ('name', 'description', 'photo', 'permissions', 'since',
40
41
              'until', 'contact_mailinglist', 'contact_email',
              'wiki_namespace', 'active')
42

43
44
45
46
47
48
49
    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
50
51
52
53
54
55
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.exclude(board__is_board=True)


@admin.register(models.Board)
56
class BoardAdmin(TranslatedModelAdmin):
57
    inlines = (CommitteeMembershipInline,)
Thom Wiggers's avatar
Thom Wiggers committed
58
    exclude = ('is_board',)
59
    filter_horizontal = ('permissions',)
Thom Wiggers's avatar
Thom Wiggers committed
60

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

Thom Wiggers's avatar
Thom Wiggers committed
64
65

@admin.register(models.CommitteeMembership)
66
class CommitteeMembershipAdmin(TranslatedModelAdmin):
67
    form = CommitteeMembershipForm
68
    list_display = ('member', 'committee', 'since', 'until', 'chair', 'role')
69
70
71
    list_filter = ('committee',)
    search_fields = ('member__user__first_name', 'member__user__last_name',
                     'member__user__email')
72
73
74
75
76


@admin.register(models.Mentorship)
class MentorsAdmin(admin.ModelAdmin):
    pass