mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-03-17 14:26:18 +00:00
add display_name field to models and update related forms and templates
This commit is contained in:
@@ -13,10 +13,9 @@ from app_gateway.models import (
|
||||
class ApplicationForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Application
|
||||
fields = ['name', 'display_name', 'upstream', 'allow_invalid_cert']
|
||||
fields = ['display_name', 'upstream', 'allow_invalid_cert']
|
||||
labels = {
|
||||
'name': _('Name'),
|
||||
'display_name': _('Display Name'),
|
||||
'display_name': _('Name'),
|
||||
'upstream': _('Upstream'),
|
||||
'allow_invalid_cert': _('Allow invalid/self-signed certificate'),
|
||||
}
|
||||
@@ -24,12 +23,12 @@ class ApplicationForm(forms.ModelForm):
|
||||
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-md-6'),
|
||||
Div('display_name', css_class='col-md-6'),
|
||||
Div('display_name', css_class='col-md-12'),
|
||||
css_class='row'
|
||||
),
|
||||
Div(
|
||||
@@ -49,12 +48,8 @@ class ApplicationForm(forms.ModelForm):
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
name = cleaned_data.get("name")
|
||||
upstream = (cleaned_data.get("upstream") or "").strip()
|
||||
|
||||
if name == "wireguard_webadmin":
|
||||
self.add_error("name", _("This is a reserved system name."))
|
||||
|
||||
if upstream:
|
||||
if "wireguard-webadmin:8000" in upstream:
|
||||
self.add_error("upstream", _("This upstream is reserved by the system."))
|
||||
@@ -103,9 +98,9 @@ class ApplicationHostForm(forms.ModelForm):
|
||||
class AccessPolicyForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = AccessPolicy
|
||||
fields = ['name', 'policy_type', 'groups', 'methods']
|
||||
fields = ['display_name', 'policy_type', 'groups', 'methods']
|
||||
labels = {
|
||||
'name': _('Name'),
|
||||
'display_name': _('Name'),
|
||||
'policy_type': _('Policy Type'),
|
||||
'groups': _('Allowed Groups'),
|
||||
'methods': _('Authentication Methods'),
|
||||
@@ -115,6 +110,7 @@ class AccessPolicyForm(forms.ModelForm):
|
||||
cancel_url = kwargs.pop('cancel_url', '#')
|
||||
policy_type = kwargs.pop('policy_type', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['display_name'].required = True
|
||||
|
||||
if self.instance and self.instance.pk:
|
||||
policy_type = self.instance.policy_type
|
||||
@@ -125,11 +121,11 @@ class AccessPolicyForm(forms.ModelForm):
|
||||
self.fields['policy_type'].widget = forms.HiddenInput()
|
||||
|
||||
self.helper = FormHelper()
|
||||
|
||||
|
||||
if policy_type in ['public', 'deny']:
|
||||
self.helper.layout = Layout(
|
||||
Div(
|
||||
Div('name', css_class='col-md-12'),
|
||||
Div('display_name', css_class='col-md-12'),
|
||||
'policy_type',
|
||||
css_class='row'
|
||||
),
|
||||
@@ -145,7 +141,7 @@ class AccessPolicyForm(forms.ModelForm):
|
||||
else:
|
||||
self.helper.layout = Layout(
|
||||
Div(
|
||||
Div('name', css_class='col-md-12'),
|
||||
Div('display_name', css_class='col-md-12'),
|
||||
'policy_type',
|
||||
css_class='row'
|
||||
),
|
||||
@@ -244,9 +240,9 @@ class ApplicationPolicyForm(forms.ModelForm):
|
||||
class ApplicationRouteForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = ApplicationRoute
|
||||
fields = ['name', 'path_prefix', 'policy', 'order']
|
||||
fields = ['display_name', 'path_prefix', 'policy', 'order']
|
||||
labels = {
|
||||
'name': _('Route Name'),
|
||||
'display_name': _('Route Name'),
|
||||
'path_prefix': _('Path Prefix'),
|
||||
'policy': _('Policy'),
|
||||
'order': _('Order'),
|
||||
@@ -255,11 +251,12 @@ class ApplicationRouteForm(forms.ModelForm):
|
||||
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-md-12'),
|
||||
Div('display_name', css_class='col-md-12'),
|
||||
css_class='row'
|
||||
),
|
||||
Div(
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('app_gateway', '0007_application_allow_invalid_cert'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='accesspolicy',
|
||||
name='display_name',
|
||||
field=models.CharField(blank=True, max_length=128),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='applicationroute',
|
||||
name='display_name',
|
||||
field=models.CharField(blank=True, max_length=128),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='applicationroute',
|
||||
name='name',
|
||||
field=models.SlugField(max_length=64),
|
||||
),
|
||||
]
|
||||
@@ -3,7 +3,7 @@ import uuid
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from gatekeeper.models import GatekeeperGroup, AuthMethod
|
||||
from gatekeeper.models import GatekeeperGroup, AuthMethod, _unique_slug
|
||||
|
||||
|
||||
class Application(models.Model):
|
||||
@@ -16,11 +16,13 @@ class Application(models.Model):
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
def __str__(self):
|
||||
def save(self, *args, **kwargs):
|
||||
if self.display_name:
|
||||
return f"{self.display_name} ({self.name})"
|
||||
else:
|
||||
return self.name
|
||||
self.name = _unique_slug(Application, self.display_name, exclude_pk=self.pk)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return self.display_name or self.name
|
||||
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
@@ -43,6 +45,7 @@ class ApplicationHost(models.Model):
|
||||
|
||||
class AccessPolicy(models.Model):
|
||||
name = models.SlugField(max_length=64, unique=True)
|
||||
display_name = models.CharField(max_length=128, blank=True)
|
||||
policy_type = models.CharField(max_length=32, choices=(('public', _('Public')), ('protected', _('Protected')), ('deny', _('Deny'))))
|
||||
groups = models.ManyToManyField(GatekeeperGroup, blank=True, related_name='policies')
|
||||
methods = models.ManyToManyField(AuthMethod, blank=True, related_name='policies')
|
||||
@@ -51,8 +54,13 @@ class AccessPolicy(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(AccessPolicy, self.display_name, exclude_pk=self.pk)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ({self.get_policy_type_display()})"
|
||||
return f"{self.display_name or self.name} ({self.get_policy_type_display()})"
|
||||
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
@@ -78,7 +86,8 @@ class ApplicationPolicy(models.Model):
|
||||
|
||||
class ApplicationRoute(models.Model):
|
||||
application = models.ForeignKey(Application, on_delete=models.CASCADE, related_name='routes')
|
||||
name = models.SlugField(max_length=64, help_text=_("Route identifier, used in export (e.g.: public_area)"))
|
||||
name = models.SlugField(max_length=64)
|
||||
display_name = models.CharField(max_length=128, blank=True)
|
||||
path_prefix = models.CharField(max_length=255)
|
||||
policy = models.ForeignKey(AccessPolicy, on_delete=models.PROTECT, related_name='routes')
|
||||
order = models.PositiveIntegerField(default=0)
|
||||
@@ -87,8 +96,16 @@ class ApplicationRoute(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(
|
||||
ApplicationRoute, self.display_name, exclude_pk=self.pk,
|
||||
filter_kwargs={'application': self.application},
|
||||
)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.application} {self.path_prefix} → {self.policy}"
|
||||
return f"{self.application} — {self.display_name or self.name} ({self.path_prefix})"
|
||||
|
||||
class Meta:
|
||||
ordering = ['application', 'order', 'path_prefix']
|
||||
|
||||
Reference in New Issue
Block a user