views.py 3.83 KB
Newer Older
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
1
2
3
4
import os

from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
5
from django.shortcuts import get_object_or_404, render
6
from django.utils import timezone
7
from django.utils.text import slugify
8
from sendfile import sendfile
9
10
11

from documents.models import (AssociationDocumentsYear, GeneralMeeting,
                              GeneralMeetingDocument, MiscellaneousDocument)
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
12
from utils.snippets import datetime_to_lectureyear
13
14
15


def index(request):
16
17
18
    lectureyear = datetime_to_lectureyear(timezone.now())

    years = {x: None for x in range(1990, lectureyear + 1)}
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    for obj in AssociationDocumentsYear.objects.all():
        years[obj.year] = {'policy': [obj.policy_document],
                           'report': [obj.annual_report, obj.financial_report],
                           }
    for year_docs in years.values():
        if year_docs is not None:
            for docs in year_docs.values():
                # Duplicate list to prevent disrupting iteration by removing
                for doc in list(docs):
                    try:
                        doc.file
                    except ValueError:
                        docs.remove(doc)

33
    meeting_years = {x: [] for x in range(1990, lectureyear + 1)}
34
    for obj in GeneralMeeting.objects.all():
35
36
37
38
        meeting_year = datetime_to_lectureyear(obj.datetime)
        if meeting_year not in meeting_years:
            meeting_years[meeting_year] = []
        meeting_years[meeting_year].append(obj)
39

40
    context = {'miscellaneous_documents': MiscellaneousDocument.objects.all(),
41
42
43
44
               'association_documents_years': sorted(years.items(),
                                                     reverse=True),
               # TODO ideally we want to do this dynamically in CSS
               'assocation_docs_width': (220 + 20) * len(years),
45
               'meeting_years': sorted(meeting_years.items(), reverse=True)
46
47
               }
    return render(request, 'documents/index.html', context)
48
49
50
51


def get_miscellaneous_document(request, pk):
    document = get_object_or_404(MiscellaneousDocument, pk=int(pk))
52
    ext = os.path.splitext(document.file.path)[1]
53
54
    # TODO verify if we need to check a permission instead.
    # This depends on how we're dealing with ex-members.
Thom Wiggers's avatar
Thom Wiggers committed
55
    if document.members_only and not request.user.is_authenticated:
56
        raise PermissionDenied
57
58
    return sendfile(request, document.file.path, attachment=True,
                    attachment_filename=slugify(document.name) + ext)
59
60
61
62
63
64
65
66
67


# TODO verify if we need to check a permission instead.
@login_required
def get_association_document(request, document_type, year):
    documents = get_object_or_404(AssociationDocumentsYear, year=int(year))
    file = {'policy-document': documents.policy_document,
            'annual-report': documents.annual_report,
            'financial-report': documents.financial_report}[document_type]
68
    ext = os.path.splitext(file.path)[1]
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    filename = '{}-{}-{}{}'.format(year, int(year)+1, document_type, ext)
    return sendfile(request, file.path,
                    attachment=True, attachment_filename=filename)


# TODO verify if we need to check a permission instead.
@login_required
def get_general_meeting_document(request, pk, document_pk):
    document = get_object_or_404(GeneralMeetingDocument, pk=int(document_pk))
    # TODO consider if we want to format the filename differently
    return sendfile(request, document.file.path, attachment=True)


# TODO verify if we need to check a permission instead.
@login_required
def get_general_meeting_minutes(request, pk):
    meeting = get_object_or_404(GeneralMeeting, pk=int(pk))
86
    ext = os.path.splitext(meeting.minutes.path)[1]
87
88
89
    filename = '{}-minutes{}'.format(meeting.datetime.date(), ext)
    return sendfile(request, meeting.minutes.path,
                    attachment=True, attachment_filename=filename)