feat: add support for TABLE_AUTO question type with table input rendering and auto-grading

This commit is contained in:
Dmitry Belan
2026-09-19 12:19:02 +03:00
parent 9ee1788692
commit 46319def76
9 changed files with 157 additions and 19 deletions
+40 -2
View File
@@ -2,6 +2,7 @@ from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required, user_passes_test
from django.utils.html import strip_tags
from .models import Assignment, Submission, Answer
import json
from .forms import AnswersForm, StudentRegistrationForm
from django.contrib.auth import login
def register(request):
@@ -84,6 +85,35 @@ def assignment_detail(request, pk):
else:
all_auto = False
max_points += 1
elif q.question_type == 'TABLE_AUTO':
rows = getattr(q, 'table_rows', 2) or 2
table_data = []
for i in range(1, rows + 1):
val1 = form.cleaned_data.get(f'answer_{q.id}_r{i}_c1', '')
val2 = form.cleaned_data.get(f'answer_{q.id}_r{i}_c2', '')
table_data.append([val1, val2])
answer_obj.text_answer = json.dumps(table_data)
if q.correct_answer:
correct_rows = q.correct_answer.split(';')
is_correct = True
if len(correct_rows) != rows:
is_correct = False
else:
for i, row in enumerate(correct_rows):
cols = row.split('|')
if len(cols) == 2:
if cols[0].strip().lower() != table_data[i][0].strip().lower() or cols[1].strip().lower() != table_data[i][1].strip().lower():
is_correct = False
break
else:
is_correct = False
break
answer_obj.is_correct = is_correct
answer_obj.points = 1 if is_correct else 0
total_points += answer_obj.points
else:
all_auto = False
max_points += 1
else:
all_auto = False
val = form.cleaned_data.get(f'file_{q.id}')
@@ -104,8 +134,16 @@ def assignment_detail(request, pk):
question_fields = []
if form:
for q in questions:
field = form[f'answer_{q.id}'] if q.question_type == 'TEXT_AUTO' else form[f'file_{q.id}']
question_fields.append((q, field))
if q.question_type == 'TEXT_AUTO':
question_fields.append((q, 'text', form[f'answer_{q.id}']))
elif q.question_type == 'TABLE_AUTO':
rows = getattr(q, 'table_rows', 2) or 2
table_fields = []
for i in range(1, rows + 1):
table_fields.append([form[f'answer_{q.id}_r{i}_c1'], form[f'answer_{q.id}_r{i}_c2']])
question_fields.append((q, 'table', table_fields))
else:
question_fields.append((q, 'file', form[f'file_{q.id}']))
return render(request, 'lms/assignment_detail.html', {
'assignment': assignment,