1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00

Site Header: Add anonymous user language select (#1319)

Currently, users must log in before they can set a language preference
on PeeringDB. This change allows anonymous users to change the language
of any page without logging in, by setting the Django language cookie.
This commit is contained in:
Daniel Van Allen
2023-03-13 14:22:08 -04:00
committed by GitHub
parent 6e7f6c4ea4
commit ae41216263
5 changed files with 71 additions and 31 deletions

View File

@@ -1111,6 +1111,7 @@ set_option("NON_ZIPCODE_COUNTRIES", non_zipcode_countries())
## Locale
LANGUAGE_CODE = "en-us"
LANGUAGE_COOKIE_AGE = 31557600 # one year
USE_I18N = True
USE_L10N = True

View File

@@ -384,6 +384,12 @@ div.landing_search div.searchbox {
width:100%;
}
#anon-language-preference {
height:auto;
width:auto;
margin-bottom:5px;
}
div.landing_search div.searchbox input[type=submit] {
font-size: 18px;
font-weight: bold;

View File

@@ -72,6 +72,15 @@ $(window).bind("load", function() {
url_request_authentication: "{% url "security-keys-request-authentication" %}"
});
$('#anon-language-preference').each(function(idx) {
$(this).on("change", function() {
Cookies.set('django_language', this.value, { expires: 365 }); // matches dj_settings.LANGUAGE_COOKIE_AGE
window.location.href=window.location.href;
window.location.reload();
// Reload the current page, without using the cache
document.location.reload(true);
})
});
});
</script>
</head>
@@ -99,38 +108,54 @@ $(window).bind("load", function() {
</div>
</div>
<div class="col-lg-4 col-md-3 col-sm-3 col-xs-12 user-container">
<div class="nav right toolbar">
{% if request.user.is_authenticated %}
<div class="user">
<a href="/profile">{{ request.user.username }}</a>
{% if not request.user.is_verified_user %}
<div class="status">({% if not request.user.email %}<span class="attention">!</span>{%endif %}
{% if request.user.email_confirmed %}
<a href="/profile">{% trans "pending" %}</a>
{% else %}
<a href="/profile">{% trans "unverified" %}</a>
{% endif %})
</div>
<div class="row">
<div class="nav right toolbar">
{% if request.user.is_authenticated %}
<div class="user">
<a href="/profile">{{ request.user.username }}</a>
{% if not request.user.is_verified_user %}
<div class="status">({% if not request.user.email %}<span class="attention">!</span>{%endif %}
{% if request.user.email_confirmed %}
<a href="/profile">{% trans "pending" %}</a>
{% else %}
<a href="/profile">{% trans "unverified" %}</a>
{% endif %})
</div>
{% endif %}
</div>
<div class="dropdown" style="display:inline-block">
<a class="btn btn-default dropdown-toggle" id="usermenu" data-bs-toggle="dropdown" aria-haspopup="true">
<img class="img-responsive" src="{{ STATIC_URL }}hamburger-colorful.png" alt="navigation" title="navigation" />
</a>
<ul class="dropdown-menu dropdown-menu-right" aria-labelledby="usermenu">
{% for org in user.organizations %}
<li><a href="/org/{{ org.id }}">{{ org.name }}</a></li>
{% endfor %}
{% if request.user.is_verified_user %}
<li><a href="/suggest/fac">{% trans "Suggest Facility" %}</a></li>
{% endif %}
<li><a href="/profile">{% trans "Profile" %}</a></li>
<li><a href="/logout">{% trans "Logout" %}</a></li>
</ul>
</div>
{% else %}
<a href="/register">{% trans "Register" %}</a> {% trans "or" %} <a href="{% url "two_factor:login" %}?next={{request.path}}" class="btn btn-default">{% trans "Login" %}</a>
{% endif %}
</div>
<div class="dropdown" style="display:inline-block">
<a class="btn btn-default dropdown-toggle" id="usermenu" data-bs-toggle="dropdown" aria-haspopup="true">
<img class="img-responsive" src="{{ STATIC_URL }}hamburger-colorful.png" alt="navigation" title="navigation" />
</a>
<ul class="dropdown-menu dropdown-menu-right" aria-labelledby="usermenu">
{% for org in user.organizations %}
<li><a href="/org/{{ org.id }}">{{ org.name }}</a></li>
</div>
<div class="row">
<div class="pull-right">
<select name="language" id="anon-language-preference" class="form-control">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% for language in LANGUAGES %}
{% get_language_info for language.0 as li %}
<option value="{{ language.0 }}"{% if language.0 == LANGUAGE_CODE %} selected{% endif %}>
{{ li.name_translated }} ({{ li.name_local }})
</option>
{% endfor %}
{% if request.user.is_verified_user %}
<li><a href="/suggest/fac">{% trans "Suggest Facility" %}</a></li>
{% endif %}
<li><a href="/profile">{% trans "Profile" %}</a></li>
<li><a href="/logout">{% trans "Logout" %}</a></li>
</ul>
</select>
</div>
{% else %}
<a href="/register">{% trans "Register" %}</a> {% trans "or" %} <a href="{% url "two_factor:login" %}?next={{request.path}}" class="btn btn-default">{% trans "Login" %}</a>
{% endif %}
</div>
</div>
</div>

View File

@@ -17,7 +17,7 @@
data-edit-sorted="yes"
data-edit-required="no"
data-edit-name="locale"
data-edit-value="{{ LANGUAGE_CODE }}"
data-edit-value="{{ user.locale }}"
data-edit-reset-value=""></div>
<a class="btn btn-default" data-edit-action="submit">{% trans "Set language preference" %}</a>
</div>

View File

@@ -592,7 +592,11 @@ def view_set_user_locale(request):
translation.activate(loc)
response = JsonResponse({"status": "ok"})
response.set_cookie(dj_settings.LANGUAGE_COOKIE_NAME, loc)
response.set_cookie(
dj_settings.LANGUAGE_COOKIE_NAME,
loc,
max_age=dj_settings.LANGUAGE_COOKIE_AGE,
)
return response
@@ -3134,7 +3138,11 @@ class LoginView(TwoFactorLoginView):
self.request.user,
max_age=dj_settings.OAUTH_COOKIE_MAX_AGE,
)
response.set_cookie(dj_settings.LANGUAGE_COOKIE_NAME, user_language)
response.set_cookie(
dj_settings.LANGUAGE_COOKIE_NAME,
user_language,
max_age=dj_settings.LANGUAGE_COOKIE_AGE,
)
return response