addons-server/apps/localizers/decorators.py

32 строки
1.0 KiB
Python

from functools import wraps
from django.conf import settings
from django.http import Http404
from django.shortcuts import redirect
from amo.urlresolvers import reverse
def locale_switcher(f):
"""Decorator redirecting clicks on the locale switcher dropdown."""
@wraps(f)
def decorated(request, *args, **kwargs):
new_userlang = request.GET.get('userlang')
if new_userlang in settings.AMO_LANGUAGES + settings.HIDDEN_LANGUAGES:
kwargs['locale_code'] = new_userlang
return redirect(reverse(decorated, args=args, kwargs=kwargs))
else:
return f(request, *args, **kwargs)
return decorated
def valid_locale(f):
"""Decorator validating locale code for per-language pages."""
@wraps(f)
def decorated(request, locale_code, *args, **kwargs):
if locale_code not in (settings.AMO_LANGUAGES +
settings.HIDDEN_LANGUAGES):
raise Http404
return f(request, locale_code, *args, **kwargs)
return decorated