Commit a506486f by Andy Armstrong Committed by GitHub

Merge pull request #15855 from edx/andya/bootstrap-update-lang-page-3

Convert /update_lang/ page to use Bootstrap
parents 93d84396 82f7d6ee
## Override the default styles_version to the Pattern Library version (version 2)
<%! main_css = "style-main-v2" %>
<%page expression_filter="h"/>
<%!
from django.utils.translation import ugettext as _
%>
<%inherit file="../base.html" />
<%block name="title">${_("Preview Language Setting")}</%block>
<%block name="bodyclass">is-signedin pattern-library</%block>
<%block name="content">
<%include file="/dark_lang/preview_lang_include.html" />
</%block>
......@@ -128,7 +128,7 @@ class I18nRegressionTests(BaseI18nTestCase):
self.assert_tag_has_attr(response.content, "html", "lang", "fa")
# Now try to access with dark lang
self.client.post(self.preview_language_url, {'preview_lang': 'fa-ir', 'set_language': 'set_language'})
self.client.post(self.preview_language_url, {'preview_language': 'fa-ir', 'action': 'set_preview_language'})
response = self.client.get(self.url)
self.assert_tag_has_attr(response.content, "html", "lang", "fa-ir")
......@@ -144,17 +144,17 @@ class I18nRegressionTests(BaseI18nTestCase):
# Verify we can switch language using the preview-lang query param
# Set the language
self.client.post(self.preview_language_url, {'preview_lang': 'eo', 'set_language': 'set_language'})
self.client.post(self.preview_language_url, {'preview_language': 'eo', 'action': 'set_preview_language'})
response = self.client.get(self.url)
self.assert_tag_has_attr(response.content, "html", "lang", "eo")
# We should be able to see released languages using preview-lang, too
self.client.post(self.preview_language_url, {'preview_lang': 'es-419', 'set_language': 'set_language'})
self.client.post(self.preview_language_url, {'preview_language': 'es-419', 'action': 'set_preview_language'})
response = self.client.get(self.url)
self.assert_tag_has_attr(response.content, "html", "lang", "es-419")
# Clearing the language should go back to site default
self.client.post(self.preview_language_url, {'reset': 'reset'})
self.client.post(self.preview_language_url, {'action': 'reset_preview_language'})
response = self.client.get(self.url)
self.assert_tag_has_attr(response.content, "html", "lang", site_lang)
......@@ -205,7 +205,7 @@ class I18nLangPrefTests(BaseI18nTestCase):
# Set user language preference
self.set_lang_preference('ar')
# Verify preview-lang takes precedence
self.client.post(self.preview_language_url, {'preview_lang': 'eo', 'set_language': 'set_language'})
self.client.post(self.preview_language_url, {'preview_language': 'eo', 'action': 'set_preview_language'})
response = self.client.get(self.url)
self.assert_tag_has_attr(response.content, "html", "lang", 'eo')
......@@ -214,7 +214,7 @@ class I18nLangPrefTests(BaseI18nTestCase):
self.assert_tag_has_attr(response.content, "html", "lang", "eo")
# Clearing language must set language back to preference language
self.client.post(self.preview_language_url, {'reset': 'reset'})
self.client.post(self.preview_language_url, {'action': 'reset_preview_language'})
response = self.client.get(self.url)
self.assert_tag_has_attr(response.content, "html", "lang", 'ar')
## Override the default styles_version to the Pattern Library version (version 2)
<%! main_css = "style-main-v2" %>
<%page expression_filter="h"/>
<%!
from django.utils.translation import ugettext as _
%>
<%inherit file="/main.html" />
<%block name="pagetitle">${_("Preview Language Setting")}</%block>
<%block name="nav_skip"></%block>
<%block name="bodyclass">pattern-library</%block>
<%block name="content">
<%include file="/dark_lang/preview_lang_include.html" />
</%block>
## mako
<%page expression_filter="h"/>
<%namespace name='static' file='../static_content.html'/>
<%!
from django.utils.translation import ugettext as _
%>
<%page expression_filter="h"/>
<h1>
<h3>
${_("Preview Language Setting")}
</h1>
</h3>
<div >
<form class="form" action="/update_lang/" method="post">
<form class="form" action="${request.path}" method="post">
<fieldset class="form-group">
<div class="field">
<label class="field-label">${_("Language Code")}
<input class="field-input input-text" type="text" name="preview_lang"
placeholder="${_('For example use en for English')}" />
<input class="field-input input-text" type="text" name="preview_language"
placeholder="${_('e.g. en for English')}" />
</label>
</div>
</fieldset>
<div class="form-actions">
<button class="btn-brand btn-base" type="submit" name="set_language" value="set_language">${_("Submit")}</button>
<button class="btn-brand btn-base" type="submit" name="reset" value="reset">${_("Reset")}</button>
<button class="btn btn-primary" type="submit" name="action" value="set_preview_language">${_("Submit")}</button>
<button class="btn btn-secondary" type="submit" name="action" value="reset_preview_language">${_("Reset")}</button>
</div>
<input type="hidden" name="csrfmiddlewaretoken" value="${ csrf_token }"/>
</form>
<br/>
% if not form_submit_message is UNDEFINED:
<h3 class="alert-title"> ${form_submit_message}</h3>
% if success:
<p class="alert-copy-with-title">
${_("Please refresh the page to see the changes applied.")}
</p>
% endif
% endif
</div>
......@@ -243,13 +243,13 @@ class DarkLangMiddlewareTests(TestCase):
"""
Sends a post request to set the preview language
"""
return self.client.post('/update_lang/', {'preview_lang': preview_language, 'set_language': 'set_language'})
return self.client.post('/update_lang/', {'preview_language': preview_language, 'action': 'set_preview_language'})
def _post_clear_preview_lang(self):
"""
Sends a post request to Clear the preview language
"""
return self.client.post('/update_lang/', {'reset': 'reset'})
return self.client.post('/update_lang/', {'action': 'reset_preview_language'})
def _set_client_session_language(self, session_language):
"""
......
......@@ -8,5 +8,5 @@ from openedx.core.djangoapps.dark_lang import views
urlpatterns = patterns(
'',
url(r'^$', views.DarkLangView.as_view(), name='preview_lang'),
url(r'^$', views.PreviewLanguageFragmentView.as_view(), name='preview_lang'),
)
......@@ -2,27 +2,26 @@
Views file for the Darklang Django App
"""
from django.contrib.auth.decorators import login_required
from django.http import Http404
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _
from django.utils.translation import LANGUAGE_SESSION_KEY
from django.views.generic.base import View
from django.utils.translation import LANGUAGE_SESSION_KEY, ugettext as _
from edxmako.shortcuts import render_to_response
from openedx.core.djangoapps.dark_lang import DARK_LANGUAGE_KEY
from openedx.core.djangoapps.dark_lang.models import DarkLangConfig
from openedx.core.djangoapps.lang_pref import LANGUAGE_KEY
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView
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 openedx.core.djangoapps.util.user_messages import PageLevelMessages
from web_fragments.fragment import Fragment
LANGUAGE_INPUT_FIELD = 'preview_lang'
LANGUAGE_INPUT_FIELD = 'preview_language'
@view_auth_classes()
class DarkLangView(View):
class PreviewLanguageFragmentView(EdxFragmentView):
"""
View used when a user is attempting to change the preview language using Darklang.
......@@ -30,136 +29,84 @@ class DarkLangView(View):
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):
def render_to_fragment(self, request, course_id=None, **kwargs):
"""
Returns the Form for setting/resetting a User's dark language setting
Arguments:
request (Request): The Django Request Object
Renders the language preview view as a fragment.
"""
html = render_to_string('dark_lang/preview-language-fragment.html', {})
return Fragment(html)
Returns:
HttpResponse: View containing the form for setting the preview lang
def create_base_standalone_context(self, request, fragment, **kwargs):
"""
Creates the base context for rendering a fragment as a standalone page.
"""
context = {
'disable_courseware_js': True,
'uses_pattern_library': True
return {
'uses_bootstrap': True,
}
return render_to_response(self.template_name, context)
@method_decorator(login_required)
def post(self, request):
def standalone_page_title(self, request, fragment, **kwargs):
"""
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
Returns the page title for the standalone update page.
"""
return self.process_darklang_request(request)
return _('Preview Language Administration')
def process_darklang_request(self, request):
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
"""
Proccess the request to Set or clear the DarkLang depending on the incoming request.
Arguments:
request (Request): The Django Request Object
Renders the fragment to control the preview language.
"""
if not self._user_can_preview_languages(request.user):
raise Http404
return super(PreviewLanguageFragmentView, self).get(request, *args, **kwargs)
Returns:
HttpResponse: View containing the form for setting the preview lang with the status
included in the context
@method_decorator(login_required)
def post(self, request, **kwargs):
"""
Accept requests to update the preview language.
"""
if not self._user_can_preview_languages(request.user):
raise Http404
action = request.POST.get('action', None)
if action == 'set_preview_language':
self._set_preview_language(request)
elif action == 'reset_preview_language':
self._clear_preview_language(request)
return redirect(request.path)
def _user_can_preview_languages(self, user):
"""
Returns true if the specified user can preview languages.
"""
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
return False
return user and not user.is_anonymous()
def _set_preview_language(self, request, context):
def _set_preview_language(self, request):
"""
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
Sets the preview language for the current user.
"""
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
preview_language = request.POST.get(LANGUAGE_INPUT_FIELD, '')
if not preview_language.strip():
PageLevelMessages.register_error_message(request, _('Language not provided'))
return
set_user_preference(request.user, DARK_LANGUAGE_KEY, preview_language)
PageLevelMessages.register_success_message(
request,
_('Language set to {preview_language}').format(
preview_language=preview_language
)
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):
def _clear_preview_language(self, request):
"""
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)
Clears the preview language for the current user.
"""
delete_user_preference(request.user, DARK_LANGUAGE_KEY)
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)
PageLevelMessages.register_success_message(
request,
_('Language reset to the default')
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment