Skip to content

Commit

Permalink
refactor: removed the redundant submissions_api.get_scoresubmission call
Browse files Browse the repository at this point in the history
  • Loading branch information
Anas12091101 committed Feb 27, 2024
1 parent 58327aa commit 782161a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 7 additions & 10 deletions edx_sga/sga.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ def get_student_item_dict(self, student_id=None):
"""
if student_id is None and (user_service := self.runtime.service(self, 'user')):
student_id = user_service.get_current_user().opt_attrs.get(ATTR_KEY_ANONYMOUS_USER_ID)

assert student_id != ("MOCK", "Forgot to call 'personalize' in test.")
return {
"student_id": student_id,
Expand Down Expand Up @@ -747,25 +746,23 @@ def student_state(self):
"""
Returns a JSON serializable representation of student's state for
rendering in client view.
"""
graded = None
"""
submission = self.get_submission()

if submission:
uploaded = {"filename": submission["answer"]["filename"]}
student_item = SubmissionsStudent.objects.get(id = submission.get("student_item"))
score = submissions_api.get_score(student_item.student_item_dict)
if score:
score = score.get("points_earned")
graded = {"score": score, "comment": force_str(self.comment)}

else:
uploaded = None

if self.annotated_sha1:
annotated = {"filename": force_str(self.annotated_filename)}
else:
annotated = None

score = self.score
if score is not None:
graded = {"score": score, "comment": force_str(self.comment)}
else:
graded = None

if self.answer_available() and (replace_urls_service := self.runtime.service(self, 'replace_urls')):
solution = replace_urls_service.replace_urls(force_str(self.solution))
Expand Down
4 changes: 2 additions & 2 deletions edx_sga/tests/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ def test_student_view_with_score(self, fragment, render_template):
user = student["module"].student
student_id = anonymous_id_for_user(user,self.course_id)

with mock.patch.object(StaffGradedAssignmentXBlock.get_submission,"__defaults__",(student_id,)):
with mock.patch.object(StaffGradedAssignmentXBlock.get_submission,"__defaults__",(student_id,)),\
mock.patch.object(StaffGradedAssignmentXBlock.get_score, "__defaults__",(student_id,)):
fragment = block.student_view()
render_template.assert_called_once()
template_arg = render_template.call_args[0][0]
Expand All @@ -308,7 +309,6 @@ def test_student_view_with_score(self, fragment, render_template):
self.assertEqual(context["is_course_staff"], True)
self.assertEqual(context["id"], "d_0")
student_state = json.loads(context["student_state"])
print(f"state: {student_state}")
self.assertEqual(student_state["display_name"], "Staff Graded Assignment")
self.assertEqual(student_state["uploaded"], {"filename": "foo.txt"})
self.assertEqual(student_state["annotated"], None)
Expand Down

0 comments on commit 782161a

Please sign in to comment.