add select devices in dashboard

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

View File

@ -1,5 +1,5 @@
from django.urls import resolve 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.utils.translation import gettext_lazy as _
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
@ -53,3 +53,21 @@ class DetailsMixin(DashboardView, TemplateView):
'object': self.object, 'object': self.object,
}) })
return context 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>
<div class="dataTable-container"> <div class="dataTable-container">
<form method="post">
{% csrf_token %}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -38,6 +40,9 @@
{% for dev in devices %} {% for dev in devices %}
<tbody> <tbody>
<tr> <tr>
<td>
<input type="checkbox" name="devices" value="{{ dev.id }}" />
</td>
<td> <td>
<a href="{% url 'device:details' dev.pk %}">{{ dev.type }} {{ dev.manufacturer }} {{ dev.model }}</a> <a href="{% url 'device:details' dev.pk %}">{{ dev.type }} {{ dev.manufacturer }} {{ dev.model }}</a>
</td> </td>
@ -45,5 +50,6 @@
</tbody> </tbody>
{% endfor %} {% endfor %}
</table> </table>
<button type="submit" value="/lot/devices/remove" name="url">Remove</button> <button type="submit" name="url" value="{% url 'lot:add' %}">add</button>
</div> </div>
{% endblock %} {% endblock %}

View File

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