add display_name field to models and update related forms and templates

This commit is contained in:
Eduardo Silva
2026-03-16 16:33:12 -03:00
parent c912e7bb5f
commit 51a2535e87
9 changed files with 136 additions and 46 deletions

View File

@@ -125,20 +125,21 @@ class GatekeeperUserForm(forms.ModelForm):
class GatekeeperGroupForm(forms.ModelForm):
class Meta:
model = GatekeeperGroup
fields = ['name', 'users']
fields = ['display_name', 'users']
labels = {
'name': _('Group Name'),
'display_name': _('Group Name'),
'users': _('Members'),
}
def __init__(self, *args, **kwargs):
cancel_url = kwargs.pop('cancel_url', '#')
super().__init__(*args, **kwargs)
self.fields['display_name'].required = True
self.helper = FormHelper()
self.helper.layout = Layout(
Div(
Div('name', css_class='col-xl-12'),
Div('display_name', css_class='col-xl-12'),
css_class='row'
),
Div(
@@ -179,11 +180,11 @@ class AuthMethodForm(forms.ModelForm):
class Meta:
model = AuthMethod
fields = [
'name', 'auth_type', 'totp_secret',
'display_name', 'auth_type', 'totp_secret',
'oidc_provider', 'oidc_client_id', 'oidc_client_secret'
]
labels = {
'name': _('Name'),
'display_name': _('Name'),
'auth_type': _('Authentication Type'),
'totp_secret': _('Global TOTP Secret'),
'oidc_provider': _('OIDC Provider URL'),
@@ -195,6 +196,7 @@ class AuthMethodForm(forms.ModelForm):
cancel_url = kwargs.pop('cancel_url', '#')
super().__init__(*args, **kwargs)
self.fields['display_name'].required = True
if self.instance and self.instance.pk:
self.fields['auth_type'].disabled = True
exp_min = self.instance.session_expiration_minutes
@@ -208,7 +210,7 @@ class AuthMethodForm(forms.ModelForm):
self.helper = FormHelper()
self.helper.layout = Layout(
Div(
Div('name', css_class='col-xl-6'),
Div('display_name', css_class='col-xl-6'),
Div('auth_type', css_class='col-xl-6'),
css_class='row auth-type-group'
),

View File

@@ -0,0 +1,21 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('gatekeeper', '0010_alter_gatekeeperuser_email'),
]
operations = [
migrations.AddField(
model_name='authmethod',
name='display_name',
field=models.CharField(blank=True, max_length=128),
),
migrations.AddField(
model_name='gatekeepergroup',
name='display_name',
field=models.CharField(blank=True, max_length=128),
),
]

View File

@@ -1,11 +1,28 @@
import uuid
from django.db import models
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
def _unique_slug(model_class, display_name, exclude_pk=None, slug_field='name', filter_kwargs=None):
base = slugify(display_name) or 'item'
slug = base
counter = 1
qs = model_class.objects.all()
if exclude_pk:
qs = qs.exclude(pk=exclude_pk)
if filter_kwargs:
qs = qs.filter(**filter_kwargs)
while qs.filter(**{slug_field: slug}).exists():
slug = f"{base}-{counter}"
counter += 1
return slug
class AuthMethod(models.Model):
name = models.SlugField(max_length=64, unique=True)
display_name = models.CharField(max_length=128, blank=True)
auth_type = models.CharField(max_length=32, choices=(
('local_password', _('Local Password')),
('totp', _('One-Time Password (TOTP)')),
@@ -31,8 +48,13 @@ class AuthMethod(models.Model):
updated = models.DateTimeField(auto_now=True)
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
def save(self, *args, **kwargs):
if self.display_name:
self.name = _unique_slug(AuthMethod, self.display_name, exclude_pk=self.pk)
super().save(*args, **kwargs)
def __str__(self):
return f"{self.name} ({self.get_auth_type_display()})"
return f"{self.display_name or self.name} ({self.get_auth_type_display()})"
class Meta:
ordering = ['name']
@@ -89,14 +111,20 @@ class GatekeeperUser(models.Model):
class GatekeeperGroup(models.Model):
name = models.SlugField(max_length=64, unique=True)
display_name = models.CharField(max_length=128, blank=True)
users = models.ManyToManyField(GatekeeperUser, blank=True, related_name='groups')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
def save(self, *args, **kwargs):
if self.display_name:
self.name = _unique_slug(GatekeeperGroup, self.display_name, exclude_pk=self.pk)
super().save(*args, **kwargs)
def __str__(self):
return self.name
return self.display_name or self.name
class Meta:
ordering = ['name']