mirror of
https://github.com/jlengrand/bugsink.git
synced 2026-03-10 08:01:17 +00:00
Push User.language choices callable to the model
as per the comment: possible from Django 5.0 up
This commit is contained in:
@@ -9,8 +9,7 @@ from django.core.exceptions import ValidationError
|
||||
from django.contrib.auth import password_validation
|
||||
from django.forms import ModelForm
|
||||
from django.utils.html import escape, mark_safe
|
||||
from django.utils.translation import gettext_lazy as _, get_language_info
|
||||
from django.conf import settings
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
TRUE_FALSE_CHOICES = (
|
||||
@@ -133,18 +132,6 @@ class SetPasswordForm(BaseSetPasswordForm):
|
||||
self.fields['new_password2'].help_text = None # "Confirm password" is descriptive enough
|
||||
|
||||
|
||||
def language_choices():
|
||||
items = [("auto", _("Auto (browser preference)"))]
|
||||
|
||||
for code, _label in settings.LANGUAGES:
|
||||
info = get_language_info(code)
|
||||
label = info["name_local"] \
|
||||
if info["name_local"] == info["name_translated"] \
|
||||
else f"{info['name_local']} ({info['name_translated']})"
|
||||
items.append((code, label))
|
||||
return items
|
||||
|
||||
|
||||
class PreferencesForm(ModelForm):
|
||||
# I haven't gotten a decent display for checkboxes in forms yet; the quickest hack around this is a ChoiceField
|
||||
send_email_alerts = forms.ChoiceField(
|
||||
@@ -155,12 +142,6 @@ class PreferencesForm(ModelForm):
|
||||
required=True,
|
||||
widget=forms.Select(),
|
||||
)
|
||||
language = forms.ChoiceField(
|
||||
label=_("Language"),
|
||||
choices=language_choices,
|
||||
required=True,
|
||||
widget=forms.Select(),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
|
||||
19
users/migrations/0004_alter_user_language.py
Normal file
19
users/migrations/0004_alter_user_language.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import users.models
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("users", "0003_user_language"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="user",
|
||||
name="language",
|
||||
field=models.CharField(
|
||||
choices=users.models.language_choices, default="auto", max_length=10
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -3,7 +3,19 @@ import secrets
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.conf import settings
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.translation import gettext_lazy as _, get_language_info
|
||||
|
||||
|
||||
def language_choices():
|
||||
items = [("auto", _("Auto (browser preference)"))]
|
||||
|
||||
for code, _label in settings.LANGUAGES:
|
||||
info = get_language_info(code)
|
||||
label = info["name_local"] \
|
||||
if info["name_local"] == info["name_translated"] \
|
||||
else f"{info['name_local']} ({info['name_translated']})"
|
||||
items.append((code, label))
|
||||
return items
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
@@ -30,10 +42,9 @@ class User(AbstractUser):
|
||||
blank=False,
|
||||
)
|
||||
language = models.CharField(
|
||||
_("Language"),
|
||||
max_length=10,
|
||||
# choices intentionally not set, we don't want changes to trigger migrations; the actual choices are set in
|
||||
# forms.py; in Django 5.0 and up we can instead used a callable here
|
||||
# choices=...
|
||||
choices=language_choices,
|
||||
default="auto",
|
||||
blank=False,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user