Skip to content

Commit

Permalink
Rack Management (#10)
Browse files Browse the repository at this point in the history
* Add Rack and RackItem Models and Forms

* Add Views and URLs

* Add Template Pages

* MakeMigrations for Rack and RackItem

* Migrate to Abstract Device Model for Client/Network Devices

* Link RackItem and Network Device

* Bugfixes

* More Bugfixes

* Generalise Device Create and Index Pages

for Client and Network Devices

* Delete Admin/Test Files

* Rack Measure Conversions and Index

* Add Networked vs Infrastructure Devices

* Forms Folder for Network App

* Make Migrations for Network Devices
  • Loading branch information
ben-burwood authored Jul 8, 2024
1 parent e7a8ba8 commit 60bcf5c
Show file tree
Hide file tree
Showing 44 changed files with 1,019 additions and 159 deletions.
4 changes: 4 additions & 0 deletions common/templates/base/crud.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ <h1>{% block header_content %}{% endblock %}</h1>
<a href="{% block back_link %}{% endblock %}" class="btn btn-danger">Back</a>
</div>

{% if show_form|default:"True" %}
<form method="post" action="{% block form_action %}{% endblock %}">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-success">Submit</button>
</form>
{% endif %}

{% block additional_content %}{% endblock %}
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions common/templates/base/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<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 'rack.index' %}">Racks</a></li>
</ul>
</li>
<li><a href="{% url 'mqtt_index' %}">MQTT</a></li>
Expand Down
8 changes: 8 additions & 0 deletions common/utils/units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def mm_to_inch(mm: float) -> float:
"""Convert mm to Inches"""
return mm / 25.4


def inch_to_mm(inch: float) -> float:
"""Convert Inches to mm"""
return inch * 25.4
3 changes: 0 additions & 3 deletions electric/admin.py

This file was deleted.

27 changes: 27 additions & 0 deletions electric/migrations/0002_alter_circuit_breaker_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.2 on 2024-06-06 18:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("electric", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="circuit",
name="breaker_type",
field=models.CharField(
choices=[
("fuse", "Fuse"),
("mcb", "MCB"),
("rcd", "RCD"),
("RCBO", "rcbo"),
("gfci", "GFCI"),
],
max_length=10,
),
),
]
27 changes: 27 additions & 0 deletions electric/migrations/0003_alter_circuit_breaker_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.2 on 2024-07-08 18:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("electric", "0002_alter_circuit_breaker_type"),
]

operations = [
migrations.AlterField(
model_name="circuit",
name="breaker_type",
field=models.CharField(
choices=[
("fuse", "Fuse"),
("mcb", "MCB"),
("rcd", "RCD"),
("rcbo", "RCBO"),
("gfci", "GFCI"),
],
max_length=10,
),
),
]
4 changes: 4 additions & 0 deletions electric/templates/electric/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ <h2>Voltage</h2>
<button type="submit" name="voltage-submit" class="btn btn-primary">Save</button>
</div>
</form>

<br> <br>

<a href="{% url 'circuit.index' %}"><h2>Circuits</h2></a>
</div>
</div>
</div>
Expand Down
3 changes: 0 additions & 3 deletions electric/tests.py

This file was deleted.

3 changes: 0 additions & 3 deletions home/admin.py

This file was deleted.

3 changes: 0 additions & 3 deletions home/tests.py

This file was deleted.

3 changes: 0 additions & 3 deletions mqtt/admin.py

This file was deleted.

3 changes: 0 additions & 3 deletions mqtt/tests.py

This file was deleted.

3 changes: 0 additions & 3 deletions network/admin.py

This file was deleted.

58 changes: 0 additions & 58 deletions network/forms.py

This file was deleted.

Empty file added network/forms/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions network/forms/devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from django import forms

from network.models import ClientDevice, NetworkDevice
from network.models.device import InfrastructureDevice, NetworkedDevice
from network.objects import ConnectionType

class NetworkedDeviceForm(forms.ModelForm):

connection_type = forms.ChoiceField(
choices=ConnectionType.choices(),
widget=forms.Select(attrs={"class": "form-control"}),
)

class Meta:
model = NetworkedDevice
abstract = True
fields = [
"name",
"mac_address",
"vlan",
"ip_address",
"connection_type",
"wifi",
]

def __init__(self, *args, vlan_options=None, wifi_options=None, **kwargs):
super(NetworkedDeviceForm, self).__init__(*args, **kwargs)

self.fields["vlan"].widget = forms.Select(choices=vlan_options, attrs={"class": "form-control"})

empty_choice = [("", "None")]

self.fields["wifi"].widget = forms.Select(choices=empty_choice + wifi_options, attrs={"class": "form-control"})
self.fields["wifi"].required = False

def clean(self):
cleaned_data = super().clean()

connection_type = cleaned_data.get("connection_type")
wifi = cleaned_data.get("wifi")
if connection_type == "ethernet" and wifi:
raise forms.ValidationError("WiFi must be null for Ethernet Connections", code="invalid")
if connection_type == "wifi" and not wifi:
raise forms.ValidationError("WiFi must be set for WiFi Connections", code="invalid")

return cleaned_data


class ClientDeviceForm(NetworkedDeviceForm):
class Meta(NetworkedDeviceForm.Meta):
model = ClientDevice


class NetworkDeviceForm(NetworkedDeviceForm):
class Meta(NetworkedDeviceForm.Meta):
model = NetworkDevice
fields = NetworkedDeviceForm.Meta.fields + ["device_type"]


class InfrastructureDeviceForm(forms.ModelForm):
class Meta:
model = InfrastructureDevice
fields = ["name", "device_type"]
27 changes: 27 additions & 0 deletions network/forms/networking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django import forms

from network.models import VLAN, WifiNetwork
from network.models.ip import IpRange

class IpRangeForm(forms.Form):

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

class Meta:
model = IpRange
fields = ["start_address", "end_address", "num_addresses", "description"]


class VLANForm(forms.ModelForm):

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

class Meta:
model = VLAN
fields = ["vlan_id", "name", "description"]


class WifiForm(forms.ModelForm):
class Meta:
model = WifiNetwork
fields = ["ssid", "password"]
23 changes: 23 additions & 0 deletions network/forms/rack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django import forms

from network.models import Rack, RackItem

class RackForm(forms.ModelForm):

class Meta:
model = Rack
fields = ["name", "width", "rack_units"]


class RackItemForm(forms.ModelForm):

class Meta:
model = RackItem
fields = ["name", "rack_units", "rack", "device"]

def __init__(self, *args, rack_options=None, device_options=None, **kwargs):
super(RackItemForm, self).__init__(*args, **kwargs)

self.fields["rack"].widget = forms.Select(choices=rack_options, attrs={"class": "form-control"})

self.fields["device"].widget = forms.Select(choices=device_options, attrs={"class": "form-control"})
60 changes: 60 additions & 0 deletions network/migrations/0002_rack_rackitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 5.0.2 on 2024-06-06 18:05

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("network", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="Rack",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=50)),
(
"width",
models.PositiveIntegerField(
choices=[(10, "WIDTH_10"), (19, "WIDTH_19"), (23, "WIDTH_23")]
),
),
("rack_units", models.FloatField()),
],
),
migrations.CreateModel(
name="RackItem",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=50)),
("rack_units", models.FloatField()),
(
"rack",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="rack_items",
to="network.rack",
),
),
],
),
]
Loading

0 comments on commit 60bcf5c

Please sign in to comment.