Skip to content

Commit

Permalink
Electrical App (#9)
Browse files Browse the repository at this point in the history
Voltage Config Value

Circuit Model/CRUD Pages
  • Loading branch information
ben-burwood authored Mar 18, 2024
1 parent 114c68f commit 5b3d833
Show file tree
Hide file tree
Showing 25 changed files with 325 additions and 10 deletions.
1 change: 1 addition & 0 deletions NetworkMapper/NetworkMapper/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
INSTALLED_APPS = [
"ip.apps.IpConfig",
"mqtt.apps.MqttConfig",
"electric.apps.ElectricConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand Down
5 changes: 3 additions & 2 deletions NetworkMapper/NetworkMapper/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

urlpatterns = [
path("admin/", admin.site.urls),
path("ip/", include("ip.urls")),
path("mqtt/", include("mqtt.urls")),
path("ip/", include("ip.urls"), name="ip"),
path("mqtt/", include("mqtt.urls"), name="mqtt"),
path("electric/", include("electric.urls"), name="electric"),
]
5 changes: 3 additions & 2 deletions NetworkMapper/common/templates/base/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<div class="sidebar">
<ul class="list-unstyled mt-4">
<li><a href="{% url 'index' %}">LAN Home</a></li>
<li><a href="{% url 'circuit.index' %}">Circuits</a></li>
<li><a href="{% url 'ip_index' %}">LAN</a></li>
<li><a href="{% url 'vlan.index' %}">VLAN</a></li>
<li><a href="{% url 'wifi.index' %}">WiFi</a></li>
<li><a href="{% url 'device.index' %}">Devices</a></li>
<li><a href="{% url 'index' %}">MQTT Home</a></li>
<li><a href="{% url 'mqtt_index' %}">MQTT</a></li>
</ul>
</div>

Expand Down
Empty file.
3 changes: 3 additions & 0 deletions NetworkMapper/electric/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/electric/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


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

from .models.circuits import Circuit

class CircuitForm(forms.ModelForm):

breaker_type = forms.ChoiceField(choices=Circuit.BREAKER_TYPES, widget=forms.Select(attrs={"class": "form-control"}))
circuit_type = forms.ChoiceField(choices=Circuit.CIRCUIT_TYPES, widget=forms.Select(attrs={"class": "form-control"}))

spd_protection = forms.BooleanField(label="SPD Protection", required=False)

description = forms.CharField(widget=forms.Textarea(attrs={"style": "height:50px;"}), required=False)

class Meta:
model = Circuit
fields = ["name", "current_rating", "breaker_type", "spd_protection", "wire_diameter", "circuit_type", "description"]
57 changes: 57 additions & 0 deletions NetworkMapper/electric/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Generated by Django 5.0.2 on 2024-03-18 22:32

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Circuit",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=100)),
("current_rating", models.IntegerField()),
(
"breaker_type",
models.CharField(
choices=[
("fuse", "Fuse"),
("mcb", "MCB"),
("rcd", "RCD"),
("gfci", "GFCI"),
],
max_length=10,
),
),
("spd_protection", models.BooleanField()),
("wire_diameter", models.FloatField()),
(
"circuit_type",
models.CharField(
choices=[
("lighting", "Lighting"),
("outlet", "Outlet"),
("appliance", "Appliance"),
("heating", "Heating"),
("cooling", "Cooling"),
],
max_length=20,
),
),
("description", models.TextField(blank=True)),
],
),
]
Empty file.
1 change: 1 addition & 0 deletions NetworkMapper/electric/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .circuits import Circuit
34 changes: 34 additions & 0 deletions NetworkMapper/electric/models/circuits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from common.utils.config import get_config_value
from django.db import models


class Circuit(models.Model):
BREAKER_TYPES = (
("fuse", "Fuse"),
("mcb", "MCB"),
("rcd", "RCD"),
("gfci", "GFCI"),
)

CIRCUIT_TYPES = (
("lighting", "Lighting"),
("outlet", "Outlets"),
("appliance", "Appliance"),
("heating", "Heating"),
("cooling", "Cooling"),
)

name = models.CharField(max_length=100)
current_rating = models.IntegerField()
breaker_type = models.CharField(max_length=10, choices=BREAKER_TYPES)
spd_protection = models.BooleanField() # Surge Protection Device
wire_diameter = models.FloatField()
circuit_type = models.CharField(max_length=20, choices=CIRCUIT_TYPES)
description = models.TextField(blank=True)

@property
def power_rating(self) -> float:
return self.current_rating * get_config_value("voltage", 0)

def __str__(self):
return self.name
17 changes: 17 additions & 0 deletions NetworkMapper/electric/templates/electric/circuit/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "base/crud.html" %}

{% block title_content %}
Create Circuit
{% endblock %}

{% block header_content %}
Create Circuit
{% endblock %}

{% block form_action %}
{% url 'circuit.create' %}
{% endblock %}

{% block back_link %}
{% url 'circuit.index' %}
{% endblock %}
17 changes: 17 additions & 0 deletions NetworkMapper/electric/templates/electric/circuit/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "base/crud.html" %}

{% block title_content %}
Edit Circuit
{% endblock %}

{% block header_content %}
Edit Circuit - {{ circuit.name }}
{% endblock %}

{% block form_action %}
{% url 'circuit.edit' circuit.id %}
{% endblock %}

{% block back_link %}
{% url 'circuit.index' %}
{% endblock %}
33 changes: 33 additions & 0 deletions NetworkMapper/electric/templates/electric/circuit/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends "base/index.html" %}

{% block title_content %}
Circuits
{% endblock %}

{% block header_content %}
Circuits
{% endblock %}

{% block create_link %}
{% url 'circuit.create' %}
{% endblock %}

{% block table_head %}
<tr>
<th>Circuit</th>
</tr>
{% endblock %}

{% block table_body %}
{% for circuit in circuits %}
<tr>
<td>{{ circuit.name }}</td>
<td>
<a href="{% url 'circuit.edit' circuit.id %}">Edit</a>
</td>
<td>
<a href="{% url 'circuit.delete' circuit.id %}">Delete</a>
</td>
</tr>
{% endfor %}
{% endblock %}
27 changes: 27 additions & 0 deletions NetworkMapper/electric/templates/electric/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "base/master.html" %}


{% block title %}
Electric
{% endblock %}


{% block content %}

<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-5">
<h2>Voltage</h2>
<form method="post" action="{% url 'electric_index' %}">
{% csrf_token %}
<div class="form-group d-flex">
<label for="voltage">Voltage:</label>
<input type="text" id="voltage" name="voltage" value="{{ voltage }}" class="form-control">
<button type="submit" name="voltage-submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>

{% endblock %}
3 changes: 3 additions & 0 deletions NetworkMapper/electric/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.
11 changes: 11 additions & 0 deletions NetworkMapper/electric/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path

from . import views

urlpatterns = [
path("", views.index.index, name="electric_index"),
path("circuit", views.circuit.index, name="circuit.index"),
path("circuit/create", views.circuit.create, name="circuit.create"),
path("circuit/<int:id>/edit", views.circuit.edit, name="circuit.edit"),
path("circuit/<int:id>/delete", views.circuit.delete, name="circuit.delete"),
]
1 change: 1 addition & 0 deletions NetworkMapper/electric/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import circuit, index
40 changes: 40 additions & 0 deletions NetworkMapper/electric/views/circuit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.shortcuts import get_object_or_404, redirect, render

from ..forms import CircuitForm
from ..models.circuits import Circuit

def index(request):
circuits = Circuit.objects.all()
return render(request, "electric/circuit/index.html", {"circuits": circuits})


def create(request):
if request.method == "POST":
form = CircuitForm(request.POST)
if form.is_valid():
form.save()
return redirect("circuit.index")
else:
form = CircuitForm()

return render(request, "electric/circuit/create.html", {"form": form})


def edit(request, id: int):
circuit = get_object_or_404(Circuit, id=id)

if request.method == "POST":
form = CircuitForm(request.POST, instance=circuit)
if form.is_valid():
form.save()
return redirect("circuit.index")
else:
form = CircuitForm(instance=circuit)

return render(request, "electric/circuit/edit.html", {"form": form, "circuit": circuit})


def delete(request, id: int):
circuit = get_object_or_404(Circuit, id=id)
circuit.delete()
return redirect("circuit.index")
15 changes: 15 additions & 0 deletions NetworkMapper/electric/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from common.utils.config import get_config_value, update_config_value
from django.shortcuts import redirect, render
from django.urls import reverse


def index(request):
if request.method == "POST":
if "voltage-submit" in request.POST:
voltage = request.POST.get("voltage")
update_config_value("voltage", voltage)
return redirect(reverse("electric_index"))

voltage = get_config_value("voltage", 0)

return render(request, "electric/index.html", {"voltage": voltage})
31 changes: 31 additions & 0 deletions NetworkMapper/ip/migrations/0004_iprange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 5.0.2 on 2024-03-18 22:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("ip", "0003_clientdevice"),
]

operations = [
migrations.CreateModel(
name="IpRange",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("start_address", models.GenericIPAddressField()),
("end_address", models.GenericIPAddressField()),
("num_addresses", models.PositiveIntegerField()),
("description", models.TextField(blank=True, null=True)),
],
),
]
4 changes: 2 additions & 2 deletions NetworkMapper/ip/templates/ip/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="row justify-content-center">
<div class="col-md-5">
<h2>Base IP</h2>
<form method="post" action="{% url 'index' %}">
<form method="post" action="{% url 'ip_index' %}">
{% csrf_token %}
<div class="form-group d-flex">
<label for="base_ip">Base IP:</label>
Expand All @@ -24,7 +24,7 @@ <h2>Base IP</h2>

<div class="col-md-5">
<h2>Route IP</h2>
<form method="post" action="{% url 'index' %}">
<form method="post" action="{% url 'ip_index' %}">
{% csrf_token %}
<div class="form-group d-flex">
<label for="router_ip">Router IP:</label>
Expand Down
2 changes: 1 addition & 1 deletion NetworkMapper/ip/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from . import views

urlpatterns = [
path("", views.index.index, name="index"),
path("", views.index.index, name="ip_index"),
path("vlan", views.vlan.index, name="vlan.index"),
path("vlan/create", views.vlan.create, name="vlan.create"),
path("vlan/<int:vlan_id>/edit", views.vlan.edit, name="vlan.edit"),
Expand Down
Loading

0 comments on commit 5b3d833

Please sign in to comment.