add select devices in dashboard

This commit is contained in:
Cayo Puigdefabregas 2024-07-09 13:34:30 +02:00
parent 74ea02a6ff
commit f3ddbb0bbd
3 changed files with 27 additions and 3 deletions

View File

@ -1,5 +1,5 @@
from django.urls import resolve
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import PermissionDenied
from django.contrib.auth.mixins import LoginRequiredMixin
@ -53,3 +53,21 @@ class DetailsMixin(DashboardView, TemplateView):
'object': self.object,
})
return context
class InventaryMixin(DashboardView, TemplateView):
def post(self, request, *args, **kwargs):
devices = [int(x) for x in dict(self.request.POST).get("devices", [])]
self.request.session["devices"] = devices
url = self.request.POST.get("url")
if url:
try:
resource = resolve(url)
if resource and devices:
return redirect(url)
except Exception:
pass
return super().get(request, *args, **kwargs)

View File

@ -27,6 +27,8 @@
</div>
<div class="dataTable-container">
<form method="post">
{% csrf_token %}
<table class="table">
<thead>
<tr>
@ -38,6 +40,9 @@
{% for dev in devices %}
<tbody>
<tr>
<td>
<input type="checkbox" name="devices" value="{{ dev.id }}" />
</td>
<td>
<a href="{% url 'device:details' dev.pk %}">{{ dev.type }} {{ dev.manufacturer }} {{ dev.model }}</a>
</td>
@ -45,5 +50,6 @@
</tbody>
{% endfor %}
</table>
<button type="submit" value="/lot/devices/remove" name="url">Remove</button> <button type="submit" name="url" value="{% url 'lot:add' %}">add</button>
</div>
{% endblock %}

View File

@ -1,10 +1,10 @@
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
from dashboard.mixins import DashboardView
from dashboard.mixins import InventaryMixin
from device.models import Device
class UnassignedDevicesView(DashboardView, TemplateView):
class UnassignedDevicesView(InventaryMixin):
template_name = "unassigned_devices.html"
section = "Unassigned"
title = _("Unassigned Devices")