Commit 38bb8186 authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Merge branch '269-tentamen-uploaden-jaartal' into 'master'

Improve exam upload form

Closes #269

See merge request !294
parents 0f3d586a ccbad61c
import datetime
from django.conf import settings
from django.forms import (ChoiceField, DateField, ModelChoiceField,
ModelForm, SelectDateWidget)
ModelForm, SelectDateWidget,
TypedChoiceField)
from django.utils import timezone
from utils.snippets import datetime_to_lectureyear
from .models import Course, Exam, Summary
class AddExamForm(ModelForm):
exam_date = DateField(widget=SelectDateWidget())
this_year = datetime.date.today().year
years = list(reversed(range(this_year - 8, this_year + 1)))
exam_date = DateField(
widget=SelectDateWidget(years=years),
initial=datetime.date.today
)
course = ModelChoiceField(
queryset=Course.objects.order_by('name_' + settings.LANGUAGE_CODE),
empty_label=None)
......@@ -22,6 +33,12 @@ class AddSummaryForm(ModelForm):
queryset=Course.objects.order_by('name_' + settings.LANGUAGE_CODE),
empty_label=None)
this_year = datetime_to_lectureyear(timezone.now())
years = reversed([(x, "{} - {}".format(x, x + 1)) for x in
range(this_year - 20, this_year + 1)])
year = TypedChoiceField(choices=years, coerce=int, empty_value=this_year)
class Meta:
model = Summary
fields = ('name', 'year', 'file', 'course', 'author')
......@@ -13,7 +13,7 @@
</div>
{% endif %}
<form method="post" class="form-horizontal span8 offset2">
<form method="post" enctype="multipart/form-data" class="form-horizontal span8 offset2">
{% csrf_token %}
{% for field in form %}
......@@ -36,4 +36,4 @@
<input type="submit" value="{% trans 'submit'|capfirst %}" class="btn btn-style1 pull-right login" />
</form>
{% endblock %}
\ No newline at end of file
{% endblock %}
import itertools
import os
from datetime import datetime
from datetime import datetime, date
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render
......@@ -81,13 +81,12 @@ def submit_exam(request, id=None):
obj.save()
form = AddExamForm()
form.exam_date = datetime.now()
else:
obj = Exam()
obj.exam_date = date.today()
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})
......@@ -98,22 +97,21 @@ def submit_summary(request, id=None):
saved = False
if request.POST:
form = AddSummaryForm(request.POST)
form = AddSummaryForm(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()
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)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment