views.py 3.29 KB
Newer Older
1
from django.shortcuts import get_object_or_404, render, redirect, reverse
2
3
import datetime
from utils.snippets import datetime_to_lectureyear
4
from utils.translation import localize_attr_name
5
from .models import Board, MemberGroup, MemberGroupMembership
Thom Wiggers's avatar
Thom Wiggers committed
6
7


8
def committee_index(request):
9
10
    """
    View that renders the committee overview page
11

12
13
14
    :param request: the request object
    :return: response containing the HTML
    """
15
    committees = MemberGroup.active_objects.all().order_by(
16
        localize_attr_name('name'))
Thom Wiggers's avatar
Thom Wiggers committed
17

18
    return render(request, 'activemembers/committee_index.html',
Thom Wiggers's avatar
Thom Wiggers committed
19
20
21
                  {'committees': committees})


22
23
24
def committee_detail(request, pk):
    """
    View that renders the page of one selected committee
25

26
27
28
29
    :param request: the request object
    :param pk: pk of the selected committee
    :return:
    """
30
    committee = get_object_or_404(MemberGroup, pk=pk)
Thom Wiggers's avatar
Thom Wiggers committed
31

Thom Wiggers's avatar
Thom Wiggers committed
32
    members = []
33
    memberships = (MemberGroupMembership
Thom Wiggers's avatar
Thom Wiggers committed
34
                   .active_memberships
35
36
                   .filter(group=committee)
                   .prefetch_related('member__membergroupmembership_set'))
Thom Wiggers's avatar
Thom Wiggers committed
37
38
39
    for membership in memberships:
        member = membership.member
        member.chair = membership.chair
40
        member.role = membership.role
41
        member.committee_since = membership.initial_connected_membership.since
Thom Wiggers's avatar
Thom Wiggers committed
42
43
        members.append(member)  # list comprehension would be more pythonic?

44
45
    members.sort(key=lambda x: x.committee_since)

46
    return render(request, 'activemembers/committee_detail.html',
Thom Wiggers's avatar
Thom Wiggers committed
47
48
                  {'committee': committee,
                   'members': members})
Thom Wiggers's avatar
Thom Wiggers committed
49
50


51
def board_index(request):
52
53
    """
    View that renders the board overview page
54

55
56
57
    :param request: the request object
    :return: response containing the HTML
    """
58
    current_year = datetime_to_lectureyear(datetime.date.today())
59
60
    board = get_object_or_404(
        Board, since__year=current_year, until__year=current_year+1)
61
    old_boards = Board.objects.all().exclude(pk=board.pk)
Thom Wiggers's avatar
Thom Wiggers committed
62
    return render(request,
63
                  'activemembers/board_index.html',
64
65
66
                  {'old_boards': old_boards,
                   'board': board
                   })
67
68


69
def board_detail(request, since, until=None):
70
71
    """
    View that renders the board for a specific lecture year
72

73
74
75
76
77
    :param request: the request object
    :param since: xxxx in xxxx-yyyy of the lecture year
    :param until: yyyy in xxxx-yyyy of the lecture year
    :return: response containing the HTML
    """
78
79
80
81
    if not until:  # try to correct /board/2016 to /2016-2017
        return redirect(reverse('activemembers:board',
                                kwargs={'since': since,
                                        'until': int(since) + 1}))
82
    board = get_object_or_404(Board, since__year=since, until__year=until)
83
    members = []
84
    memberships = (MemberGroupMembership
Joost Rijneveld's avatar
Joost Rijneveld committed
85
                   .objects
86
                   .filter(group=board)
87
88
89
90
91
92
93
94
                   .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?

95
    return render(request, 'activemembers/board_detail.html',
96
97
                  {'board': board,
                   'members': members})