WireGuard instance form translation

This commit is contained in:
Eduardo Silva
2025-04-15 13:20:58 -03:00
parent 77ed501cfc
commit 52e0f19708
6 changed files with 150 additions and 46 deletions

View File

@@ -1,22 +1,26 @@
from django import forms
from .models import WireGuardInstance, NETMASK_CHOICES
from wgwadmlibrary.tools import is_valid_ip_or_hostname
import ipaddress
from django import forms
from django.utils.translation import gettext_lazy as _
from wgwadmlibrary.tools import is_valid_ip_or_hostname
from .models import NETMASK_CHOICES, WireGuardInstance
class WireGuardInstanceForm(forms.ModelForm):
name = forms.CharField(label='Display Name', required=False)
instance_id = forms.IntegerField(label='Instance ID')
private_key = forms.CharField(label='Private Key')
public_key = forms.CharField(label='Public Key')
hostname = forms.CharField(label='Public Address')
listen_port = forms.IntegerField(label='Listen Port')
address = forms.GenericIPAddressField(label='VPN IP Address')
netmask = forms.ChoiceField(choices=NETMASK_CHOICES, label='Netmask')
post_up = forms.CharField(label='Post Up', required=False)
post_down = forms.CharField(label='Post Down', required=False)
peer_list_refresh_interval = forms.IntegerField(label='Web Refresh Interval', initial=20)
dns_primary = forms.GenericIPAddressField(label='Primary DNS', initial='1.1.1.1', required=False)
dns_secondary = forms.GenericIPAddressField(label='Secondary DNS', initial='', required=False)
name = forms.CharField(label=_('Display Name'), required=False)
instance_id = forms.IntegerField(label=_('Instance ID'))
private_key = forms.CharField(label=_('Private Key'))
public_key = forms.CharField(label=_('Public Key'))
hostname = forms.CharField(label=_('Public Address'))
listen_port = forms.IntegerField(label=_('Listen Port'))
address = forms.GenericIPAddressField(label=_('Internal IP Address'))
netmask = forms.ChoiceField(choices=NETMASK_CHOICES, label=_('Netmask'))
post_up = forms.CharField(label=_('Post Up'), required=False)
post_down = forms.CharField(label=_('Post Down'), required=False)
peer_list_refresh_interval = forms.IntegerField(label=_('Web Refresh Interval'), initial=10)
dns_primary = forms.GenericIPAddressField(label=_('Primary DNS'), initial='1.1.1.1', required=False)
dns_secondary = forms.GenericIPAddressField(label=_('Secondary DNS'), initial='', required=False)
class Meta:
model = WireGuardInstance
@@ -35,10 +39,10 @@ class WireGuardInstanceForm(forms.ModelForm):
peer_list_refresh_interval = cleaned_data.get('peer_list_refresh_interval')
if peer_list_refresh_interval < 5:
raise forms.ValidationError('Peer List Refresh Interval must be at least 5 seconds')
raise forms.ValidationError(_('Peer List Refresh Interval must be at least 5 seconds'))
if not is_valid_ip_or_hostname(hostname):
raise forms.ValidationError('Invalid hostname or IP Address')
raise forms.ValidationError(_('Invalid hostname or IP Address'))
current_network = ipaddress.ip_network(f"{address}/{netmask}", strict=False)
all_other_instances = WireGuardInstance.objects.all()
@@ -47,7 +51,7 @@ class WireGuardInstanceForm(forms.ModelForm):
for instance in all_other_instances:
other_network = ipaddress.ip_network(f"{instance.address}/{instance.netmask}", strict=False)
if current_network.overlaps(other_network):
raise forms.ValidationError(f"The network range {current_network} overlaps with another instance's network range {other_network}.")
raise forms.ValidationError(_('The selected network range overlaps with another instance.'))
#if self.instance:
# if post_up or post_down:

View File

@@ -4,6 +4,7 @@ from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db import models
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy as _
from user_manager.models import UserAcl
@@ -125,25 +126,25 @@ def view_wireguard_manage_instance(request):
current_instance = wireguard_instances.first()
if current_instance:
page_title = f'wg{current_instance.instance_id}'
message_title = 'Update WireGuard Instance'
message_title = _('Update WireGuard Instance')
if current_instance.name:
page_title += f' ({current_instance.name})'
if request.GET.get('action') == 'delete':
message_title = 'Delete WireGuard Instance'
message_title = _('Delete WireGuard Instance')
if request.GET.get('confirmation') == 'delete wg' + str(current_instance.instance_id):
if current_instance.peer_set.all().count() > 0:
messages.warning(request, 'Error removing wg' +str(current_instance.instance_id) + '|Cannot delete WireGuard instance wg' + str(current_instance.instance_id) + '. There are still peers associated with this instance.')
messages.warning(request, _('Error removing instance: wg') + str(current_instance.instance_id) + _('|Cannot delete the requested WireGuard instance. There are still peers associated with this instance.'))
return redirect('/server/manage/?uuid=' + str(current_instance.uuid))
current_instance.delete()
messages.success(request, message_title + '|WireGuard instance wg' + str(current_instance.instance_id) + ' deleted successfully.')
messages.success(request, message_title + _('|WireGuard instance deleted: wg') + str(current_instance.instance_id))
return redirect('/server/manage/')
else:
messages.warning(request, 'Invalid confirmation' + '|Please confirm deletion of WireGuard instance wg' + str(current_instance.instance_id))
messages.warning(request, _('Invalid confirmation|Please confirm deletion of WireGuard instance: wg') + str(current_instance.instance_id))
return redirect('/server/manage/?uuid=' + str(current_instance.uuid))
else:
page_title = 'Create a new WireGuard Instance'
message_title = 'New WireGuard Instance'
page_title = _('Create a new WireGuard Instance')
message_title = _('New WireGuard Instance')
if request.method == 'POST':
form = WireGuardInstanceForm(request.POST, instance=current_instance)
@@ -151,7 +152,7 @@ def view_wireguard_manage_instance(request):
this_form = form.save(commit=False)
this_form.pending_changes = True
this_form.save()
messages.success(request, message_title + '|WireGuard instance wg' + str(form.instance.instance_id) + ' saved successfully.')
messages.success(request, message_title + '|WireGuard instance updated: wg' + str(form.instance.instance_id))
return redirect('/server/manage/?uuid=' + str(form.instance.uuid))
else:
if not current_instance: