Auto-calculate points and final grade when saving submission
This commit is contained in:
@@ -186,6 +186,31 @@ class SubmissionAdmin(ModelAdmin):
|
|||||||
error=str(e)
|
error=str(e)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def save_formset(self, request, form, formset, change):
|
||||||
|
instances = formset.save(commit=False)
|
||||||
|
for instance in instances:
|
||||||
|
if isinstance(instance, Answer):
|
||||||
|
if instance.is_correct and instance.points is None:
|
||||||
|
instance.points = instance.question.points
|
||||||
|
elif instance.is_correct is False and instance.points is None:
|
||||||
|
instance.points = 0
|
||||||
|
instance.save()
|
||||||
|
|
||||||
|
for obj in formset.deleted_objects:
|
||||||
|
obj.delete()
|
||||||
|
|
||||||
|
formset.save_m2m()
|
||||||
|
|
||||||
|
if formset.model == Answer:
|
||||||
|
submission = formset.instance
|
||||||
|
total_earned = sum((ans.points or 0) for ans in submission.answers.all())
|
||||||
|
total_max = sum((q.points or 1) for q in submission.assignment.questions.all())
|
||||||
|
|
||||||
|
new_grade = f"{total_earned}/{total_max}"
|
||||||
|
if submission.grade != new_grade:
|
||||||
|
submission.grade = new_grade
|
||||||
|
submission.save(update_fields=['grade'])
|
||||||
|
|
||||||
def get_queryset(self, request):
|
def get_queryset(self, request):
|
||||||
qs = super().get_queryset(request)
|
qs = super().get_queryset(request)
|
||||||
if request.user.is_superuser or request.user.role == 'ADMIN':
|
if request.user.is_superuser or request.user.role == 'ADMIN':
|
||||||
|
|||||||
Reference in New Issue
Block a user