Skip to content

Commit

Permalink
Merge pull request #93 from juliushaag/90-hinzufügen-und-editieren-vo…
Browse files Browse the repository at this point in the history
…n-hardwareprofiles-geht-nicht-über-die-website

Fixed post naming schema
  • Loading branch information
juliushaag authored Aug 9, 2024
2 parents 23006af + 77603bc commit d24b1f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
14 changes: 7 additions & 7 deletions quafelweb/hardware_controller/templates/configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@
{% block content %}
<form method="post" class="page_panel" action="submit/">
{% csrf_token %}
<input name="hardware_id" type="hidden" value="{{ hardware.uuid }}"></input>
<input name="id" type="hidden" value="{{ hardware.uuid }}"></input>
<div id="hardware_name">
<input required name="hardware_name" value="{{ hardware.name }}">
<input required name="name" value="{{ hardware.name }}">
<span>Name</span>
</div>
<div id="hardware_description">
<textarea required name="hardware_description">{{ hardware.description }}</textarea>
<textarea required name="description">{{ hardware.description }}</textarea>
<span>Description</span>
</div>
<div id="hardware_connection">
<input required name="hardware_connection" value="{{ hardware.protocol }}://{{ hardware.ip_addr }}:{{hardware.port_addr}}">
<input required name="connection" value="{{ hardware.protocol }}://{{ hardware.ip_addr }}:{{hardware.port_addr}}">
<span>Connection</span>
</div>
<div id="hardware_totp">
<label for="hardware_totp"><b>Requires TOTP</b></label>
<input type="checkbox" name="hardware_totp" {% if hardware.needs_totp %}checked{%endif%}>
<input type="checkbox" name="totp" {% if hardware.needs_totp %}checked{%endif%}>
</div>

<div id="button_array">
<a href="{% url "hardware" %}" class="link_button">Back</a>
<button type="submit" name="hardware_id" value={{hardware.uuid}}>Save</button>
<button type="submit" name="hardware_id" id="delete_button" value={{hardware.uuid}} formaction="{% url "delete_hardware" %}" formmethod="post">Delete</button>
<button type="submit">Save</button>
<button type="submit" id="delete_button"formaction="{% url "delete_hardware" %}" formmethod="post">Delete</button>
</div>

</form>
Expand Down
4 changes: 2 additions & 2 deletions quafelweb/hardware_controller/templates/hardware.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<button type="submit" action="/hardware"><span class="material-symbols-outlined">search</span></button>
</div>
<div id='hardware_container'>
{%for hardware in hardware_profiles %}
{%for hardware in profiles %}
<form class='hardware_entry' action="configure/" method="get">
<p>{{ hardware.name }}</p>
<button class="icon_button" name="hardware_id" value={{hardware.uuid}}><span class="material-symbols-outlined">edit</span></button>
<button class="icon_button" name="id" value={{hardware.uuid}}><span class="material-symbols-outlined">edit</span></button>
</form>
{% empty %}
<p id="no_profiles">No hardware profiles found</p>
Expand Down
52 changes: 29 additions & 23 deletions quafelweb/hardware_controller/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class HardwareView:


HARDWARE_ADDR_REGEX = r"^(.*?)://(.*?)(?::(\d+))?$"

# /hardware

@AccountView.require_login
Expand All @@ -21,30 +24,33 @@ def manage_profiles(request):
if search := request.GET.get("search"):
profiles = [profile for profile in profiles if search in profile.name]

context = {"hardware_profiles": profiles}
context = {"profiles": profiles}
return render(request, "hardware.html", context=context)

# /hardware/add with post

@AccountView.require_login
def add_profile(request):
id = request.POST.get("hardware_id")
name = request.POST.get("hardware_name")
description = request.POST.get("hardware_description")
connection = request.POST.get("hardware_connection")
name = request.POST.get("name")
description = request.POST.get("description")
connection = request.POST.get("connection") or ""


addr_match = r"^([a-zA-Z]+)://(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})$"
conn_match = regex.match(HardwareView.HARDWARE_ADDR_REGEX, connection)

if id and name and description and regex.match(addr_match, connection):
protocol, ip_addr, port_addr = conn_match.groups()

if name and description and protocol and ip_addr:

protocol, ip_addr, port_addr = connection.replace("//", "").split(":", 2)
HardwareProfile(
hp = HardwareProfile(
name=name,
description=description,
protocol=protocol,
ip_addr=ip_addr,
port_addr=port_addr,
).save()
port_addr = port_addr or 22
)

hp.save()

return redirect("hardware")

Expand All @@ -53,7 +59,7 @@ def add_profile(request):
@AccountView.require_login
def delete_profile(request):

if id := request.POST.get("hardware_id"):
if id := request.POST.get("id"):

SimulationRun.objects.filter(hardware=id).delete()
HardwareProfile.objects.filter(uuid=id).delete()
Expand All @@ -63,7 +69,7 @@ def delete_profile(request):
@AccountView.require_login
def configure_profile(request):

if id := request.GET.get("hardware_id"):
if id := request.GET.get("id"):

profile = HardwareProfile.objects.filter(uuid=id)

Expand All @@ -78,20 +84,20 @@ def configure_profile(request):

@AccountView.require_login
def submit_change(request):
id = request.POST.get("hardware_id")
name = request.POST.get("hardware_name")
description = request.POST.get("hardware_description")
connection = request.POST.get("hardware_connection")

addr_match = r"^([a-zA-Z]+)://(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})$"
id = request.POST.get("id")
name = request.POST.get("name")
description = request.POST.get("description")
connection = request.POST.get("connection") or ""
needs_totp = request.POST.get("totp") == "on"

if id and name and description and regex.match(addr_match, connection):
conn_match = regex.match(HardwareView.HARDWARE_ADDR_REGEX, connection)

protocol, ip_addr, port_addr = connection.replace("//", "").split(":", 2)
protocol, ip_addr, port_addr = conn_match.groups()

if id and name and description and protocol and ip_addr:

profile = HardwareProfile.objects.filter(uuid=id)

needs_totp = request.POST.get("hardware_totp") == "on"

if not profile.exists():
raise RuntimeError("Invalid hardware profile requested") # TODO
Expand All @@ -101,7 +107,7 @@ def submit_change(request):
description=description,
protocol=protocol,
ip_addr=ip_addr,
port_addr=port_addr,
port_addr=port_addr or 22,
needs_totp=needs_totp,
)

Expand Down

0 comments on commit d24b1f5

Please sign in to comment.