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

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
17
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
48
49
50
51
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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(
        until=0)
    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})


@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()
            form.exam_date = datetime.now()
    else:
        obj = Exam()
        if id is not None:
            obj.course = Course.objects.get(id=id)
        form = AddExamForm(instance=obj)
        form.exam_date = datetime.now()

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


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

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

            obj = Summary()
            obj.year = datetime.now().year
            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.year = datetime.now().year
        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')