1
|
|
|
from django.core.management.base import BaseCommand |
2
|
|
|
from django.contrib.auth.models import User |
3
|
|
|
from opensubmit.models import Course, Assignment, Grading, GradingScheme, Submission, SubmissionFile, SubmissionTestResult, TestMachine |
4
|
|
|
from opensubmit import settings |
5
|
|
|
from django.utils import timezone |
6
|
|
|
from django.core.files import File as DjangoFile |
7
|
|
|
from tempfile import NamedTemporaryFile |
8
|
|
|
import datetime |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def createSubmissionFile(): |
12
|
|
|
with NamedTemporaryFile(mode="wt", delete=False, prefix=settings.MEDIA_ROOT) as tmpfile: |
13
|
|
|
# Submission file |
14
|
|
|
tmpfile.write("The quick brown fox jumps over the lazy dog.") |
15
|
|
|
tmpfile.close() |
16
|
|
|
sf = SubmissionFile(attachment=DjangoFile(tmpfile.name)) |
17
|
|
|
sf.save() |
18
|
|
|
# os.remove(tmpfile.name) |
19
|
|
|
# test machine |
20
|
|
|
machine = TestMachine() |
21
|
|
|
machine.name = "Demo Test Machine" |
22
|
|
|
machine.save() |
23
|
|
|
# Test results |
24
|
|
|
val_result = SubmissionTestResult() |
25
|
|
|
val_result.submission_file = sf |
26
|
|
|
val_result.kind = SubmissionTestResult.VALIDITY_TEST |
27
|
|
|
val_result.result = "Validation test result for student" |
28
|
|
|
val_result.result_tutor = "Validation test result for tutor" |
29
|
|
|
val_result.machine = machine |
30
|
|
|
val_result.save() |
31
|
|
|
full_result = SubmissionTestResult() |
32
|
|
|
full_result.submission_file = sf |
33
|
|
|
full_result.kind = SubmissionTestResult.FULL_TEST |
34
|
|
|
full_result.result_tutor = "Full test result for tutor" |
35
|
|
|
full_result.machine = machine |
36
|
|
|
full_result.save() |
37
|
|
|
return sf |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
class Command(BaseCommand): |
41
|
|
|
help = 'Creates demo data in the installation' |
42
|
|
|
|
43
|
|
|
def handle(self, *args, **options): |
44
|
|
|
|
45
|
|
|
print("Adding demo data ...") |
46
|
|
|
|
47
|
|
|
# create demo users |
48
|
|
|
users = {} |
49
|
|
|
for name in ['demo_student', 'demo_cheater', 'demo_tutor', 'demo_owner']: |
50
|
|
|
user = User.objects.filter(username=name).delete() |
51
|
|
|
user = User.objects.create_user(username=name, |
52
|
|
|
email='[email protected]', |
53
|
|
|
password=name, |
54
|
|
|
first_name=name, |
55
|
|
|
last_name=name) |
56
|
|
|
users[name] = user |
57
|
|
|
|
58
|
|
|
# create demo grading |
59
|
|
|
Grading.objects.filter(title='passed (demo)').delete() |
60
|
|
|
passGrade = Grading(title='passed (demo)', means_passed=True) |
61
|
|
|
passGrade.save() |
62
|
|
|
Grading.objects.filter(title='failed (demo)').delete() |
63
|
|
|
failGrade = Grading(title='failed (demo)', means_passed=False) |
64
|
|
|
failGrade.save() |
65
|
|
|
GradingScheme.objects.filter( |
66
|
|
|
title='Pass/Fail Grading Scheme (Demo)').delete() |
67
|
|
|
passFailGrading = GradingScheme( |
68
|
|
|
title='Pass/Fail Grading Scheme (Demo)') |
69
|
|
|
passFailGrading.save() |
70
|
|
|
passFailGrading.gradings.add(passGrade) |
71
|
|
|
passFailGrading.gradings.add(failGrade) |
72
|
|
|
passFailGrading.save() |
73
|
|
|
|
74
|
|
|
# create demo course |
75
|
|
|
Course.objects.filter(title='Demo Course').delete() |
76
|
|
|
course = Course( |
77
|
|
|
title='Demo Course', |
78
|
|
|
active=True, |
79
|
|
|
owner=users['demo_owner'], |
80
|
|
|
homepage="http://www.open-submit.org" |
81
|
|
|
) |
82
|
|
|
course.save() |
83
|
|
|
course.tutors.add(users['demo_tutor']) |
84
|
|
|
course.participants.add(users['demo_student'].profile) |
85
|
|
|
course.participants.add(users['demo_cheater'].profile) |
86
|
|
|
|
87
|
|
|
today = timezone.now() |
88
|
|
|
last_week = today - datetime.timedelta(weeks=1) |
89
|
|
|
tomorrow = today + datetime.timedelta(days=1) |
90
|
|
|
next_week = today + datetime.timedelta(weeks=1) |
91
|
|
|
|
92
|
|
|
# create demo assignment |
93
|
|
|
ass1 = Assignment( |
94
|
|
|
title='Demo A1: Graded group work with deadline', |
95
|
|
|
course=course, |
96
|
|
|
download='http://example.org/assignments1.pdf', |
97
|
|
|
gradingScheme=passFailGrading, |
98
|
|
|
publish_at=last_week, |
99
|
|
|
soft_deadline=tomorrow, |
100
|
|
|
hard_deadline=next_week, |
101
|
|
|
has_attachment=True, |
102
|
|
|
max_authors=3 |
103
|
|
|
) |
104
|
|
|
ass1.save() |
105
|
|
|
|
106
|
|
|
# create demo assignment without grading |
107
|
|
|
ass2 = Assignment( |
108
|
|
|
title='Demo A2: Non-graded group work with deadline', |
109
|
|
|
course=course, |
110
|
|
|
download='http://example.org/assignments2.pdf', |
111
|
|
|
gradingScheme=None, |
112
|
|
|
publish_at=last_week, |
113
|
|
|
soft_deadline=tomorrow, |
114
|
|
|
hard_deadline=next_week, |
115
|
|
|
has_attachment=True, |
116
|
|
|
max_authors=3 |
117
|
|
|
) |
118
|
|
|
ass2.save() |
119
|
|
|
|
120
|
|
|
# create demo assignment without deadlines |
121
|
|
|
ass3 = Assignment( |
122
|
|
|
title='Demo A3: Graded group work without deadline, only notes', |
123
|
|
|
course=course, |
124
|
|
|
download='http://example.org/assignments1.pdf', |
125
|
|
|
gradingScheme=passFailGrading, |
126
|
|
|
publish_at=last_week, |
127
|
|
|
soft_deadline=None, |
128
|
|
|
hard_deadline=None, |
129
|
|
|
has_attachment=False, |
130
|
|
|
max_authors=3 |
131
|
|
|
) |
132
|
|
|
ass3.save() |
133
|
|
|
|
134
|
|
|
# create demo assignment without deadlines |
135
|
|
|
ass4 = Assignment( |
136
|
|
|
title='Demo A4: Graded group work with deadline being over', |
137
|
|
|
course=course, |
138
|
|
|
download='http://example.org/assignments1.pdf', |
139
|
|
|
gradingScheme=passFailGrading, |
140
|
|
|
publish_at=last_week, |
141
|
|
|
soft_deadline=last_week, |
142
|
|
|
hard_deadline=last_week, |
143
|
|
|
has_attachment=False, |
144
|
|
|
max_authors=3 |
145
|
|
|
) |
146
|
|
|
ass4.save() |
147
|
|
|
|
148
|
|
|
# create demo submission |
149
|
|
|
Submission( |
150
|
|
|
assignment=ass1, |
151
|
|
|
submitter=users['demo_student'], |
152
|
|
|
notes="Demo submission for A1.", |
153
|
|
|
state=Submission.SUBMITTED_TESTED, |
154
|
|
|
file_upload=createSubmissionFile() |
155
|
|
|
).save() |
156
|
|
|
|
157
|
|
|
# create cheater submission in course 1 |
158
|
|
|
Submission( |
159
|
|
|
assignment=ass1, |
160
|
|
|
submitter=users['demo_cheater'], |
161
|
|
|
notes="Demo duplicate submission for A1.", |
162
|
|
|
state=Submission.SUBMITTED_TESTED, |
163
|
|
|
file_upload=createSubmissionFile() |
164
|
|
|
).save() |
165
|
|
|
|