Python django.utils.translation.LANGUAGE_SESSION_KEY Examples
The following are 23
code examples of django.utils.translation.LANGUAGE_SESSION_KEY().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.utils.translation
, or try the search function
.
Example #1
Source File: __init__.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def logout(request): """ Removes the authenticated user's ID from the request and flushes their session data. """ # Dispatch the signal before the user is logged out so the receivers have a # chance to find out *who* logged out. user = getattr(request, 'user', None) if hasattr(user, 'is_authenticated') and not user.is_authenticated(): user = None user_logged_out.send(sender=user.__class__, request=request, user=user) # remember language choice saved to session language = request.session.get(LANGUAGE_SESSION_KEY) request.session.flush() if language is not None: request.session[LANGUAGE_SESSION_KEY] = language if hasattr(request, 'user'): from django.contrib.auth.models import AnonymousUser request.user = AnonymousUser()
Example #2
Source File: __init__.py From bioforum with MIT License | 6 votes |
def logout(request): """ Remove the authenticated user's ID from the request and flush their session data. """ # Dispatch the signal before the user is logged out so the receivers have a # chance to find out *who* logged out. user = getattr(request, 'user', None) if hasattr(user, 'is_authenticated') and not user.is_authenticated: user = None user_logged_out.send(sender=user.__class__, request=request, user=user) # remember language choice saved to session language = request.session.get(LANGUAGE_SESSION_KEY) request.session.flush() if language is not None: request.session[LANGUAGE_SESSION_KEY] = language if hasattr(request, 'user'): from django.contrib.auth.models import AnonymousUser request.user = AnonymousUser()
Example #3
Source File: views.py From esdc-ce with Apache License 2.0 | 6 votes |
def setlang(request): """ Sets a user's language preference and redirects to a given URL or, by default, back to the previous page. """ next = request.GET.get('next', None) if not is_safe_url(url=next, host=request.get_host()): next = request.META.get('HTTP_REFERER') if not is_safe_url(url=next, host=request.get_host()): next = '/' response = redirect(next) lang_code = request.GET.get('language', None) if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session[LANGUAGE_SESSION_KEY] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code, max_age=settings.LANGUAGE_COOKIE_AGE, path=settings.LANGUAGE_COOKIE_PATH, domain=settings.LANGUAGE_COOKIE_DOMAIN) return response
Example #4
Source File: middleware.py From cadasta-platform with GNU Affero General Public License v3.0 | 6 votes |
def process_response(self, request, response): if not hasattr(request, 'user'): return response if not request.user.is_authenticated: return response user_language = request.user.language current_language = translation.get_language() if user_language == current_language: return response translation.activate(user_language) request.session[translation.LANGUAGE_SESSION_KEY] = user_language return response
Example #5
Source File: __init__.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
def logout(request): """ Remove the authenticated user's ID from the request and flush their session data. """ # Dispatch the signal before the user is logged out so the receivers have a # chance to find out *who* logged out. user = getattr(request, 'user', None) if not getattr(user, 'is_authenticated', True): user = None user_logged_out.send(sender=user.__class__, request=request, user=user) # remember language choice saved to session language = request.session.get(LANGUAGE_SESSION_KEY) request.session.flush() if language is not None: request.session[LANGUAGE_SESSION_KEY] = language if hasattr(request, 'user'): from django.contrib.auth.models import AnonymousUser request.user = AnonymousUser()
Example #6
Source File: __init__.py From python2017 with MIT License | 6 votes |
def logout(request): """ Removes the authenticated user's ID from the request and flushes their session data. """ # Dispatch the signal before the user is logged out so the receivers have a # chance to find out *who* logged out. user = getattr(request, 'user', None) if hasattr(user, 'is_authenticated') and not user.is_authenticated: user = None user_logged_out.send(sender=user.__class__, request=request, user=user) # remember language choice saved to session language = request.session.get(LANGUAGE_SESSION_KEY) request.session.flush() if language is not None: request.session[LANGUAGE_SESSION_KEY] = language if hasattr(request, 'user'): from django.contrib.auth.models import AnonymousUser request.user = AnonymousUser()
Example #7
Source File: checkin.py From Servo with BSD 2-Clause "Simplified" License | 5 votes |
def init_locale(request): """Initialize locale for the check-in interface.""" lc = settings.INSTALL_LOCALE.split('.') locale.setlocale(locale.LC_TIME, lc) locale.setlocale(locale.LC_NUMERIC, lc) locale.setlocale(locale.LC_MESSAGES, lc) locale.setlocale(locale.LC_MONETARY, lc) translation.activate(settings.INSTALL_LANGUAGE) request.session[translation.LANGUAGE_SESSION_KEY] = settings.INSTALL_LANGUAGE
Example #8
Source File: utils.py From feedsubs with MIT License | 5 votes |
def set_session_language_if_necessary(request, user): preferred_language = user.um_profile.language if request.LANGUAGE_CODE == preferred_language: return if not translation.check_for_language(preferred_language): logger.warning('Invalid language %s for user %s', preferred_language, user) return logger.info('Set language %s for user session %s', preferred_language, user) translation.activate(preferred_language) request.LANGUAGE_CODE = translation.get_language() request.session[translation.LANGUAGE_SESSION_KEY] = preferred_language
Example #9
Source File: userprofile.py From esdc-ce with Apache License 2.0 | 5 votes |
def activate_locale(self, request): """ Save language and timezone settings from profile into session. The settings are read by django middleware. """ request.session[LANGUAGE_SESSION_KEY] = str(self.language) request.session[settings.TIMEZONE_SESSION_KEY] = str(self.timezone)
Example #10
Source File: test_views.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_logout_preserve_language(self): """Language stored in session is preserved after logout""" # Create a new session with language engine = import_module(settings.SESSION_ENGINE) session = engine.SessionStore() session[LANGUAGE_SESSION_KEY] = 'pl' session.save() self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key self.client.get('/logout/') self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], 'pl')
Example #11
Source File: test_views.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_logout_preserve_language(self): """Language stored in session is preserved after logout""" # Create a new session with language engine = import_module(settings.SESSION_ENGINE) session = engine.SessionStore() session[LANGUAGE_SESSION_KEY] = 'pl' session.save() self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key self.client.get('/logout/') self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], 'pl')
Example #12
Source File: views.py From speakerfight with MIT License | 5 votes |
def form_valid(self, form): self.object = form.save() translation.activate(self.object.language) self.request.session[ translation.LANGUAGE_SESSION_KEY ] = self.object.language return self.success_redirect(_(u'Language changed.'))
Example #13
Source File: views.py From foundation.mozilla.org with Mozilla Public License 2.0 | 5 votes |
def redirect_to_initiatives(request, subpath): lang = request.LANGUAGE_CODE translation.activate(lang) request.session[translation.LANGUAGE_SESSION_KEY] = lang query_string = "" if request.META['QUERY_STRING']: query_string = f'?{request.META["QUERY_STRING"]}' return redirect(f'/{request.LANGUAGE_CODE}/initiatives/{subpath}{query_string}')
Example #14
Source File: __init__.py From openhgsenti with Apache License 2.0 | 5 votes |
def logout(request): """ Removes the authenticated user's ID from the request and flushes their session data. """ # Dispatch the signal before the user is logged out so the receivers have a # chance to find out *who* logged out. user = getattr(request, 'user', None) if hasattr(user, 'is_authenticated') and not user.is_authenticated(): user = None user_logged_out.send(sender=user.__class__, request=request, user=user) # remember language choice saved to session language = request.session.get(LANGUAGE_SESSION_KEY) request.session.flush() if language is not None: request.session[LANGUAGE_SESSION_KEY] = language if hasattr(request, 'user'): from django.contrib.auth.models import AnonymousUser request.user = AnonymousUser()
Example #15
Source File: override.py From zing with GNU General Public License v3.0 | 5 votes |
def get_lang_from_session(request, supported): if hasattr(request, "session"): lang_code = request.session.get(LANGUAGE_SESSION_KEY, None) if lang_code and lang_code in supported: return lang_code return None
Example #16
Source File: views.py From rdmo with Apache License 2.0 | 5 votes |
def i18n_switcher(request, language): referer = get_referer(request, default='/') # set the new language translation.activate(language) request.session[translation.LANGUAGE_SESSION_KEY] = language return HttpResponseRedirect(referer) # def return_settings(request): # data = {} # data['default_uri_prefix'] = settings.DEFAULT_URI_PREFIX # return HttpResponse(json.dumps(data), content_type='application/json')
Example #17
Source File: views.py From mangaki with GNU Affero General Public License v3.0 | 5 votes |
def about(request, lang): if lang != '': translation.activate(lang) request.session[translation.LANGUAGE_SESSION_KEY] = lang return render(request, 'about.html')
Example #18
Source File: account.py From Servo with BSD 2-Clause "Simplified" License | 5 votes |
def settings(request): """User editing their profile.""" title = _("Profile Settings") form = ProfileForm(instance=request.user) if request.method == "POST": form = ProfileForm(request.POST, request.FILES, instance=request.user) if form.is_valid(): user = form.save() messages.success(request, _("Settings saved")) User.refresh_nomail() if form.cleaned_data['password1']: request.user.set_password(form.cleaned_data['password1']) request.user.save() lang = user.activate_locale() translation.activate(lang) request.session[translation.LANGUAGE_SESSION_KEY] = lang request.session['django_timezone'] = user.timezone return redirect(settings) else: messages.error(request, _("Error in profile data")) return render(request, "accounts/settings.html", locals())
Example #19
Source File: trans_real.py From python2017 with MIT License | 4 votes |
def get_language_from_request(request, check_path=False): """ Analyzes the request to find what language the user wants the system to show. Only languages listed in settings.LANGUAGES are taken into account. If the user requests a sublanguage where we have a main language, we send out the main language. If check_path is True, the URL path prefix will be checked for a language code, otherwise this is skipped for backwards compatibility. """ if check_path: lang_code = get_language_from_path(request.path_info) if lang_code is not None: return lang_code supported_lang_codes = get_languages() if hasattr(request, 'session'): lang_code = request.session.get(LANGUAGE_SESSION_KEY) if lang_code in supported_lang_codes and lang_code is not None and check_for_language(lang_code): return lang_code lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME) try: return get_supported_language_variant(lang_code) except LookupError: pass accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '') for accept_lang, unused in parse_accept_lang_header(accept): if accept_lang == '*': break if not language_code_re.search(accept_lang): continue try: return get_supported_language_variant(accept_lang) except LookupError: continue try: return get_supported_language_variant(settings.LANGUAGE_CODE) except LookupError: return settings.LANGUAGE_CODE
Example #20
Source File: views.py From iguana with Creative Commons Attribution Share Alike 4.0 International | 4 votes |
def post(self, request, *args, **kwargs): self.object = self.get_object() # set language key request.session[translation.LANGUAGE_SESSION_KEY] = request.POST['language'] # if a new avatar image was set, save it as the 'old' one self.avatar_kwargs = {'old_avatar_name': self.object.avatar.name} # Changing the email address should not be possible without entering the correct password!! # Normally this is a check one want in the clean-methods of those forms, but there it is not possible to access # elements of other forms, so this is a dirty workaround. # TODO maybe it is possible to provide this error to the respective clean-functions username = self.kwargs.get('username') # this should make this situation TOCTOU-safe # in case it doesn't try to use select_for_update() with transaction.atomic(): # NOTE: this has to be part of the atomic block! current_user = get_user_model().objects.get(username=username) old_email = current_user.email forms = request.POST new_email = forms['email'] wrong_password = False if old_email != new_email: try: provided_password = forms['old_password'] except (TypeError, MultiValueDictKeyError): provided_password = None if(provided_password is None or provided_password == "" or authenticate(username=username, password=provided_password) is None): wrong_password = True # NOTE: in this case we shall not call save (super) if wrong_password: messages.add_message(request, messages.ERROR, _('You can not change the email address without entering the correct password.' + 'All changes have been discarded!')) # TODO copy elements? - this might be some pain because we have to split those elements on both forms return super(EditProfilePageView, self).get(request, *args, **kwargs) response = super(EditProfilePageView, self).post(request, *args, **kwargs) current_user = get_user_model().objects.get(username=username) if response.status_code == 200: messages.add_message(request, messages.ERROR, _('Something went wrong, one or more field credentials are not fulfilled')) else: messages.add_message(request, messages.SUCCESS, _('Profile successfully edited.')) return response
Example #21
Source File: trans_real.py From python with Apache License 2.0 | 4 votes |
def get_language_from_request(request, check_path=False): """ Analyzes the request to find what language the user wants the system to show. Only languages listed in settings.LANGUAGES are taken into account. If the user requests a sublanguage where we have a main language, we send out the main language. If check_path is True, the URL path prefix will be checked for a language code, otherwise this is skipped for backwards compatibility. """ if check_path: lang_code = get_language_from_path(request.path_info) if lang_code is not None: return lang_code supported_lang_codes = get_languages() if hasattr(request, 'session'): lang_code = request.session.get(LANGUAGE_SESSION_KEY) if lang_code in supported_lang_codes and lang_code is not None and check_for_language(lang_code): return lang_code lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME) try: return get_supported_language_variant(lang_code) except LookupError: pass accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '') for accept_lang, unused in parse_accept_lang_header(accept): if accept_lang == '*': break if not language_code_re.search(accept_lang): continue try: return get_supported_language_variant(accept_lang) except LookupError: continue try: return get_supported_language_variant(settings.LANGUAGE_CODE) except LookupError: return settings.LANGUAGE_CODE
Example #22
Source File: default.py From cadasta-platform with GNU Affero General Public License v3.0 | 4 votes |
def form_valid(self, form): try: with transaction.atomic(): user = form.save(self.request) user_lang = form.cleaned_data['language'] if user_lang != translation.get_language(): translation.activate(user_lang) self.request.session[ translation.LANGUAGE_SESSION_KEY] = user_lang if user.phone: device = VerificationDevice.objects.create( user=user, unverified_phone=user.phone) device.generate_challenge() message = _("Verification token sent to {phone}") message = message.format(phone=user.phone) messages.add_message(self.request, messages.INFO, message) if user.email: send_email_confirmation(self.request, user) self.request.session['phone_verify_id'] = user.id message = _("We have created your account. You should have " "received an email or a text to verify your " "account.") messages.add_message(self.request, messages.SUCCESS, message) return super().form_valid(form) except TwilioRestException as e: if e.status >= 500: logger.exception(str(e)) msg = TWILIO_ERRORS.get('default') else: msg = TWILIO_ERRORS.get(e.code) if msg: form.add_error('phone', msg) return self.form_invalid(form) else: raise
Example #23
Source File: trans_real.py From bioforum with MIT License | 4 votes |
def get_language_from_request(request, check_path=False): """ Analyze the request to find what language the user wants the system to show. Only languages listed in settings.LANGUAGES are taken into account. If the user requests a sublanguage where we have a main language, we send out the main language. If check_path is True, the URL path prefix will be checked for a language code, otherwise this is skipped for backwards compatibility. """ if check_path: lang_code = get_language_from_path(request.path_info) if lang_code is not None: return lang_code supported_lang_codes = get_languages() if hasattr(request, 'session'): lang_code = request.session.get(LANGUAGE_SESSION_KEY) if lang_code in supported_lang_codes and lang_code is not None and check_for_language(lang_code): return lang_code lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME) try: return get_supported_language_variant(lang_code) except LookupError: pass accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '') for accept_lang, unused in parse_accept_lang_header(accept): if accept_lang == '*': break if not language_code_re.search(accept_lang): continue try: return get_supported_language_variant(accept_lang) except LookupError: continue try: return get_supported_language_variant(settings.LANGUAGE_CODE) except LookupError: return settings.LANGUAGE_CODE