Make ClubRole.name translatable

This commit is contained in:
imperosol committed 2026-11-14 14:10:13 +01:00
1 parent aab5cc178e
commit 884940af30
8 files changed
+167 -23

No files matched your search

+15 -2
View File
@@ -302,12 +302,22 @@ class ClubRoleForm(forms.ModelForm):
class Meta:
model = ClubRole
fields = ["name", "description", "is_presidency", "is_board", "is_active"]
fields = [
"name",
"name_en",
"description",
"is_presidency",
"is_board",
"is_active",
]
widgets = {
"is_presidency": forms.HiddenInput(),
"is_board": forms.HiddenInput(),
"is_active": forms.CheckboxInput(attrs={"class": "switch"}),
}
help_texts = {
"name_en": _("Provide an english name for non-french speaking users.")
}
def clean(self):
cleaned_data = super().clean()
@@ -354,7 +364,10 @@ class ClubRoleCreateForm(forms.ModelForm):
class Meta:
model = ClubRole
fields = ["name", "description"]
fields = ["name", "name_en", "description"]
help_texts = {
"name_en": _("Provide an english name for non-french speaking users.")
}
def __init__(
self, *args, club: Club, is_presidency: bool, is_board: bool, **kwargs
@@ -0,0 +1,72 @@
# Generated by Django 5.2.18 on 2026-11-14 11:44
from typing import TYPE_CHECKING
from django.db import migrations, models
from django.db.models import F
from django.db.models.expressions import Case, Value, When
if TYPE_CHECKING:
from django.db.migrations.state import StateApps
def copy_name(apps: StateApps, schema_editor):
"""Automatically populate French names and the most common English ones"""
ClubRole = apps.get_model("club", "ClubRole")
ClubRole.objects.update(
name_fr=F("name"),
name_en=Case(
When(name="Président⸱e", then=Value("President")),
When(name="Vice-Président⸱e", then=Value("Vice-president")),
When(name="Trésorier⸱e", then=Value("Treasurer")),
When(name="Responsable communication", then=Value("Communication manager")),
When(name="Secrétaire", then=Value("Secretary")),
When(name="Responsable info", then=Value("IT manager")),
When(name="Membre du bureau", then=Value("Board member")),
When(name="Membre actif⸱ve", then=Value("Active member")),
When(name="Curieux⸱euse", then=Value("Curious")),
),
)
class Migration(migrations.Migration):
dependencies = [("club", "0019_remove_mailingsubscription_mailing_and_more")]
operations = [
migrations.AddField(
model_name="clubrole",
name="name_en",
field=models.CharField(max_length=50, null=True, verbose_name="name"),
),
migrations.AddField(
model_name="clubrole",
name="name_fr",
field=models.CharField(max_length=50, null=True, verbose_name="name"),
),
migrations.AddConstraint(
model_name="clubrole",
constraint=models.UniqueConstraint(
fields=("club", "name"),
name="clubrole_club_name_unique",
violation_error_message="There is already a role with that name",
),
),
migrations.AddConstraint(
model_name="clubrole",
constraint=models.UniqueConstraint(
fields=("club", "name_fr"),
name="clubrole_club_name_unique-fr",
violation_error_message="There is already a role with that name",
),
),
migrations.AddConstraint(
model_name="clubrole",
constraint=models.UniqueConstraint(
fields=("club", "name_en"),
name="clubrole_club_name_unique-en",
violation_error_message="There is already a role with that name",
),
),
migrations.RunPython(
copy_name, reverse_code=migrations.RunPython.noop, elidable=True
),
]
+6 -1
View File
@@ -307,7 +307,12 @@ class ClubRole(OrderedModel):
violation_error_message=_(
"A role cannot be in the presidency while not being in the board"
),
)
),
models.UniqueConstraint(
fields=("club", "name"),
name="clubrole_club_name_unique",
violation_error_message=_("There is already a role with that name"),
),
]
def __str__(self):
+3
View File
@@ -37,6 +37,9 @@
<div class="form-group">
{{ subform.name.as_field_group() }}
</div>
<div class="form-group">
{{ subform.name_en.as_field_group() }}
</div>
<div class="form-group">
{{ subform.description.as_field_group() }}
</div>
+12
View File
@@ -4,6 +4,7 @@ import pytest
from django.contrib.auth.models import Permission
from django.test import Client, TestCase
from django.urls import reverse
from django.utils import translation
from django.utils.timezone import now
from model_bakery import baker, seq
from model_bakery.recipe import Recipe
@@ -281,3 +282,14 @@ class TestClubRoleUpdate(TestCase):
# the second membership is finished, so its user shouldn't get the role
assert members[0].user.groups.contains(self.club.board_group)
assert not members[1].user.groups.contains(self.club.board_group)
def test_name_translation(self):
"""Test that setting the english name works."""
name_en = "something something"
self.payload["roles-0-name_en"] = name_en
formset = ClubRoleFormSet(data=self.payload, instance=self.club)
assert formset.is_valid()
formset.save()
self.roles[0].refresh_from_db()
with translation.override("en"):
assert self.roles[0].name == name_en
+8
View File
@@ -0,0 +1,8 @@
from modeltranslation.translator import TranslationOptions, register
from club.models import ClubRole
@register(ClubRole)
class ClubRoleTranslationOptions(TranslationOptions):
fields = ("name",)