mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2025-08-04 18:24:31 +02:00
DNS settings first commit and static host management
This commit is contained in:
parent
2e8ac8669c
commit
13702c664e
13 changed files with 251 additions and 0 deletions
0
dns/__init__.py
Normal file
0
dns/__init__.py
Normal file
3
dns/admin.py
Normal file
3
dns/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
dns/apps.py
Normal file
6
dns/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DnsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'dns'
|
50
dns/forms.py
Normal file
50
dns/forms.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
import requests
|
||||
from .models import DNSSettings, StaticHost
|
||||
|
||||
from django import forms
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, Div, Field, HTML
|
||||
from crispy_forms.bootstrap import FormActions, StrictButton
|
||||
from django.core.exceptions import ValidationError
|
||||
from datetime import datetime
|
||||
from django.core.exceptions import ValidationError
|
||||
import re
|
||||
|
||||
|
||||
|
||||
class StaticHostForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = StaticHost
|
||||
fields = ['hostname', 'ip_address']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(StaticHostForm, self).__init__(*args, **kwargs)
|
||||
self.helper = FormHelper()
|
||||
self.helper.form_method = 'post'
|
||||
if self.instance.pk:
|
||||
delete_html = "<a href='javascript:void(0)' class='btn btn-outline-danger' data-command='delete' onclick='openCommandDialog(this)'>Delete</a>"
|
||||
else:
|
||||
delete_html = ''
|
||||
self.helper.layout = Layout(
|
||||
Fieldset(
|
||||
'Static DNS',
|
||||
Div(
|
||||
Field('hostname', css_class='form-control'),
|
||||
Field('ip_address', css_class='form-control'),
|
||||
css_class='col-md-6'
|
||||
),
|
||||
),
|
||||
FormActions(
|
||||
Submit('save', 'Save', css_class='btn btn-primary'),
|
||||
HTML('<a class="btn btn-outline-primary" href="/dns/">Back</a> '),
|
||||
HTML(delete_html),
|
||||
)
|
||||
)
|
||||
|
||||
def clean(self):
|
||||
hostname = self.cleaned_data.get('hostname')
|
||||
if hostname:
|
||||
regex = r'^[a-zA-Z][a-zA-Z0-9-\.]*[a-zA-Z0-9]$'
|
||||
if not re.match(regex, hostname):
|
||||
raise ValidationError('Invalid hostname')
|
||||
return
|
39
dns/migrations/0001_initial.py
Normal file
39
dns/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
# Generated by Django 5.0.4 on 2024-04-26 17:42
|
||||
|
||||
import uuid
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='DNSSettings',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(default='dns_settings', max_length=100)),
|
||||
('dns_primary', models.GenericIPAddressField(blank=True, default='1.1.1.1', null=True)),
|
||||
('dns_secondary', models.GenericIPAddressField(blank=True, default='1.0.0.1', null=True)),
|
||||
('pending_changes', models.BooleanField(default=True)),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('updated', models.DateTimeField(auto_now=True)),
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='StaticHost',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('hostname', models.CharField(max_length=100, unique=True)),
|
||||
('ip_address', models.GenericIPAddressField()),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('updated', models.DateTimeField(auto_now=True)),
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
|
||||
],
|
||||
),
|
||||
]
|
0
dns/migrations/__init__.py
Normal file
0
dns/migrations/__init__.py
Normal file
25
dns/models.py
Normal file
25
dns/models.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
from django.db import models
|
||||
import uuid
|
||||
|
||||
|
||||
class DNSSettings(models.Model):
|
||||
name = models.CharField(default='dns_settings', max_length=100)
|
||||
dns_primary = models.GenericIPAddressField(blank=True, null=True, default='1.1.1.1')
|
||||
dns_secondary = models.GenericIPAddressField(blank=True, null=True, default='1.0.0.1')
|
||||
pending_changes = models.BooleanField(default=True)
|
||||
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
|
||||
class StaticHost(models.Model):
|
||||
hostname = models.CharField(max_length=100, unique=True)
|
||||
ip_address = models.GenericIPAddressField()
|
||||
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
def __str__(self):
|
||||
return self.hostname
|
3
dns/tests.py
Normal file
3
dns/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
53
dns/views.py
Normal file
53
dns/views.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.contrib import messages
|
||||
from user_manager.models import UserAcl
|
||||
from .models import DNSSettings, StaticHost
|
||||
from .forms import StaticHostForm
|
||||
|
||||
|
||||
@login_required
|
||||
def view_static_host_list(request):
|
||||
dns_settings, _ = DNSSettings.objects.get_or_create(name='dns_settings')
|
||||
static_host_list = StaticHost.objects.all().order_by('hostname')
|
||||
context = {
|
||||
'dns_settings': dns_settings,
|
||||
'static_host_list': static_host_list,
|
||||
}
|
||||
return render(request, 'dns/static_host_list.html', context=context)
|
||||
|
||||
|
||||
@login_required
|
||||
def view_manage_static_host(request):
|
||||
if not UserAcl.objects.filter(user=request.user).filter(user_level__gte=40).exists():
|
||||
return render(request, 'access_denied.html', {'page_title': 'Access Denied'})
|
||||
dns_settings, _ = DNSSettings.objects.get_or_create(name='dns_settings')
|
||||
if request.GET.get('uuid'):
|
||||
static_dns = get_object_or_404(StaticHost, uuid=request.GET.get('uuid'))
|
||||
if request.GET.get('action') == 'delete':
|
||||
if request.GET.get('confirmation') == 'delete':
|
||||
static_dns.delete()
|
||||
dns_settings.pending_changes = True
|
||||
dns_settings.save()
|
||||
messages.success(request, 'Static DNS deleted successfully')
|
||||
return redirect('/dns/')
|
||||
else:
|
||||
messages.warning(request, 'Static DNS not deleted|Invalid confirmation')
|
||||
return redirect('/dns/')
|
||||
else:
|
||||
static_dns = None
|
||||
|
||||
form = StaticHostForm(request.POST or None, instance=static_dns)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
dns_settings.pending_changes = True
|
||||
dns_settings.save()
|
||||
messages.success(request, 'Static DNS saved successfully')
|
||||
return redirect('/dns/')
|
||||
|
||||
context = {
|
||||
'dns_settings': dns_settings,
|
||||
'form': form,
|
||||
'instance': static_dns,
|
||||
}
|
||||
return render(request, 'generic_form.html', context=context)
|
Loading…
Add table
Add a link
Reference in a new issue