Skip to content

Commit

Permalink
Fix for EDUCATOR-1090
Browse files Browse the repository at this point in the history
Now that uuids exist in the database both with and without hyphens, we must be
resilient to both.
  • Loading branch information
Eric Fischer committed Aug 4, 2017
1 parent 7e835ff commit ee12def
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def load_requirements(*requirements_paths):

setup(
name='edx-submissions',
version='2.0.6',
version='2.0.8',
author='edX',
description='An API for creating submissions and scores.',
url='http://github.com/edx/edx-submissions.git',
Expand Down
24 changes: 17 additions & 7 deletions submissions/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ def create_submission(student_item_dict, answer, submitted_at=None, attempt_numb
raise SubmissionInternalError(error_message)


def _get_submission_model(uuid, read_replica=False):
"""
Helper to retrieve a given Submission object from the database. Helper is needed to centralize logic that fixes
EDUCATOR-1090, because uuids are stored both with and without hyphens.
"""
submission_qs = Submission.objects
if read_replica:
submission_qs = _use_read_replica(submission_qs)

query_regex = "^{}$|^{}$".format(uuid, uuid.replace("-",""))
submission = submission_qs.get(uuid__regex=query_regex)
return submission


def get_submission(submission_uuid, read_replica=False):
"""Retrieves a single submission by uuid.
Expand Down Expand Up @@ -244,11 +258,7 @@ def get_submission(submission_uuid, read_replica=False):
return cached_submission_data

try:
submission_qs = Submission.objects
if read_replica:
submission_qs = _use_read_replica(submission_qs)

submission = submission_qs.get(uuid=submission_uuid)
submission = _get_submission_model(submission_uuid, read_replica)
submission_data = SubmissionSerializer(submission).data
cache.set(cache_key, submission_data)
except Submission.DoesNotExist:
Expand Down Expand Up @@ -701,7 +711,7 @@ def get_latest_score_for_submission(submission_uuid, read_replica=False):
"""
try:
# Ensure that submission_uuid is valid before fetching score
submission_model = Submission.objects.get(uuid=submission_uuid)
submission_model = _get_submission_model(submission_uuid, read_replica)
score_qs = Score.objects.filter(
submission__uuid=submission_model.uuid
).order_by("-id").select_related("submission")
Expand Down Expand Up @@ -824,7 +834,7 @@ def set_score(submission_uuid, points_earned, points_possible,
"""
try:
submission_model = Submission.objects.get(uuid=submission_uuid)
submission_model = _get_submission_model(submission_uuid)
except Submission.DoesNotExist:
raise SubmissionNotFoundError(
u"No submission matching uuid {}".format(submission_uuid)
Expand Down
6 changes: 1 addition & 5 deletions submissions/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ def test_get_submission_and_student(self):
retrieved = api.get_submission_and_student(submission['uuid'])
self.assertItemsEqual(submission, retrieved)

# Should raise an exception if uuid is malformed
with self.assertRaises(api.SubmissionInternalError):
api.get_submission_and_student(u'no such uuid')

# Should raise a different exception if the student item does not exist
# Should raise an exception if the student item does not exist
with self.assertRaises(api.SubmissionNotFoundError):
api.get_submission_and_student(u'deadbeef-1234-5678-9100-1234deadbeef')

Expand Down

0 comments on commit ee12def

Please sign in to comment.