views.py 3.43 KB
Newer Older
1
import datetime
2 3 4 5 6 7

from django.db.models import QuerySet
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.views.generic import ListView, DetailView

8
from utils.snippets import datetime_to_lectureyear
9
from utils.translation import localize_attr_name
Thom Wiggers's avatar
Thom Wiggers committed
10
from .models import Board, MemberGroupMembership, Committee, Society
Thom Wiggers's avatar
Thom Wiggers committed
11 12


13
class _MemberGroupDetailView(DetailView):
14
    """
15
    Base view for membergroup details
16
    """
17
    context_object_name = 'membergroup'
Thom Wiggers's avatar
Thom Wiggers committed
18

19 20
    def get_context_data(self, **kwargs) -> dict:
        context = super().get_context_data(**kwargs)
Thom Wiggers's avatar
Thom Wiggers committed
21

22 23 24 25 26 27 28 29 30 31
        memberships = (MemberGroupMembership
                       .active_objects
                       .filter(group=context['membergroup'])
                       .prefetch_related('member__membergroupmembership_set'))
        members = [{
            'member': x.member,
            'chair': x.chair,
            'role': x.role,
            'since': x.initial_connected_membership.since
        } for x in memberships]
Thom Wiggers's avatar
Thom Wiggers committed
32

33
        members.sort(key=lambda x: x['since'])
34

35 36
        context.update({'members': members})
        return context
Thom Wiggers's avatar
Thom Wiggers committed
37

Thom Wiggers's avatar
Thom Wiggers committed
38

39 40 41 42 43 44 45
class CommitteeIndexView(ListView):
    """
    View that renders the committee overview page
    """
    template_name = 'activemembers/committee_index.html'
    queryset = Committee.active_objects
    context_object_name = 'committees'
Thom Wiggers's avatar
Thom Wiggers committed
46

47 48
    def get_ordering(self) -> str:
        return localize_attr_name('name')
Thom Wiggers's avatar
Thom Wiggers committed
49

50

51
class CommitteeDetailView(_MemberGroupDetailView):
52
    """
53
    View that renders the page of one selected committee
54
    """
55 56
    template_name = 'activemembers/committee_detail.html'
    model = Committee
57

58

59 60 61
class SocietyIndexView(ListView):
    """
    View that renders the societies overview page
62
    """
63 64 65
    template_name = 'activemembers/society_index.html'
    queryset = Society.active_objects
    context_object_name = 'societies'
66

67 68
    def get_ordering(self) -> str:
        return localize_attr_name('name')
69 70


71
class SocietyDetailView(_MemberGroupDetailView):
72
    """
73 74 75 76 77
    View that renders the page of one selected society
    """
    template_name = 'activemembers/society_detail.html'
    model = Society

78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
class BoardIndexView(ListView):
    """
    View that renders the board overview page
    """
    template_name = 'activemembers/board_index.html'
    context_object_name = 'old_boards'
    current_board = None

    def get_queryset(self) -> QuerySet:
        if self.current_board:
            return Board.objects.exclude(pk=self.current_board.pk)
        return Board.objects.all()

    def get_context_data(self, **kwargs) -> dict:
        context = super().get_context_data(**kwargs)
        context.update({
            'current_board': self.current_board
        })
        return context

    def dispatch(self, request, *args, **kwargs) -> HttpResponse:
        lecture_year = datetime_to_lectureyear(datetime.date.today())
        self.current_board = Board.objects.get(
            since__year=lecture_year, until__year=lecture_year + 1)
        return super().dispatch(request, *args, **kwargs)


class BoardDetailView(_MemberGroupDetailView):
    """
    View that renders the page of one selected board
109
    """
110 111 112 113 114 115 116 117 118
    template_name = 'activemembers/board_detail.html'
    context_object_name = 'membergroup'

    def get_object(self, queryset=None) -> Board:
        return get_object_or_404(
            Board,
            since__year=self.kwargs.get('since'),
            until__year=self.kwargs.get('until')
        )