Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #768 from liqd/rm-2019-06-user-language
Browse files Browse the repository at this point in the history
Add language in user settings
  • Loading branch information
Magdalena Noffke committed Jul 2, 2019
2 parents c55eca8 + 83bdb58 commit 5bf143f
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 13 deletions.
3 changes: 2 additions & 1 deletion apps/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ class Meta:
'facebook_handle',
'twitter_handle',
'get_notifications',
'get_newsletters'
'get_newsletters',
'language'
]
2 changes: 2 additions & 0 deletions apps/account/templates/a4_candy_account/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ <h1 class="u-first-heading">{% trans 'Your profile' %}</h1>

{% include 'a4_candy_contrib/includes/form_checkbox_field.html' with field=form.get_newsletters %}

{% include 'a4_candy_contrib/includes/form_field.html' with field=form.language %}

<div class="u-spacer-bottom">
<button type="submit" class="btn btn--primary">{% trans 'Save changes'%}</button>
</div>
Expand Down
7 changes: 7 additions & 0 deletions apps/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.views.generic.base import RedirectView

from apps.users.models import User
from apps.users.utils import set_session_language

from . import forms

Expand All @@ -29,3 +30,9 @@ def get_object(self):

def get_success_url(self):
return self.request.path

def form_valid(self, form):
set_session_language(self.request.user.email,
form.cleaned_data['language'],
self.request)
return super(ProfileUpdateView, self).form_valid(form)
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
from adhocracy4.actions.models import Action
from adhocracy4.actions.verbs import Verbs
from adhocracy4.comments.models import Comment
from adhocracy4.emails import Email
from adhocracy4.emails.mixins import SyncEmailMixin
from adhocracy4.projects.models import Project
from adhocracy4.reports import emails as reports_emails
from adhocracy4.reports.models import Report
from apps.ideas.models import Idea
from apps.notifications import emails as notification_emails
from apps.projects import models as project_models
from apps.users.emails import EmailWithUserLanguage as Email

User = get_user_model()

Expand Down
2 changes: 1 addition & 1 deletion apps/newsletters/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from django.conf import settings
from django.contrib import auth

from adhocracy4.emails import Email
from adhocracy4.emails.mixins import ReportToAdminEmailMixin
from apps.users.emails import EmailWithUserLanguage as Email

Organisation = apps.get_model(settings.A4_ORGANISATIONS_MODEL)
User = auth.get_user_model()
Expand Down
2 changes: 1 addition & 1 deletion apps/notifications/emails.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import auth

from adhocracy4.emails import Email
from apps.users.emails import EmailWithUserLanguage as Email

User = auth.get_user_model()

Expand Down
2 changes: 1 addition & 1 deletion apps/notifications/management/commands/send_test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from adhocracy4.actions.models import Action
from adhocracy4.actions.verbs import Verbs
from adhocracy4.comments.models import Comment
from adhocracy4.emails import Email
from adhocracy4.emails.mixins import SyncEmailMixin
from adhocracy4.projects.models import Project
from adhocracy4.reports import emails as reports_emails
Expand All @@ -19,6 +18,7 @@
from apps.notifications import emails as notification_emails
from apps.organisations.models import Organisation
from apps.projects import models as project_models
from apps.users.emails import EmailWithUserLanguage as Email

User = get_user_model()

Expand Down
2 changes: 1 addition & 1 deletion apps/projects/emails.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from adhocracy4.emails import Email
from apps.projects import tasks
from apps.users.emails import EmailWithUserLanguage as Email


class InviteParticipantEmail(Email):
Expand Down
9 changes: 9 additions & 0 deletions apps/users/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth.signals import user_logged_in
from django.utils.translation import ugettext_lazy as _

default_app_config = 'apps.users.apps.Config'
Expand All @@ -7,3 +8,11 @@
'only letters, digits, spaces and @/./+/-/_ '
'characters. It must start with a digit or a '
'letter.')


def set_language(sender, user, request, **kwargs):
from .utils import set_session_language
set_session_language(user, None, request)


user_logged_in.connect(set_language)
2 changes: 1 addition & 1 deletion apps/users/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from django.conf import settings
from django.utils.http import is_safe_url

from adhocracy4.emails import Email
from adhocracy4.emails.mixins import SyncEmailMixin
from apps.users import USERNAME_INVALID_MESSAGE
from apps.users import USERNAME_REGEX
from apps.users.emails import EmailWithUserLanguage as Email


