Skip to content

Commit f68e40e

Browse files
committed
Introduce language picking
Activate language middleware Add middleware URLs Add new dropdown menu to navbar containing language links and move logout link there Add script to prevent language changes for pages with unsaved content Add class to all forms that should get that protection Fix formatting of some templates
1 parent 76e1d21 commit f68e40e

11 files changed

Lines changed: 75 additions & 28 deletions

File tree

collab_coursebook/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
MIDDLEWARE = [
5252
'django.middleware.security.SecurityMiddleware',
5353
'django.contrib.sessions.middleware.SessionMiddleware',
54+
'django.middleware.locale.LocaleMiddleware',
5455
'django.middleware.common.CommonMiddleware',
5556
'django.middleware.csrf.CsrfViewMiddleware',
5657
'django.contrib.auth.middleware.AuthenticationMiddleware',

collab_coursebook/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727
path('accounts/logout/', django_cas_ng.views.LogoutView.as_view(), name='cas_ng_logout'),
2828
path('accounts/callback/', django_cas_ng.views.CallbackView.as_view(), name='cas_ng_proxy_callback'),
2929
path('', include('frontend.urls', namespace='frontend')),
30+
path('i18n/', include('django.conf.urls.i18n')),
3031
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

frontend/templates/frontend/base.html

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,49 @@
1717

1818
<link rel="stylesheet" href="{% static 'css/main.css' %}">
1919

20+
<script type='text/javascript'>
21+
var changed_form = false;
22+
23+
$(document).ready(function() {
24+
// Ask before changing language if user changed main form on page
25+
$("#language-switcher").submit(function(e){
26+
if(changed_form) {
27+
if(!confirm("{% trans 'Are you sure you want to change the language now? This will clear the form!' %}"))
28+
e.preventDefault(e);
29+
}
30+
});
31+
32+
// Remember any main form edits/changes
33+
$("form.post-form :input").change(function(){
34+
if($(this).val() != '')
35+
changed_form = true;
36+
});
37+
});
38+
</script>
39+
2040
{% block imports %}{% endblock %}
2141
</head>
2242
<body>
2343

24-
{% block general_content %}
44+
{% block general_content %}
45+
{% endblock %}
2546

26-
{% endblock %}
47+
<div class="container" style="padding-top:1em;padding-bottom: 1em;">
48+
{% block footer_custom %}
49+
{% endblock %}
50+
{% footer_info as FI %}
51+
{% if FI.impress_text %}
52+
{{ FI.impress_text }} &middot;
53+
{% endif %}
54+
{% if FI.impress_url %}
55+
<a href="{{ FI.impress_url }}">{% trans "Impress" %}</a> &middot;
56+
{% endif %}
57+
{% if FI.repo_url %}
58+
<a href="{{ FI.repo_url }}">{% trans "This software is open source" %}</a>
59+
{% endif %}
60+
</div>
2761

28-
<div class="container" style="padding-top:1em;padding-bottom: 1em;">
29-
{% block footer_custom %}
30-
{% endblock %}
31-
{% footer_info as FI %}
32-
{% if FI.impress_text %}
33-
{{ FI.impress_text }} &middot;
34-
{% endif %}
35-
{% if FI.impress_url %}
36-
<a href="{{ FI.impress_url }}">{% trans "Impress" %}</a> &middot;
37-
{% endif %}
38-
{% if FI.repo_url %}
39-
<a href="{{ FI.repo_url }}">{% trans "This software is open source" %}</a>
40-
{% endif %}
41-
</div>
42-
43-
{% block bottom_script %}{% endblock %}
62+
{% block bottom_script %}{% endblock %}
4463

4564
</body>
4665
</html>

frontend/templates/frontend/comment/edit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
{% block content %}
1010
<h1>Edit comment</h1>
11-
<form action="" method="post">
11+
<form action="" class="post-form" method="post">
1212
{% csrf_token %}
1313

1414
{% bootstrap_form form %}

frontend/templates/frontend/content/addcontent.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{% endblock %}
1313

1414
{% block content %}
15-
<form method="post" enctype=multipart/form-data>
15+
<form method="post" class="post-form" enctype=multipart/form-data>
1616
{% csrf_token %}
1717
{% bootstrap_form form %}
1818
{% bootstrap_form content_type_form %}

frontend/templates/frontend/content/detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ <h5><a name="rating">{% trans "Rating" %}</a></h5>
114114

115115
<h5 style="margin-top: 30px;"><a name="comments">{% trans "Comments" %}</a></h5>
116116
{% if user.is_authenticated %}
117-
<form action="{% url 'frontend:content' course.id topic.id content.id %}" method="post">
117+
<form action="{% url 'frontend:content' course.id topic.id content.id %}" class="post-form" method="post">
118118
{% csrf_token %}
119119

120120
{% bootstrap_form comment_form %}

frontend/templates/frontend/course/create.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{% block content %}
1919
<h1>Create a new Course</h1>
2020

21-
<form method="post" enctype=multipart/form-data>
21+
<form method="post" class="post-form" enctype=multipart/form-data>
2222
{% csrf_token %}
2323

2424
{% bootstrap_form form %}

frontend/templates/frontend/course/duplicate.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{% block content %}
1919
<h1>{% trans "Duplicate Course" %}</h1>
2020

21-
<form method="post" enctype=multipart/form-data>
21+
<form method="post" class="post-form" enctype=multipart/form-data>
2222
{% csrf_token %}
2323

2424
{% bootstrap_form form %}

frontend/templates/frontend/course/edit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{% block content %}
1919
<h1>Edit Course</h1>
2020

21-
<form method="post" enctype=multipart/form-data>
21+
<form method="post" class="post-form" enctype=multipart/form-data>
2222
{% csrf_token %}
2323

2424
{% bootstrap_form form %}

frontend/templates/frontend/navbar.html

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,35 @@
1515
&nbsp;
1616
{% endif %}
1717
<a href="{% url 'frontend:profile' pk=user.profile.pk %}" class="text-decoration-none text-white">{{ user.get_username }}</a>
18-
&nbsp;
19-
<a href="{% url "cas_ng_logout" %}" class="btn btn-primary" style="">{% trans "Logout" %}</a>
18+
19+
<div class="btn-group" role="group">
20+
<div class="btn-group" role="group">
21+
<button id="btnGroupNavbar" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
22+
<div class="dropdown-menu" aria-labelledby="btnGroupNavbar" style="">
23+
<div class="dropdown-header">
24+
{% trans "Language" %}:
25+
</div>
26+
<!-- language switcher -->
27+
<form action="{% url 'set_language' %}" method="post" class="form-inline" name="language-form" id="language-switcher" style="flex-direction: column;">
28+
{% csrf_token %}
29+
<input name="next" type="hidden" value="{{ request.path }}" />
30+
31+
{% get_current_language as LANGUAGE_CODE %}
32+
{% get_available_languages as LANGUAGES %}
33+
{% get_language_info_list for LANGUAGES as languages %}
34+
35+
{% for language in languages %}
36+
<button type="submit" class="dropdown-item" name="language" value="{{ language.code }}" style="align-self: end;">
37+
{{ language.code | upper }}
38+
</button>
39+
{% endfor %}
40+
</form>
41+
<div class="dropdown-divider"></div>
42+
<a href="{% url "cas_ng_logout" %}" class="dropdown-item" style="">{% trans "Logout" %}</a>
43+
</div>
44+
</div>
45+
</div>
2046
</div>
2147
{% endif %}
2248

23-
<a href="{% url "frontend:dashboard" %}" style="color: #ffffff;text-decoration: none;"><h4 style="font-weight: bold;display: inline;">Collab Coursebook</h4></a>
49+
<a href="{% url "frontend:dashboard" %}" style="color: #ffffff;text-decoration: none;"><h4 style="font-weight: bold;display: inline;">Collab Coursebook</h4></a>

0 commit comments

Comments
 (0)