Skip to content

Commit

Permalink
Convert extension arguments to seconds + Add weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
AlirezaT99 committed Jul 29, 2024
1 parent d5d4adf commit b578cc4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion nbgrader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def to_dict(self):
"task_score": self.task_score,
"max_task_score": self.max_task_score,
"needs_manual_grade": self.needs_manual_grade,
"extension": (self.extension + datetime.datetime(1970, 1, 1)).isoformat() if self.extension is not None else None,
"extension": self.extension.total_seconds() if self.extension is not None else None,
}

def __repr__(self) -> str:
Expand Down
8 changes: 5 additions & 3 deletions nbgrader/apps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def get_submissions(self, assignment_id):
submission["autograded"] = True
submission["submitted"] = True
if submission["extension"]:
submission["extension"] = (submission["extension"] + datetime.datetime(1970, 1, 1)).isoformat()
submission["extension"] = submission["extension"].total_seconds()
submissions.append(submission)

for student_id in ungraded:
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def fetch_feedback(self, assignment_id, student_id):
ret_dic["value"] = sorted(assignments, key=lambda x: (x['course_id'], x['assignment_id']))
return ret_dic

def grant_extension_to_student(self, assignment_id, student_id, minutes, hours, days):
def grant_extension_to_student(self, assignment_id, student_id, minutes, hours, days, weeks):
"""Grants extension for a particular assignment and student.
Arguments
Expand All @@ -1180,6 +1180,8 @@ def grant_extension_to_student(self, assignment_id, student_id, minutes, hours,
The number of hours to extend the assignment deadline
days: int
The number of days to extend the assignment deadline
weeks: int
The number of weeks to extend the assignment deadline
Returns
-------
Expand All @@ -1193,7 +1195,7 @@ def grant_extension_to_student(self, assignment_id, student_id, minutes, hours,
"""
with self.gradebook as gb:
try:
gb.grant_extension(assignment_id, student_id, minutes, hours, days)
gb.grant_extension(assignment_id, student_id, minutes, hours, days, weeks)
except InvalidEntry as e:
return {"success": False, "error": str(e)}
return {"success": True}
3 changes: 2 additions & 1 deletion nbgrader/server_extensions/formgrader/apihandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ def post(self, assignment_id, student_id):
minutes = int(data.get('minutes', 0))
hours = int(data.get('hours', 0))
days = int(data.get('days', 0))
weeks = int(data.get('weeks', 0))
except ValueError:
raise web.HTTPError(400, "Invalid extension time")
self.write(json.dumps(self.api.grant_extension_to_student(assignment_id, student_id, minutes, hours, days)))
self.write(json.dumps(self.api.grant_extension_to_student(assignment_id, student_id, minutes, hours, days, weeks)))


class GenerateAllFeedbackHandler(BaseApiHandler):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ var SubmissionUI = Backbone.View.extend({
var body = $("<table/>").addClass("table table-striped form-table");
var tableBody = $("<tbody/>");
body.append(tableBody);


var weeks = $("<tr/>");
tableBody.append(weeks);
weeks.append($("<td/>").addClass("align-middle").text("Weeks"));
weeks.append($("<td/>").append($("<input/>").addClass("modal-weeks").attr({type: "number", min: 0, step: 1, value: this.modal_extension_weeks})));

var days = $("<tr/>");
tableBody.append(days);
days.append($("<td/>").addClass("align-middle").text("Days"));
Expand Down Expand Up @@ -313,11 +318,14 @@ var SubmissionUI = Backbone.View.extend({

this.$modal = createModal("grant-extension-modal", "Granting Extension to " + this.model.get("student"), body, footer);

var extension = new Date(this.model.get("extension") || '1970-01-01T00:00:00Z');
var modal_extension_days = Math.trunc(extension.getTime() / 86400000);
var modal_extension_hours = extension.getHours();
var modal_extension_minutes = extension.getMinutes();

var extension = this.model.get("extension") || 0;
var modal_extension_days = Math.trunc(extension / 86400);
var modal_extension_weeks = Math.trunc(modal_extension_days / 7);
modal_extension_days = modal_extension_days % 7;
var modal_extension_hours = Math.trunc((extension % 86400) / 3600);
var modal_extension_minutes = Math.trunc((extension % 3600) / 60);

this.$modal.find("input.modal-weeks").val(modal_extension_weeks);
this.$modal.find("input.modal-days").val(modal_extension_days);
this.$modal.find("input.modal-hours").val(modal_extension_hours);
this.$modal.find("input.modal-minutes").val(modal_extension_minutes);
Expand All @@ -327,6 +335,7 @@ var SubmissionUI = Backbone.View.extend({
save: function () {
this.animateSaving();

var weeks = this.$modal.find("input.modal-weeks").val() || 0;
var days = this.$modal.find("input.modal-days").val() || 0;
var hours = this.$modal.find("input.modal-hours").val() || 0;
var minutes = this.$modal.find("input.modal-minutes").val() || 0;
Expand All @@ -342,7 +351,8 @@ var SubmissionUI = Backbone.View.extend({
data: JSON.stringify({
minutes: minutes,
hours: hours,
days: days
days: days,
weeks: weeks,
}),
})
.done(_.bind(this.grant_extension_log, this));
Expand Down Expand Up @@ -380,6 +390,7 @@ var SubmissionUI = Backbone.View.extend({
if (this.$modal) {
this.$modal.modal('hide')
this.$modal = undefined;
this.modal_extension_weeks = 0;
this.modal_extension_days = 0;
this.modal_extension_hours = 0;
this.modal_extension_minutes = 0;
Expand Down
4 changes: 2 additions & 2 deletions nbgrader/tests/apps/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,8 @@ def test_grant_extension_to_student(self, api, course_dir):
api.generate_assignment("ps1")
api.autograde("ps1", "foo")

result = api.grant_extension_to_student("ps1", "foo", 1, 2, 3)
result = api.grant_extension_to_student("ps1", "foo", 1, 2, 3, 4)
assert result["success"]

extension = api.get_submission("ps1", "foo")["extension"]
assert extension == datetime(1970, 1, 4, 2, 1).isoformat()
assert extension == timedelta(weeks=4, days=3, hours=2, minutes=1).total_seconds()

0 comments on commit b578cc4

Please sign in to comment.