From ee12def2defd164f1e423ad9263e98ec6ac8df10 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Fri, 4 Aug 2017 14:59:50 -0400 Subject: [PATCH] Fix for EDUCATOR-1090 Now that uuids exist in the database both with and without hyphens, we must be resilient to both. --- setup.py | 2 +- submissions/api.py | 24 +++++++++++++++++------- submissions/tests/test_api.py | 6 +----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/setup.py b/setup.py index e2c250f..bfb3044 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/submissions/api.py b/submissions/api.py index 22a9a75..c0b76d1 100644 --- a/submissions/api.py +++ b/submissions/api.py @@ -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. @@ -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: @@ -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") @@ -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) diff --git a/submissions/tests/test_api.py b/submissions/tests/test_api.py index 5d218ce..4221a82 100644 --- a/submissions/tests/test_api.py +++ b/submissions/tests/test_api.py @@ -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')