-
Notifications
You must be signed in to change notification settings - Fork 315
/
latesubmissions.py
78 lines (58 loc) · 2.74 KB
/
latesubmissions.py
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
from traitlets import Instance
from traitlets import Type
from .. import utils
from ..api import Gradebook
from ..plugins import BasePlugin
from ..plugins import LateSubmissionPlugin
from . import NbGraderPreprocessor
class AssignLatePenalties(NbGraderPreprocessor):
"""Preprocessor for assigning penalties for late submissions to the database"""
plugin_class = Type(
LateSubmissionPlugin,
klass=BasePlugin,
help="The plugin class for assigning the late penalty for each notebook."
).tag(config=True)
plugin_inst = Instance(BasePlugin).tag(config=False)
def init_plugin(self):
self.plugin_inst = self.plugin_class(parent=self)
def _check_late_penalty(self, notebook, penalty):
msg = "(Penalty {}) Adjusting late submission penalty from {} to {}."
if penalty < 0:
self.log.warning(msg.format("< 0", penalty, 0))
return 0
if penalty > notebook.score:
self.log.warning(msg.format("> score", penalty, notebook.score))
return notebook.score
return penalty
def preprocess(self, nb, resources):
# pull information from the resources
self.notebook_id = resources['nbgrader']['notebook']
self.assignment_id = resources['nbgrader']['assignment']
self.student_id = resources['nbgrader']['student']
self.db_url = resources['nbgrader']['db_url']
# init the plugin
self.init_plugin()
# connect to the database
self.gradebook = Gradebook(self.db_url)
try:
# process the late submissions
nb, resources = super(AssignLatePenalties, self).preprocess(nb, resources)
assignment = self.gradebook.find_submission(
self.assignment_id, self.student_id)
if assignment.total_seconds_late > 0:
self.log.warning("{} is {} seconds late".format(
assignment, assignment.total_seconds_late))
notebook = self.gradebook.find_submission_notebook(
self.notebook_id, self.assignment_id, self.student_id)
late_penalty = self.plugin_inst.late_submission_penalty(
self.student_id, notebook.score, assignment.total_seconds_late)
self.log.warning("Late submission penalty: {}".format(late_penalty))
if late_penalty is not None:
late_penalty = self._check_late_penalty(notebook, late_penalty)
notebook.late_submission_penalty = late_penalty
self.gradebook.db.commit()
finally:
self.gradebook.close()
return nb, resources
def preprocess_cell(self, cell, resources, cell_index):
return cell, resources