Skip to content

Commit

Permalink
Merge pull request #468 from edx/dcs/instructions-i18n
Browse files Browse the repository at this point in the history
Ensure that the instructions are translated on the backend
  • Loading branch information
davestgermain authored Nov 29, 2018
2 parents 51a9549 + e5dc501 commit 59de704
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/backends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Proctoring System configuration endpoint
}

The keys in the rules object should be machine readable. The values are human readable. PS should respect the HTTP request ``Accept-Language``
header and translate all human readable rules into the requested language.
header and translate all human readable rules and instructions into the requested language.

If a download_url is included in the response, Open edX will redirect learners to the address before the proctoring session starts. The address will include ``attempt={attempt_id}`` in the query string.

Expand Down
25 changes: 23 additions & 2 deletions edx_proctoring/backends/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import uuid
import pkg_resources

from edx_proctoring.backends.backend import ProctoringBackendProvider
from edx_proctoring.statuses import ProctoredExamStudentAttemptStatus
from edx_rest_api_client.client import OAuthAPIClient
Expand Down Expand Up @@ -89,7 +90,7 @@ def get_proctoring_config(self):
"""
url = self.config_url
log.debug('Requesting config from %r', url)
response = self.session.get(url).json()
response = self.session.get(url, headers=self._get_language_headers()).json()
return response

def get_exam(self, exam):
Expand Down Expand Up @@ -200,6 +201,23 @@ def get_instructor_url(self, course_id, user, exam_id=None, attempt_id=None):
log.debug('Created instructor url for %r %r %r', course_id, exam_id, attempt_id)
return url

def _get_language_headers(self):
"""
Returns a dictionary of the Accept-Language headers
"""
# This import is here because developers writing backends which subclass this class
# may want to import this module and use the other methods, without having to run in the context
# of django settings, etc.
from django.conf import settings
from django.utils.translation import get_language

current_lang = get_language()
default_lang = settings.LANGUAGE_CODE
lang_header = default_lang
if current_lang and current_lang != default_lang:
lang_header = '{};{}'.format(current_lang, default_lang)
return {'Accept-Language': lang_header}

def _make_attempt_request(self, exam, attempt, method='POST', status=None, **payload):
"""
Calls backend attempt API
Expand All @@ -209,6 +227,9 @@ def _make_attempt_request(self, exam, attempt, method='POST', status=None, **pay
else:
payload = None
url = self.exam_attempt_url.format(exam_id=exam, attempt_id=attempt)
headers = {}
if method == 'GET':
headers.update(self._get_language_headers())
log.debug('Making %r attempt request at %r', method, url)
response = self.session.request(method, url, json=payload).json()
response = self.session.request(method, url, json=payload, headers=headers).json()
return response
22 changes: 22 additions & 0 deletions edx_proctoring/backends/tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import responses

from django.test import TestCase
from django.utils.translation import activate

from edx_proctoring.backends.rest import BaseRestProctoringProvider

Expand Down Expand Up @@ -88,6 +89,27 @@ def test_get_attempt(self):
)
external_attempt = self.provider.get_attempt(attempt)
self.assertEqual(external_attempt, attempt)
self.assertEqual(responses.calls[1].request.headers['Accept-Language'], 'en-us')

@responses.activate
def test_get_attempt_i18n(self):
activate('es')
attempt = {
'id': 1,
'external_id': 'abcd',
'proctored_exam': self.backend_exam,
'user': 1,
'instructions': []
}
responses.add(
responses.GET,
url=self.provider.exam_attempt_url.format(
exam_id=self.backend_exam['external_id'], attempt_id=attempt['external_id']),
json=attempt
)
external_attempt = self.provider.get_attempt(attempt)
self.assertEqual(external_attempt, attempt)
self.assertEqual(responses.calls[1].request.headers['Accept-Language'], 'es;en-us')

@responses.activate
def test_on_exam_saved(self):
Expand Down

0 comments on commit 59de704

Please sign in to comment.