Files
edx-platform/common/djangoapps/dark_lang/views.py
Albert St. Aubin b42a7a1cfb Removed the Darklang preview-lang and clear-lang parameters and added the new DarkLang settings form at /update_lang
Remove the DarkLang middleware from the LMS

Created and basic routing to the update_lang page for the GET Request
TNL-4742

Basic form functionality

Working example in LMS of the form to set the language

Login now required to change the preview language, and corrected some minor bugs

Updates to move the template code to lms and to correct minor defects
TNL-4742

Added template for preview_lang.html to cms
TNL-4742

Changed filename of darklang.py to api.py to match convention
TNL-4742

Updated and refactored the Darklang tests
TNL-4742

Updated comments in tests
TNL-4742

Formating updates
TNL-4742

Updated comments and formatting
TNL-4742

Corrected i18n tests and corrected PEP8 format issues
TNL-4742

Code Lint/PEP-8 corrections and upates
TNL-4742

Removed constant that was not needed (to be squashed)
TNL-4742

Added init method to clear up PEP8 Warnings (will squash)
TNL-4742

PEP-8/Lint issue resolved (squash)

Updated for i18n
TNL-4742

Refactored the preview_lang.html page to use a common included template

Refactoring and changes from PR comments
TNL-4742

Correction for safecommit violation (Squash)
TNL-4742

PR changes and refactoring (Squash)

Updates to reduce changes made in the urls used
TNL-4742

Removed unneeded aria-described by and bug in MAKO Template (squash)
TNK-4742

Updated docstring comments

Clarified form response text

Minor PR request (Squash)

Refactoring of how the responses are generated within the DarkLang views file

A series of refactors in response to PR comments

Method name change for clarity in reponse to PR comments (Squash)

Updates to tests per PR requests (Squash)

Minor comment updates for clarity and PR requests (Squash)

Updated per PR comments and added a test for empty preview_language

Layout and code style updates (Squash)

Updated test to contain method in the request.

Removed the Darklang preview-lang and clear-lang parameters and added the new DarkLang settings form at /update_lang

Refactored tests and added some tests for coverage, corrected defect with empty input codes

Removed unused and obsolete code

Corrected test errors, resolved PR comments, and updated comments to clarify testing
TNL-4742

Updated tests to deal with Pylint quality issue (Squash)

Updated tests to better reflect test case and PR updates (Squash)
2016-07-27 10:43:08 -04:00

164 lines
6.0 KiB
Python

"""
Views file for the Darklang Django App
"""
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.utils.translation import LANGUAGE_SESSION_KEY
from django.utils.translation import ugettext as _
from django.views.generic.base import View
from openedx.core.djangoapps.user_api.preferences.api import (
delete_user_preference, get_user_preference, set_user_preference
)
from openedx.core.lib.api.view_utils import view_auth_classes
from dark_lang import DARK_LANGUAGE_KEY
from dark_lang.models import DarkLangConfig
from edxmako.shortcuts import render_to_response
from lang_pref import LANGUAGE_KEY
LANGUAGE_INPUT_FIELD = 'preview_lang'
@view_auth_classes()
class DarkLangView(View):
"""
View used when a user is attempting to change the preview language using Darklang.
Expected Behavior:
GET - returns a form for setting/resetting the user's dark language
POST - updates or clears the setting to the given dark language
"""
template_name = 'darklang/preview_lang.html'
@method_decorator(login_required)
def get(self, request):
"""
Returns the Form for setting/resetting a User's dark language setting
Arguments:
request (Request): The Django Request Object
Returns:
HttpResponse: View containing the form for setting the preview lang
"""
context = {
'disable_courseware_js': True,
'uses_pattern_library': True
}
return render_to_response(self.template_name, context)
@method_decorator(login_required)
def post(self, request):
"""
Sets or clears the DarkLang depending on the incoming post data.
Arguments:
request (Request): The Django Request Object
Returns:
HttpResponse: View containing the form for setting the preview lang with the status
included in the context
"""
return self.process_darklang_request(request)
def process_darklang_request(self, request):
"""
Proccess the request to Set or clear the DarkLang depending on the incoming request.
Arguments:
request (Request): The Django Request Object
Returns:
HttpResponse: View containing the form for setting the preview lang with the status
included in the context
"""
context = {
'disable_courseware_js': True,
'uses_pattern_library': True
}
response = None
if not DarkLangConfig.current().enabled:
message = _('Preview Language is currently disabled')
context.update({'form_submit_message': message})
context.update({'success': False})
response = render_to_response(self.template_name, context, request=request)
elif 'set_language' in request.POST:
# Set the Preview Language
response = self._set_preview_language(request, context)
elif 'reset' in request.POST:
# Reset and clear the language preference
response = self._clear_preview_language(request, context)
return response
def _set_preview_language(self, request, context):
"""
Set the Preview language
Arguments:
request (Request): The incoming Django Request
context dict: The basic context for the Response
Returns:
HttpResponse: View containing the form for setting the preview lang with the status
included in the context
"""
message = None
show_refresh_message = False
preview_lang = request.POST.get(LANGUAGE_INPUT_FIELD, '')
if not preview_lang.strip():
message = _('Language code not provided')
else:
# Set the session key to the requested preview lang
request.session[LANGUAGE_SESSION_KEY] = preview_lang
# Make sure that we set the requested preview lang as the dark lang preference for the
# user, so that the lang_pref middleware doesn't clobber away the dark lang preview.
auth_user = request.user
if auth_user:
set_user_preference(request.user, DARK_LANGUAGE_KEY, preview_lang)
message = _('Language set to language code: {preview_language_code}').format(
preview_language_code=preview_lang
)
show_refresh_message = True
context.update({'form_submit_message': message})
context.update({'success': show_refresh_message})
response = render_to_response(self.template_name, context)
return response
def _clear_preview_language(self, request, context):
"""
Clears the dark language preview
Arguments:
request (Request): The incoming Django Request
context dict: The basic context for the Response
Returns:
HttpResponse: View containing the form for setting the preview lang with the status
included in the context
"""
# delete the session language key (if one is set)
if LANGUAGE_SESSION_KEY in request.session:
del request.session[LANGUAGE_SESSION_KEY]
user_pref = ''
auth_user = request.user
if auth_user:
# Reset user's dark lang preference to null
delete_user_preference(auth_user, DARK_LANGUAGE_KEY)
# Get & set user's preferred language
user_pref = get_user_preference(auth_user, LANGUAGE_KEY)
if user_pref:
request.session[LANGUAGE_SESSION_KEY] = user_pref
if user_pref is None:
message = _('Language reset to the default language code')
else:
message = _("Language reset to user's preference: {preview_language_code}").format(
preview_language_code=user_pref
)
context.update({'form_submit_message': message})
context.update({'success': True})
return render_to_response(self.template_name, context)