From dacc29a2d57b38ed2c0ce303c38394ce03296cba Mon Sep 17 00:00:00 2001 From: "Dave St.Germain" Date: Mon, 8 Jul 2019 11:37:49 -0400 Subject: [PATCH] Fixed saving exams to backend when there's a due date set --- edx_proctoring/__init__.py | 2 +- edx_proctoring/backends/rest.py | 7 +++++-- edx_proctoring/backends/tests/test_rest.py | 6 ++++++ edx_proctoring/serializers.py | 7 +++++++ edx_proctoring/signals.py | 8 ++++---- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/edx_proctoring/__init__.py b/edx_proctoring/__init__.py index e8fcfa3d825..7ca7d921567 100644 --- a/edx_proctoring/__init__.py +++ b/edx_proctoring/__init__.py @@ -5,6 +5,6 @@ from __future__ import absolute_import # Be sure to update the version number in edx_proctoring/package.json -__version__ = '2.0.4' +__version__ = '2.0.5' default_app_config = 'edx_proctoring.apps.EdxProctoringConfig' # pylint: disable=invalid-name diff --git a/edx_proctoring/backends/rest.py b/edx_proctoring/backends/rest.py index a3546580e38..ca45cf15e36 100644 --- a/edx_proctoring/backends/rest.py +++ b/edx_proctoring/backends/rest.py @@ -266,8 +266,11 @@ def on_exam_saved(self, exam): response = self.session.post(url, json=exam) data = response.json() except Exception as exc: # pylint: disable=broad-except - # pylint: disable=no-member - content = exc.response.content if hasattr(exc, 'response') else response.content + if response: + # pylint: disable=no-member + content = exc.response.content if hasattr(exc, 'response') else response.content + else: + content = None log.exception('failed to save exam. %r', content) data = {} return data.get('id') diff --git a/edx_proctoring/backends/tests/test_rest.py b/edx_proctoring/backends/tests/test_rest.py index d0139135a57..a628ed8d9dd 100644 --- a/edx_proctoring/backends/tests/test_rest.py +++ b/edx_proctoring/backends/tests/test_rest.py @@ -166,6 +166,12 @@ def test_failed_exam_save(self): external_id = self.provider.on_exam_saved(self.backend_exam) self.assertEqual(external_id, None) + @responses.activate + def test_bad_exam_save(self): + self.backend_exam['bad'] = object() + external_id = self.provider.on_exam_saved(self.backend_exam) + self.assertEqual(external_id, None) + @responses.activate def test_register_exam_attempt(self): context = { diff --git a/edx_proctoring/serializers.py b/edx_proctoring/serializers.py index 6bb6e59602f..59ce8b6eaa3 100644 --- a/edx_proctoring/serializers.py +++ b/edx_proctoring/serializers.py @@ -46,6 +46,13 @@ class Meta: ) +class ProctoredExamJSONSafeSerializer(ProctoredExamSerializer): + """ + ProctoredExam serializer which will return dates as strings. + """ + due_date = serializers.DateTimeField(required=False) + + class UserSerializer(serializers.ModelSerializer): """ Serializer for the User Model. diff --git a/edx_proctoring/signals.py b/edx_proctoring/signals.py index 05fef5b7a0d..41a89db2ff1 100644 --- a/edx_proctoring/signals.py +++ b/edx_proctoring/signals.py @@ -22,8 +22,8 @@ def check_for_category_switch(sender, instance, **kwargs): # pylint: disable=un if instance.id: original = sender.objects.get(pk=instance.id) if original.is_proctored and instance.is_proctored != original.is_proctored: - from edx_proctoring.serializers import ProctoredExamSerializer - exam = ProctoredExamSerializer(instance).data + from edx_proctoring.serializers import ProctoredExamJSONSafeSerializer + exam = ProctoredExamJSONSafeSerializer(instance).data # from the perspective of the backend, the exam is now inactive. exam['is_active'] = False backend = get_backend_provider(name=exam['backend']) @@ -44,8 +44,8 @@ def save_exam_on_backend(sender, instance, **kwargs): # pylint: disable=unused- exam_obj = instance.proctored_exam review_policy = instance if exam_obj.is_proctored: - from edx_proctoring.serializers import ProctoredExamSerializer - exam = ProctoredExamSerializer(exam_obj).data + from edx_proctoring.serializers import ProctoredExamJSONSafeSerializer + exam = ProctoredExamJSONSafeSerializer(exam_obj).data if review_policy: exam['rule_summary'] = review_policy.review_policy backend = get_backend_provider(exam)