Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #121

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Fixes #121

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,6 @@ venv.bak/

# mypy
.mypy_cache/

# conf file
zezere.conf
1 change: 1 addition & 0 deletions zezere/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class Meta:
"delete": rules.owns_device,
"provision": rules.owns_device,
"claim": rules.can_claim,
"remove_unclaimed": rules.can_claim,
}

def __str__(self):
Expand Down
50 changes: 36 additions & 14 deletions zezere/templates/portal/claim.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,42 @@

{% block content %}
{% if super %}
Unowned devices:
<h1>Unowned devices:</h1>
{% else %}
Unowned devices from this IP address:
<h1>Unowned devices from this IP address:</h1>
{% endif %}
{% for device in unclaimed_devices %}
{% has_perm 'zezere.claim_device' user device as can_claim_device %}
{% if can_claim_device %}
<form method="POST">
MAC address: {{ device.mac_address }}
{% csrf_token %}
<input type="hidden" name="mac_address" value="{{ device.mac_address }}">
<input type="submit" value="Claim">
</form>
<br />
{% endif %}
{% endfor %}
<div class="container-fluid">
<table border="1" class="table">
<thead>
<tr>
<th>Mac Address</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
{% for device in unclaimed_devices %}
{% has_perm 'zezere.claim_device' user device as can_claim_device %}
{% if can_claim_device %}
<tr>
<td>{{ device.mac_address }}</td>
<td>
<form method="POST">
{% csrf_token %}
<input type="hidden" name="mac_address" value="{{ device.mac_address }}">
<input class="btn btn-primary btn-sm" type="submit" value="Claim">
</form>
</td>
<td>
<form method="POST" action="/portal/claim/remove/">
{% csrf_token %}
<input type="hidden" name="device_id" value="{{ device.id }}">
<input class ="btn btn-danger btn-sm" type="submit" value="Remove">
</form>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
81 changes: 50 additions & 31 deletions zezere/templates/portal/devices.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,56 @@
{% block title %}Device list{% endblock %}

{% block content %}
Your devices:
<table border="1">
<tr>
<th>Mac Address</th>
<th>Hostname</th>
<th>Run Request</th>
<th>Actions</th>
</tr>
{% for device in devices %}
{% has_perm 'zezere.view_device' user device as can_view_device %}
{% if can_view_device %}
<h1>Your devices:</h1>
<hr/>
{% if not devices %}
<h3> No claimed devices</h3>
{% else %}
<div class="container-fluid">
<table border="1" class="table">
<thead>
<tr>
<td>{{ device.mac_address }}</td>
<td>{{ device.hostname }}</td>
<td>
{% if device.run_request %}
{{ device.run_request }}
{% endif %}
</td>
<td>
{% if device.run_request %}
<form method="POST" action="/portal/devices/runreq/{{ device.mac_address }}/clean/">
{% csrf_token %}
<input type="submit" value="Cancel runrequest">
</form>
{% else %}
<a href="/portal/devices/runreq/{{ device.mac_address }}">Submit provision request</a>
{% endif %}
</td>
<th>Mac Address</th>
<th>Hostname</th>
<th>Run Request</th>
<th>Actions</th>
<th></th>
</tr>
{% endif %}
{% endfor %}
</table>
</thead>
<tbody>
{% for device in devices %}
{% has_perm 'zezere.view_device' user device as can_view_device %}
{% if can_view_device %}
<tr>
<td class="align-items-center">{{ device.mac_address }}</td>
<td>{{ device.hostname }}</td>
<td>
{% if device.run_request %}
{{ device.run_request }}
{% endif %}
</td>
<td>
{% if device.run_request %}
<form method="POST" action="/portal/devices/runreq/{{ device.mac_address }}/clean/">
{% csrf_token %}
<input class="btn btn-warning" type="submit" value="Cancel runrequest">
</form>
{% else %}
<a class="btn btn-primary" href="/portal/devices/runreq/{{ device.mac_address }}">Submit provision request</a>
{% endif %}
</td>
<td>
<form method="POST" action="/portal/devices/delete/">
{% csrf_token %}
<input type="hidden" name="device_id" value="{{ device.id }}">
<input class ="btn btn-danger" type="submit" value="Remove Device">
</form>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions zezere/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
# Portal
path("portal/", views_portal.index, name="portal_index"),
path("portal/claim/", views_portal.claim, name="portal_claim"),
path("portal/claim/remove/", views_portal.remove_unclaimed, name="portal_remove_unclamed"),
path("portal/devices/", views_portal.devices, name="portal_devices"),
path(
"portal/devices/delete/",
views_portal.unclaim,
name="portal_unclaim_device",
),
path(
"portal/devices/runreq/<str:mac_addr>/",
views_portal.new_runreq,
Expand Down
25 changes: 25 additions & 0 deletions zezere/views_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ def claim(request):
return render(request, "portal/claim.html", context)


@login_required
@require_POST
def remove_unclaimed(request):
device_id = request.POST.get("device_id")
device = get_object_or_404(Device, id=device_id)
if not request.user.has_perm(Device.get_perm("remove_unclaimed"), device):
raise PermissionDenied()

device.delete()
return redirect("/portal/claim/")


@login_required
@require_POST
def unclaim(request):
device_id = request.POST.get("device_id")
device = get_object_or_404(Device, id=device_id)
if not request.user.has_perm(Device.get_perm("delete"), device):
raise PermissionDenied()

device.owner = None
device.save()
return redirect("/portal/devices/")


@login_required
def devices(request):
devices = Device.objects.filter(owner=request.user)
Expand Down