Skip to content

Commit

Permalink
Merge pull request #1 from benbur98/vlan_app
Browse files Browse the repository at this point in the history
Implement Basic VLAN App with CRUD
  • Loading branch information
ben-burwood authored Feb 27, 2024
2 parents 4e2e393 + c557e56 commit 68eb897
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ __pycache__/

# IDE
.idea
.run
.vscode

# Environments
.env
Expand All @@ -14,3 +16,6 @@ venv/
ENV/
env.bak/
venv.bak/

# Databases
*.sqlite3
1 change: 1 addition & 0 deletions NetworkMapper/NetworkMapper/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# Application definition

INSTALLED_APPS = [
"ip.apps.IpConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand Down
3 changes: 2 additions & 1 deletion NetworkMapper/NetworkMapper/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"""

from django.contrib import admin
from django.urls import path
from django.urls import include, path

urlpatterns = [
path("admin/", admin.site.urls),
path("ip/", include("ip.urls")),
]
Empty file added NetworkMapper/ip/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions NetworkMapper/ip/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions NetworkMapper/ip/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class IpConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "ip"
8 changes: 8 additions & 0 deletions NetworkMapper/ip/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django import forms

from .models import VLAN

class VLANForm(forms.ModelForm):
class Meta:
model = VLAN
fields = ["vlan_id", "name", "description"]
30 changes: 30 additions & 0 deletions NetworkMapper/ip/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 5.0.2 on 2024-02-27 18:48

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="VLAN",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("vlan_id", models.IntegerField(unique=True)),
("name", models.CharField(max_length=100)),
("description", models.TextField(blank=True, null=True)),
],
),
]
Empty file.
14 changes: 14 additions & 0 deletions NetworkMapper/ip/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import models


class VLAN(models.Model):
vlan_id = models.IntegerField(unique=True)
name = models.CharField(max_length=100)
description = models.TextField(blank=True, null=True)

def __str__(self):
return f"{self.vlan_id} - {self.name}"

@property
def subnet(self) -> str:
return f"192.168.{self.vlan_id}.0/24"
21 changes: 21 additions & 0 deletions NetworkMapper/ip/templates/ip/vlan/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create VLAN</title>
</head>
<body>

<h1>Create VLAN</h1>

<form method="post" action="{% url 'vlan.create' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create VLAN</button>
</form>

<a href="{% url 'vlan.index' %}">Back to VLAN List</a>

</body>
</html>
19 changes: 19 additions & 0 deletions NetworkMapper/ip/templates/ip/vlan/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit VLAN</title>
</head>
<body>

<h1>Edit VLAN - {{ vlan.vlan_id }}</h1>

<form method="post" action="{% url 'vlan.edit' vlan.id %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save Changes</button>
</form>

</body>
</html>
39 changes: 39 additions & 0 deletions NetworkMapper/ip/templates/ip/vlan/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>VLAN List</title>
</head>
<body>

<a href="{% url 'vlan.create' %}">Create VLAN</a>

<h1>VLAN List</h1>

<table border="1">
<thead>
<tr>
<th>VLAN ID</th>
<th>Name</th>
<th>Description</th>
<th>Subnet</th>
</tr>
</thead>
<tbody>
{% for vlan in vlans %}
<tr>
<td>{{ vlan.vlan_id }}</td>
<td>{{ vlan.name }}</td>
<td>{{ vlan.description|default:"N/A" }}</td>
<td>{{ vlan.subnet }}</td>
<td>
<a href="{% url 'vlan.edit' vlan.id %}">Edit</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>

</body>
</html>
3 changes: 3 additions & 0 deletions NetworkMapper/ip/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions NetworkMapper/ip/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from . import views

urlpatterns = [
path("vlan", views.index, name="vlan.index"),
path("vlan/create", views.create_vlan, name="vlan.create"),
path("vlan/<int:vlan_id>/edit", views.edit_vlan, name="vlan.edit"),
]
34 changes: 34 additions & 0 deletions NetworkMapper/ip/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.shortcuts import get_object_or_404, redirect, render

from .forms import VLANForm
from .models import VLAN

def index(request):
vlans = VLAN.objects.all()
return render(request, "ip/vlan/index.html", {"vlans": vlans})


def create_vlan(request):
if request.method == "POST":
form = VLANForm(request.POST)
if form.is_valid():
form.save()
return redirect("vlan.index") # Redirect to the VLAN Index after successful creation
else:
form = VLANForm()

return render(request, "ip/vlan/create.html", {"form": form})


def edit_vlan(request, vlan_id: int):
vlan = get_object_or_404(VLAN, id=vlan_id)

if request.method == "POST":
form = VLANForm(request.POST, instance=vlan)
if form.is_valid():
form.save()
return redirect("vlan.index") # Redirect to the VLAN Index after successful edit
else:
form = VLANForm(instance=vlan)

return render(request, "ip/vlan/edit.html", {"form": form, "vlan": vlan})

0 comments on commit 68eb897

Please sign in to comment.