views.py 4.03 KB
Newer Older
Jan's avatar
Jan committed
1
import itertools
2
import os
3
from datetime import datetime, date
Jan's avatar
Jan committed
4
5

from django.contrib.auth.decorators import login_required
6
from django.shortcuts import get_object_or_404, render
Jan's avatar
Jan committed
7
8
9
10
11
12
13
14
15
16
from django.utils.translation import ugettext_lazy as _
from sendfile import sendfile

from .forms import AddExamForm, AddSummaryForm
from .models import Category, Course, Exam, Summary


def courses(request):
    categories = Category.objects.all()
    objects = Course.objects.order_by('name_' + request.LANGUAGE_CODE).filter(
Luuk Scholten's avatar
Luuk Scholten committed
17
        until=None)
Jan's avatar
Jan committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    return render(request, 'education/courses.html',
                  {'courses': objects, 'categories': categories})


def course(request, id):
    obj = get_object_or_404(Course, pk=id)
    courses = list(obj.old_courses.all())
    courses.append(obj)
    items = []
    for course in courses:
        items = itertools.chain(items,
                                ({"type": "summary", "year": x.year,
                                  "name": "{} {}".format(_("Summary"),
                                                         x.name),
                                  "id": x.id,
                                  "course": course}
                                 for x in course.summary_set.filter(
                                    accepted=True)))
        items = itertools.chain(items,
                                ({"type": "exam", "year": x.year, "name":
                                    "{} {}".format(
                                        dict(Exam.EXAM_TYPES)[x.type], x.name),
                                  "id": x.id, "course": course}
                                 for x in
                                 course.exam_set.filter(accepted=True)))

    return render(request, 'education/course.html',
                  {'course': obj, 'items': items})


Nienke's avatar
Nienke committed
48
49
50
51
def student_participation(request):
    return render(request, 'education/student_participation.html')


Jan's avatar
Jan committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@login_required
def exam(request, id):
    exam = get_object_or_404(Exam, id=int(id))
    ext = os.path.splitext(exam.file.path)[1]
    filename = '{}-exam{}{}'.format(exam.course.name, exam.year, ext)
    return sendfile(request, exam.file.path,
                    attachment=True, attachment_filename=filename)


@login_required
def summary(request, id):
    obj = get_object_or_404(Summary, id=int(id))
    ext = os.path.splitext(obj.file.path)[1]
    filename = '{}-summary{}{}'.format(obj.course.name, obj.year, ext)
    return sendfile(request, obj.file.path,
                    attachment=True, attachment_filename=filename)


@login_required
def submit_exam(request, id=None):
    saved = False

    if request.POST:
        form = AddExamForm(request.POST, request.FILES)
        if form.is_valid():
            saved = True
            obj = form.save(commit=False)
            obj.uploader = request.user
            obj.uploader_date = datetime.now()
            obj.save()

            form = AddExamForm()
    else:
        obj = Exam()
86
        obj.exam_date = date.today()
Jan's avatar
Jan committed
87
88
89
90
91
92
93
94
95
96
97
98
99
        if id is not None:
            obj.course = Course.objects.get(id=id)
        form = AddExamForm(instance=obj)

    return render(request, 'education/add_exam.html',
                  {'form': form, 'saved': saved})


@login_required
def submit_summary(request, id=None):
    saved = False

    if request.POST:
100
        form = AddSummaryForm(request.POST, request.FILES)
Jan's avatar
Jan committed
101
102
103
104
105
        if form.is_valid():
            saved = True
            obj = form.save(commit=False)
            obj.uploader = request.user
            obj.uploader_date = datetime.now()
106
            obj.save()
Jan's avatar
Jan committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

            obj = Summary()
            obj.author = request.user.get_full_name()
            form = AddSummaryForm(instance=obj)
    else:
        obj = Summary()
        if id is not None:
            obj.course = Course.objects.get(id=id)
        obj.author = request.user.get_full_name()
        form = AddSummaryForm(instance=obj)

    return render(request, 'education/add_summary.html',
                  {'form': form, 'saved': saved})


@login_required
def books(request):
    return render(request, 'education/books.html')