class UserAccountEmail(SyncEmailMixin, Email):
Expand Down
2 changes: 1 addition & 1 deletion apps/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class UserAdmin(auth.admin.UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'email', 'password')}),
(None, {'fields': ('username', 'email', 'language', 'password')}),
(_('Permissions'), {'fields': ('is_staff', 'is_superuser')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
Expand Down
15 changes: 15 additions & 0 deletions apps/users/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from adhocracy4.emails import Email

from .models import User


class EmailWithUserLanguage(Email):

def get_languages(self, receiver):
res = []
try:
language = User.objects.get(email=receiver).language
res = [language, self.fallback_language]
except User.DoesNotExist:
res = super(EmailWithUserLanguage, self).get_languages(receiver)
return res
20 changes: 20 additions & 0 deletions apps/users/migrations/0008_user_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.21 on 2019-06-27 11:32
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('a4_candy_users', '0007_rename_table_to_defaults'),
]

operations = [
migrations.AddField(
model_name='user',
name='language',
field=models.CharField(choices=[('en', 'English'), ('de', 'German')], default='de', max_length=4, verbose_name='Language'),
),
]
10 changes: 10 additions & 0 deletions apps/users/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf import settings
from django.contrib.auth import models as auth_models
from django.core import validators
from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
from django.utils.translation import get_language
from django.utils.translation import ugettext_lazy as _

from adhocracy4.images.fields import ConfiguredImageField
Expand Down Expand Up @@ -100,6 +102,13 @@ class User(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
verbose_name=_('Avatar picture'),
)

language = models.CharField(
verbose_name=_('Language'),
choices=settings.LANGUAGES,
default=settings.DEFAULT_USER_LANGUAGE_CODE,
max_length=4,
)

objects = auth_models.UserManager()

USERNAME_FIELD = 'email'
Expand All @@ -120,6 +129,7 @@ def signup(self, username, email, commit=True):
"""Update the fields required for sign-up."""
self.username = username
self.email = email
self.language = get_language()
if commit:
self.save()

Expand Down
12 changes: 12 additions & 0 deletions apps/users/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.utils.translation import LANGUAGE_SESSION_KEY
from django.utils.translation import activate

from .models import User


def set_session_language(user_email, language=None, request=None):
if not language:
language = User.objects.get(email=user_email).language
activate(language)
if hasattr(request, 'session'):
request.session[LANGUAGE_SESSION_KEY] = language
1 change: 1 addition & 0 deletions liqd_product/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en'
DEFAULT_USER_LANGUAGE_CODE = 'de'

TIME_ZONE = 'Europe/Berlin'

Expand Down
12 changes: 8 additions & 4 deletions liqd_product/fixtures/users-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"password": "pbkdf2_sha256$20000$2sgrOX9fSpHE$PctfUABBDXH0lbjC6yIZHOwOJXDQhOp8l8b8MhfQTgY=",
"is_staff": true,
"is_active": true,
"is_superuser": true
"is_superuser": true,
"language": "de"
}
},
{
Expand All @@ -30,7 +31,8 @@
"password": "pbkdf2_sha256$20000$qMYSzezfIiw3$w3A0xY/kOgE8yA4m3RDFItXTqWCV3N7v2CLy2fD8gyw=",
"is_staff": false,
"is_active": true,
"is_superuser": false
"is_superuser": false,
"language": "en"
}
},
{
Expand All @@ -52,7 +54,8 @@
"password": "pbkdf2_sha256$36000$cV8dNCt9KQvy$9B6aNADd5mu9icBCxH87F+5QDdI/H7JdiP06Ua16vsA=",
"is_staff": false,
"is_active": true,
"is_superuser": false
"is_superuser": false,
"language": "en"
}
},
{
Expand All @@ -74,7 +77,8 @@
"password": "pbkdf2_sha256$36000$S4eWhSbehLwc$AW8pzKRfrJZz9bO135VklnIdAPNhUfe8KzJL6FvQwLg=",
"is_staff": false,
"is_active": true,
"is_superuser": false
"is_superuser": false,
"language": "de"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2478,7 +2478,7 @@ msgstr "Englisch"

#: liqd_product/config/settings/base.py:187
msgid "German"
msgstr "Deutsche"
msgstr "Deutsch"

#: liqd_product/config/settings/base.py:406
msgid "Pin without icon"
Expand Down

0 comments on commit 5bf143f

Please sign in to comment.