Skip to content

Commit

Permalink
Allow unclaimed devices to be removed
Browse files Browse the repository at this point in the history
Signed-off-by: rdotjain <[email protected]>
  • Loading branch information
rdotjain committed Jun 28, 2023
1 parent bf974bd commit a999294
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
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 %}
1 change: 1 addition & 0 deletions zezere/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# 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/",
Expand Down
12 changes: 12 additions & 0 deletions zezere/views_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ 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):
Expand Down

0 comments on commit a999294

Please sign in to comment.