-
Notifications
You must be signed in to change notification settings - Fork 7
/
submission.go
558 lines (469 loc) · 16.9 KB
/
submission.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// InfoMark - a platform for managing courses with
// distributing exercise sheets and testing exercise submissions
// Copyright (C) 2019 ComputerGraphics Tuebingen
// Authors: Patrick Wieschollek
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package app
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/infomark-org/infomark-backend/api/helper"
"github.com/infomark-org/infomark-backend/api/shared"
"github.com/infomark-org/infomark-backend/auth/authenticate"
"github.com/infomark-org/infomark-backend/auth/authorize"
"github.com/infomark-org/infomark-backend/model"
"github.com/infomark-org/infomark-backend/symbol"
"github.com/spf13/viper"
)
// SubmissionResource specifies Submission management handler.
type SubmissionResource struct {
Stores *Stores
}
// NewSubmissionResource create and returns a SubmissionResource.
func NewSubmissionResource(stores *Stores) *SubmissionResource {
return &SubmissionResource{
Stores: stores,
}
}
// GetFileHandler is public endpoint for
// URL: /courses/{course_id}/tasks/{task_id}/submission
// URLPARAM: course_id,integer
// URLPARAM: task_id,integer
// METHOD: get
// TAG: submissions
// RESPONSE: 200,ZipFile
// RESPONSE: 400,BadRequest
// RESPONSE: 401,Unauthenticated
// RESPONSE: 403,Unauthorized
// SUMMARY: get the zip file containing the submission of the request identity for a given task
func (rs *SubmissionResource) GetFileHandler(w http.ResponseWriter, r *http.Request) {
task := r.Context().Value(symbol.CtxKeyTask).(*model.Task)
// submission := r.Context().Value(symbol.CtxKeySubmission).(*model.Submission)
accessClaims := r.Context().Value(symbol.CtxKeyAccessClaims).(*authenticate.AccessClaims)
givenRole := r.Context().Value(symbol.CtxKeyCourseRole).(authorize.CourseRole)
submission, err := rs.Stores.Submission.GetByUserAndTask(accessClaims.LoginID, task.ID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
// students can only access their own files
if submission.UserID != accessClaims.LoginID {
if givenRole == authorize.STUDENT {
render.Render(w, r, ErrUnauthorized)
return
}
}
hnd := helper.NewSubmissionFileHandle(submission.ID)
if !hnd.Exists() {
render.Render(w, r, ErrNotFound)
return
}
if err := hnd.WriteToBody(w); err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
}
// GetCollectionHandler is public endpoint for
// URL: /courses/{course_id}/tasks/{task_id}/groups/{group_id}
// URLPARAM: course_id,integer
// URLPARAM: sheet_id,integer
// URLPARAM: task_id,integer
// URLPARAM: group_id,integer
// METHOD: get
// TAG: submissions
// RESPONSE: 200,ZipFile
// RESPONSE: 400,BadRequest
// RESPONSE: 401,Unauthenticated
// RESPONSE: 403,Unauthorized
// SUMMARY: get the path to the zip file containing all submissions for a given task and a given group if exists
func (rs *SubmissionResource) GetCollectionHandler(w http.ResponseWriter, r *http.Request) {
givenRole := r.Context().Value(symbol.CtxKeyCourseRole).(authorize.CourseRole)
if givenRole == authorize.STUDENT {
render.Render(w, r, ErrUnauthorized)
return
}
course := r.Context().Value(symbol.CtxKeyCourse).(*model.Course)
task := r.Context().Value(symbol.CtxKeyTask).(*model.Task)
var groupID int64
var err error
// try to get id from URL
if groupID, err = strconv.ParseInt(chi.URLParam(r, "group_id"), 10, 64); err != nil {
render.Render(w, r, ErrNotFound)
return
}
sheet, err := rs.Stores.Task.IdentifySheetOfTask(task.ID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
// find specific group in database
group, err := rs.Stores.Group.Get(groupID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
hnd := helper.NewSubmissionsCollectionFileHandle(course.ID, sheet.ID, task.ID, group.ID)
text := ""
if hnd.Exists() {
text = fmt.Sprintf("%s/courses/%d/tasks/%d/groups/%d/file",
viper.GetString("url"),
course.ID, task.ID, group.ID,
)
}
if err := render.Render(w, r, newRawResponse(text)); err != nil {
render.Render(w, r, ErrRender(err))
return
}
}
// GetCollectionFileHandler is public endpoint for
// URL: /courses/{course_id}/tasks/{task_id}/groups/{group_id}/file
// URLPARAM: course_id,integer
// URLPARAM: sheet_id,integer
// URLPARAM: task_id,integer
// URLPARAM: group_id,integer
// METHOD: get
// TAG: submissions
// RESPONSE: 200,ZipFile
// RESPONSE: 400,BadRequest
// RESPONSE: 401,Unauthenticated
// RESPONSE: 403,Unauthorized
// SUMMARY: get the zip file containing all submissions for a given task and a given group
func (rs *SubmissionResource) GetCollectionFileHandler(w http.ResponseWriter, r *http.Request) {
givenRole := r.Context().Value(symbol.CtxKeyCourseRole).(authorize.CourseRole)
if givenRole == authorize.STUDENT {
render.Render(w, r, ErrUnauthorized)
return
}
course := r.Context().Value(symbol.CtxKeyCourse).(*model.Course)
task := r.Context().Value(symbol.CtxKeyTask).(*model.Task)
var groupID int64
var err error
// try to get id from URL
if groupID, err = strconv.ParseInt(chi.URLParam(r, "group_id"), 10, 64); err != nil {
render.Render(w, r, ErrNotFound)
return
}
sheet, err := rs.Stores.Task.IdentifySheetOfTask(task.ID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
// find specific group in database
group, err := rs.Stores.Group.Get(groupID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
hnd := helper.NewSubmissionsCollectionFileHandle(course.ID, sheet.ID, task.ID, group.ID)
if !hnd.Exists() {
render.Render(w, r, ErrNotFound)
return
}
if err := hnd.WriteToBody(w); err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
}
// GetFileByIDHandler is public endpoint for
// URL: /courses/{course_id}/submissions/{submission_id}/file
// URLPARAM: course_id,integer
// URLPARAM: submission_id,integer
// METHOD: get
// TAG: submissions
// RESPONSE: 200,ZipFile
// RESPONSE: 400,BadRequest
// RESPONSE: 401,Unauthenticated
// RESPONSE: 403,Unauthorized
// SUMMARY: get the zip file of a specific submission
func (rs *SubmissionResource) GetFileByIDHandler(w http.ResponseWriter, r *http.Request) {
submission := r.Context().Value(symbol.CtxKeySubmission).(*model.Submission)
accessClaims := r.Context().Value(symbol.CtxKeyAccessClaims).(*authenticate.AccessClaims)
givenRole := r.Context().Value(symbol.CtxKeyCourseRole).(authorize.CourseRole)
submission, err := rs.Stores.Submission.Get(submission.ID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
// students can only access their own files
if submission.UserID != accessClaims.LoginID {
if givenRole == authorize.STUDENT {
render.Render(w, r, ErrUnauthorized)
return
}
}
hnd := helper.NewSubmissionFileHandle(submission.ID)
if !hnd.Exists() {
render.Render(w, r, ErrNotFound)
return
}
if err := hnd.WriteToBody(w); err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
}
// UploadFileHandler is public endpoint for
// URL: /courses/{course_id}/tasks/{task_id}/submission
// URLPARAM: course_id,integer
// URLPARAM: task_id,integer
// METHOD: post
// TAG: submissions
// REQUEST: zipfile
// RESPONSE: 204,NoContent
// RESPONSE: 400,BadRequest
// RESPONSE: 401,Unauthenticated
// RESPONSE: 403,Unauthorized
// SUMMARY: changes the zip file of a submission belonging to the request identity
func (rs *SubmissionResource) UploadFileHandler(w http.ResponseWriter, r *http.Request) {
course := r.Context().Value(symbol.CtxKeyCourse).(*model.Course)
task := r.Context().Value(symbol.CtxKeyTask).(*model.Task)
sheet := r.Context().Value(symbol.CtxKeySheet).(*model.Sheet)
accessClaims := r.Context().Value(symbol.CtxKeyAccessClaims).(*authenticate.AccessClaims)
course_role := r.Context().Value(symbol.CtxKeyCourseRole).(authorize.CourseRole)
if course_role == authorize.STUDENT && !PublicYet(sheet.PublishAt) {
render.Render(w, r, ErrBadRequestWithDetails(fmt.Errorf("sheet not published yet")))
return
}
if course_role == authorize.STUDENT && OverTime(sheet.DueAt) {
render.Render(w, r, ErrBadRequestWithDetails(fmt.Errorf("too late deadline was %v but now it is %v", sheet.DueAt, NowUTC())))
return
}
usedUserID := accessClaims.LoginID
if r.FormValue("user_id") != "" && course_role == authorize.ADMIN {
// admins cannot upload solutions for students even after the deadline
requested_user_id, _ := strconv.Atoi(r.FormValue("user_id"))
usedUserID = int64(requested_user_id)
}
var grade *model.Grade
defaultPublicTestLog := "submission received and will be tested"
if !task.PublicDockerImage.Valid || !helper.NewPublicTestFileHandle(task.ID).Exists() {
// .Valid == true iff not null
defaultPublicTestLog = "no unit tests for this task are available"
}
defaultPrivateTestLog := "submission received and will be tested"
if !task.PrivateDockerImage.Valid || !helper.NewPrivateTestFileHandle(task.ID).Exists() {
// .Valid == true iff not null
defaultPrivateTestLog = "no unit tests for this task are available"
}
// create submisison if not exists
submission, err := rs.Stores.Submission.GetByUserAndTask(usedUserID, task.ID)
if err != nil {
// no such submission
submission, err = rs.Stores.Submission.Create(&model.Submission{UserID: usedUserID, TaskID: task.ID})
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
// create also empty grade, which will be filled in later
grade = &model.Grade{
PublicExecutionState: 0,
PrivateExecutionState: 0,
PublicTestLog: defaultPublicTestLog,
PrivateTestLog: defaultPrivateTestLog,
PublicTestStatus: 0,
PrivateTestStatus: 0,
AcquiredPoints: 0,
Feedback: "",
TutorID: 1,
SubmissionID: submission.ID,
}
// fetch id from grade as we need it
grade, err = rs.Stores.Grade.Create(grade)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
} else {
// submission exists, we only need to get the grade
grade, err = rs.Stores.Grade.GetForSubmission(submission.ID)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
// and update the grade
grade.PublicExecutionState = 0
grade.PrivateExecutionState = 0
grade.PublicTestLog = defaultPublicTestLog
grade.PrivateTestLog = defaultPrivateTestLog
err = rs.Stores.Grade.Update(grade)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
}
// the file will be located
if _, err := helper.NewSubmissionFileHandle(submission.ID).WriteToDisk(r, "file_data"); err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
sha256, err := helper.NewSubmissionFileHandle(submission.ID).Sha256()
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
// enqueue file into testing queue
// By definition user with id 1 is the system itself with root access
tokenManager, err := authenticate.NewTokenAuth()
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
accessToken, err := tokenManager.CreateAccessJWT(
authenticate.NewAccessClaims(1, true))
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
if task.PublicDockerImage.Valid && helper.NewPublicTestFileHandle(task.ID).Exists() {
// enqueue public test
request := shared.NewSubmissionAMQPWorkerRequest(
course.ID, task.ID, submission.ID, grade.ID,
accessToken, viper.GetString("url"), task.PublicDockerImage.String, sha256, "public")
body, err := json.Marshal(request)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
err = DefaultSubmissionProducer.Publish(body)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
} else {
grade.PublicTestLog = "No public dockerimage was specified --> will not run any public test"
err = rs.Stores.Grade.Update(grade)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
}
if task.PrivateDockerImage.Valid && helper.NewPrivateTestFileHandle(task.ID).Exists() {
// enqueue private test
request := shared.NewSubmissionAMQPWorkerRequest(
course.ID, task.ID, submission.ID, grade.ID,
accessToken, viper.GetString("url"), task.PrivateDockerImage.String, sha256, "private")
body, err := json.Marshal(request)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
err = DefaultSubmissionProducer.Publish(body)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
} else {
grade.PrivateTestLog = "No private dockerimage was specified --> will not run any private test"
err = rs.Stores.Grade.Update(grade)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
}
totalSubmissionCounterVec.WithLabelValues(fmt.Sprintf("%d", task.ID)).Inc()
render.Status(r, http.StatusOK)
}
// IndexHandler is public endpoint for
// URL: /courses/{course_id}/submissions
// URLPARAM: course_id,integer
// QUERYPARAM: sheet_id,integer
// QUERYPARAM: task_id,integer
// QUERYPARAM: group_id,integer
// QUERYPARAM: user_id,integer
// METHOD: get
// TAG: submissions
// RESPONSE: 200,SubmissionResponseList
// RESPONSE: 400,BadRequest
// RESPONSE: 401,Unauthenticated
// RESPONSE: 403,Unauthorized
// SUMMARY: Query submissions in a course
func (rs *SubmissionResource) IndexHandler(w http.ResponseWriter, r *http.Request) {
course := r.Context().Value(symbol.CtxKeyCourse).(*model.Course)
filterGroupID := helper.Int64FromURL(r, "group_id", 0)
filterUserID := helper.Int64FromURL(r, "user_id", 0)
filterSheetID := helper.Int64FromURL(r, "sheet_id", 0)
filterTaskID := helper.Int64FromURL(r, "task_id", 0)
submissions, err := rs.Stores.Submission.GetFiltered(course.ID, filterGroupID, filterUserID, filterSheetID, filterTaskID)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
// render JSON reponse
if err = render.RenderList(w, r, newSubmissionListResponse(submissions, course.ID)); err != nil {
render.Render(w, r, ErrRender(err))
return
}
render.Status(r, http.StatusOK)
}
// .............................................................................
// Context middleware is used to load an Submission object from
// the URL parameter `TaskID` passed through as the request. In case
// the Submission could not be found, we stop here and return a 404.
// We do NOT check whether the identity is authorized to get this Submission.
func (rs *SubmissionResource) Context(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// course_from_url := r.Context().Value(symbol.CtxKeyCourse).(*model.Course)
var submissionID int64
var err error
// try to get id from URL
if submissionID, err = strconv.ParseInt(chi.URLParam(r, "submission_id"), 10, 64); err != nil {
render.Render(w, r, ErrNotFound)
return
}
// find specific Submission in database
submission, err := rs.Stores.Submission.Get(submissionID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
ctx := context.WithValue(r.Context(), symbol.CtxKeySubmission, submission)
// when there is a submissionID in the url, there is NOT a taskID in the url,
// BUT: when there is a Submission, there is a task
// BUT: when there is a task, there is a course (other middlewarwe)
// find specific Task in database
var task *model.Task
task, err = rs.Stores.Task.Get(submission.TaskID)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
ctx = context.WithValue(ctx, symbol.CtxKeyTask, task)
// find sheet
sheet, err := rs.Stores.Task.IdentifySheetOfTask(task.ID)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
if sheetID, err := strconv.ParseInt(chi.URLParam(r, "sheet_id"), 10, 64); err == nil {
if sheetID != sheet.ID {
render.Render(w, r, ErrNotFound)
return
}
} else {
ctx = context.WithValue(ctx, symbol.CtxKeySheet, sheet)
}
// find course
course, err := rs.Stores.Task.IdentifyCourseOfTask(task.ID)
if err != nil {
render.Render(w, r, ErrInternalServerErrorWithDetails(err))
return
}
ctx = context.WithValue(ctx, symbol.CtxKeyCourse, course)
// serve next
next.ServeHTTP(w, r.WithContext(ctx))
})
}