Skip to content

Commit eda2860

Browse files
committed
- Increased exam quantity in seeder to 3
- Added gradeSubmission function to views to process submission feedback
1 parent 110b76a commit eda2860

2 files changed

Lines changed: 141 additions & 63 deletions

File tree

api/management/commands/seed_db.py

Lines changed: 138 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,141 @@ def handle(self, *args, **kwargs):
1010
self.stdout.write('Seeding data...')
1111

1212
# Clear all existing data (Optional, comment this ssection out out if need be )
13-
Exam.objects.all().delete()
14-
User.objects.all().delete()
15-
self.stdout.write('Old data cleared.')
16-
17-
# Creating a studentuser for testing
18-
student, created = User.objects.get_or_create(username='student', email='student@test.com')
19-
if created:
20-
student.set_password('password123')
21-
student.save()
22-
23-
# An admin/superuser for checking the panel
24-
admin_user, created = User.objects.get_or_create(username='admin', email='admin@test.com')
25-
if created:
26-
admin_user.set_password('admin123')
27-
admin_user.is_superuser = True
28-
admin_user.is_staff = True
29-
admin_user.save()
30-
31-
# Create an Exam
32-
exam = Exam.objects.create(
33-
title='Intro to Physics',
34-
duration=datetime.timedelta(minutes=60), # 1 Hour
35-
course_name='PHY101',
36-
metadata='Mid-term assessment covering Newton\'s Laws.'
37-
)
38-
39-
# Create Exam questions
40-
# Q1: MCQ
41-
Question.objects.create(
42-
exam=exam,
43-
question_text="What is the unit of Force?",
44-
question_type='MCQ',
45-
# Storing the list of options as a dictionary
46-
options={'options': ['Newton', 'Joule', 'Pascal', 'Watt']},
47-
# Stores the correct answer in the answer key
48-
correct_answers={'answer': 'Newton'},
49-
order=1
50-
)
51-
52-
# Q2: MCQ
53-
Question.objects.create(
54-
exam=exam,
55-
question_text="Which law states F=ma?",
56-
question_type='MCQ',
57-
options={'options': ['1st Law', '2nd Law', '3rd Law']},
58-
correct_answers={'answer': '2nd Law'},
59-
order=2
60-
)
61-
62-
# Q3: Short Answer (text, no options)
63-
Question.objects.create(
64-
exam=exam,
65-
question_text="Define 'Velocity' in one sentence.",
66-
question_type='SA',
67-
options={}, # Empty cause SA
68-
correct_answers={'keywords': ['speed', 'direction', 'vector']},
69-
order=3
70-
)
71-
72-
self.stdout.write(self.style.SUCCESS('Successfully seeded database!'))
73-
self.stdout.write('Student Login: student / password123')
74-
self.stdout.write('Admin Login: admin / admin123')
13+
try:
14+
Exam.objects.all().delete()
15+
User.objects.all().delete()
16+
self.stdout.write(self.style.SUCCESS('Old data cleared successfully'))
17+
except Exception as e:
18+
print ("An error occurred while clearing database: ", e)
19+
20+
try:
21+
# Student
22+
student, created = User.objects.get_or_create(username='student', email='student@test.com')
23+
if created:
24+
student.set_password('password123')
25+
student.save()
26+
27+
# An admin/superuser for checking panel
28+
admin_user, created = User.objects.get_or_create(username='admin', email='admin@test.com')
29+
if created:
30+
admin_user.set_password('admin123')
31+
admin_user.is_superuser = True
32+
admin_user.is_staff = True
33+
admin_user.save()
34+
35+
# Create Exam
36+
exam = Exam.objects.create(
37+
title='Intro to Physics',
38+
duration=datetime.timedelta(minutes=60), # 1 Hour
39+
course_name='PHY101',
40+
metadata='Mid-term assessment covering Newton\'s Laws.'
41+
)
42+
43+
# Create Exam questions
44+
# Q1: MCQ
45+
Question.objects.create(
46+
exam=exam,
47+
question_text="What is the unit of Force?",
48+
question_type='MCQ',
49+
# Storing the list of options as a dictionary
50+
options={'options': ['Newton', 'Joule', 'Pascal', 'Watt']},
51+
# Stores the correct answer in the answer key
52+
correct_answers={'answer': 'Newton'},
53+
order=1
54+
)
55+
56+
# Q2: MCQ
57+
Question.objects.create(
58+
exam=exam,
59+
question_text="Which law states F=ma?",
60+
question_type='MCQ',
61+
options={'options': ['1st Law', '2nd Law', '3rd Law']},
62+
correct_answers={'answer': '2nd Law'},
63+
order=2
64+
)
65+
66+
# Q3: Short Answer (text, no options)
67+
Question.objects.create(
68+
exam=exam,
69+
question_text="Define 'Velocity' in one sentence.",
70+
question_type='SA',
71+
options={}, # Empty cause SA
72+
correct_answers={'keywords': ['speed', 'direction', 'vector']},
73+
order=3
74+
)
75+
76+
# Create Exam: Intro to Chemistry
77+
chem = Exam.objects.create(
78+
title='Intro to Chemistry',
79+
duration=datetime.timedelta(minutes=45),
80+
course_name='CHEM101',
81+
metadata='Foundational chemistry concepts: atoms, molecules, pH.'
82+
)
83+
84+
Question.objects.create(
85+
exam=chem,
86+
question_text="What is the chemical formula for water?",
87+
question_type='MCQ',
88+
options={'options': ['H2O', 'CO2', 'O2', 'NaCl']},
89+
correct_answers={'answer': 'H2O'},
90+
order=1
91+
)
92+
93+
Question.objects.create(
94+
exam=chem,
95+
question_text="Which pH value is acidic?",
96+
question_type='MCQ',
97+
options={'options': ['pH 3', 'pH 7', 'pH 10']},
98+
correct_answers={'answer': 'pH 3'},
99+
order=2
100+
)
101+
102+
Question.objects.create(
103+
exam=chem,
104+
question_text="Define an atom in one sentence.",
105+
question_type='SA',
106+
options={},
107+
correct_answers={'keywords': ['proton', 'neutron', 'electron', 'nucleus']},
108+
order=3
109+
)
110+
111+
# Create Exam: Calculus I
112+
calc = Exam.objects.create(
113+
title='Calculus I',
114+
duration=datetime.timedelta(minutes=90),
115+
course_name='MATH101',
116+
metadata='Limits and derivatives basics.'
117+
)
118+
119+
Question.objects.create(
120+
exam=calc,
121+
question_text="What is the derivative of sin(x)?",
122+
question_type='MCQ',
123+
options={'options': ['cos(x)', '-cos(x)', 'sin(x)', '-sin(x)']},
124+
correct_answers={'answer': 'cos(x)'},
125+
order=1
126+
)
127+
128+
Question.objects.create(
129+
exam=calc,
130+
question_text="In one sentence, what is a limit?",
131+
question_type='SA',
132+
options={},
133+
correct_answers={'keywords': ['approach', 'value']},
134+
order=2
135+
)
136+
137+
Question.objects.create(
138+
exam=calc,
139+
question_text="What is the derivative of x^2?",
140+
question_type='MCQ',
141+
options={'options': ['2x', 'x', 'x^2', '1']},
142+
correct_answers={'answer': '2x'},
143+
order=3
144+
)
145+
146+
self.stdout.write(self.style.SUCCESS('Successfully seeded database!'))
147+
self.stdout.write('Student Login: student / password123')
148+
self.stdout.write('Admin Login: admin / admin123')
149+
except Exception as e:
150+
print ("An error occurred while creating users and or exams: ", e)

api/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from rest_framework import viewsets, permissions
55
from .models import Exam, Submission
66
from .serializers import ExamSerializer, SubmissionSerializer
7+
from .services import grade_submission
78

89
class ExamViewSet(viewsets.ReadOnlyModelViewSet):
910
"""
@@ -27,4 +28,5 @@ def get_queryset(self):
2728

2829
def perform_create(self, serializer):
2930
# link submission to the actual user
30-
serializer.save(student=self.request.user)
31+
submission= serializer.save(student=self.request.user)
32+
grade_submission(submission)

0 commit comments

Comments
 (0)