1
2
3
4
5
6
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
|
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.forms import inlineformset_factory
from grades.forms import GradingSystemForm, GradeRangeForm
from grades.models import GradingSystem, GradeRange
@login_required
def grading_systems(request):
user = request.user
default_grading_system = GradingSystem.objects.get(name='default')
grading_systems = GradingSystem.objects.filter(creator=user)
return render(request, 'grading_systems.html', {'default_grading_system':
default_grading_system, 'grading_systems': grading_systems})
@login_required
def add_grading_system(request, system_id=None):
user = request.user
grading_system = None
if system_id is not None:
grading_system = GradingSystem.objects.get(id=system_id)
GradeRangeFormSet = inlineformset_factory(GradingSystem, GradeRange,
GradeRangeForm, extra=0)
grade_form = GradingSystemForm(instance=grading_system)
is_default = (grading_system is not None and
grading_system.name == 'default')
if request.method == 'POST':
formset = GradeRangeFormSet(request.POST, instance=grading_system)
grade_form = GradingSystemForm(request.POST, instance=grading_system)
if grade_form.is_valid():
system = grade_form.save(commit=False)
system.creator = user
system.save()
system_id = system.id
if formset.is_valid():
formset.save()
if 'add' in request.POST:
GradeRangeFormSet = inlineformset_factory(
GradingSystem, GradeRange, GradeRangeForm, extra=1
)
formset = GradeRangeFormSet(instance=grading_system)
return render(request, 'add_grades.html',
{'formset': formset,
'grade_form': grade_form, "system_id": system_id,
'is_default': is_default}
)
|