Skip to content

Commit

Permalink
Merge pull request #67 from edx/efischer/EDU-1090
Browse files Browse the repository at this point in the history
Efischer/edu 1090
  • Loading branch information
Eric Fischer authored Aug 4, 2017
2 parents ec284b3 + ee12def commit 5e3eeaf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ dogapi>=1.2.1,<2.0.0
django-model-utils>=2.3.1,<3.0.0
# Use the same DRF version as edx-platform
git+https://github.com/edx/django-rest-framework.git@1ceda7c086fddffd1c440cc86856441bbf0bd9cb#egg=djangorestframework==3.6.3
jsonfield>=2.0.2,<3.0.0
jsonfield>=1.0.3,<2.0.0
pytz
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.7',
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 5e3eeaf

Please sign in to comment.