-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from benbur98/vlan_app
Implement Basic VLAN App with CRUD
- Loading branch information
Showing
16 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |