views.py 2.04 KB
Newer Older
1
from django.shortcuts import get_object_or_404, render
Thom Wiggers's avatar
Thom Wiggers committed
2

3
from utils.translation import localize_attr_name
4
from .models import Board, Committee, CommitteeMembership
Thom Wiggers's avatar
Thom Wiggers committed
5
6


7
def committee_index(request):
Thom Wiggers's avatar
Thom Wiggers committed
8
    """Overview of committees"""
9
10
11

    committees = Committee.active_committees.all().order_by(
        localize_attr_name('name'))
Thom Wiggers's avatar
Thom Wiggers committed
12

13
    return render(request, 'activemembers/committee_index.html',
Thom Wiggers's avatar
Thom Wiggers committed
14
15
16
                  {'committees': committees})


17
def committee_detail(request, id):
Thom Wiggers's avatar
Thom Wiggers committed
18
    """View the details of a committee"""
19
    committee = get_object_or_404(Committee, pk=id)
Thom Wiggers's avatar
Thom Wiggers committed
20

Thom Wiggers's avatar
Thom Wiggers committed
21
22
23
    members = []
    memberships = (CommitteeMembership
                   .active_memberships
Thom Wiggers's avatar
Thom Wiggers committed
24
                   .filter(committee=committee)
25
                   .prefetch_related('member__committeemembership_set'))
Thom Wiggers's avatar
Thom Wiggers committed
26
27
28
    for membership in memberships:
        member = membership.member
        member.chair = membership.chair
29
        member.committee_since = membership.initial_connected_membership.since
Thom Wiggers's avatar
Thom Wiggers committed
30
31
        members.append(member)  # list comprehension would be more pythonic?

32
33
    members.sort(key=lambda x: x.committee_since)

34
    return render(request, 'activemembers/committee_detail.html',
Thom Wiggers's avatar
Thom Wiggers committed
35
36
                  {'committee': committee,
                   'members': members})
Thom Wiggers's avatar
Thom Wiggers committed
37
38


39
def board_index(request):
Thom Wiggers's avatar
Thom Wiggers committed
40
41
42
    boards = Board.objects.all()

    return render(request,
43
                  'activemembers/board_index.html',
44
45
46
47
                  {'boards': boards})


def board_detail(request, id):
Joost Rijneveld's avatar
Joost Rijneveld committed
48
    """View the details of a board"""
49
50
51
    board = get_object_or_404(Board, pk=id)
    members = []
    memberships = (CommitteeMembership
Joost Rijneveld's avatar
Joost Rijneveld committed
52
                   .objects
53
54
55
56
57
58
59
60
61
                   .filter(committee=board)
                   .prefetch_related('member'))

    for membership in memberships:
        member = membership.member
        member.chair = membership.chair
        member.role = membership.role
        members.append(member)  # list comprehension would be more pythonic?

62
    return render(request, 'activemembers/board_detail.html',
63
64
                  {'board': board,
                   'members': members})