put device into a lot

This commit is contained in:
Cayo Puigdefabregas 2024-07-09 17:31:24 +02:00
parent 160dea135b
commit 65ad4a04e4
7 changed files with 153 additions and 5 deletions

View File

@ -4,6 +4,7 @@ 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
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from device.models import Device
class Http403(PermissionDenied): class Http403(PermissionDenied):
@ -39,6 +40,12 @@ class DashboardView(LoginRequiredMixin):
}) })
return context return context
def get_session_devices(self):
# import pdb; pdb.set_trace()
dev_ids = self.request.session.pop("devices", [])
self._devices = Device.objects.filter(id__in=dev_ids).filter(owner=self.request.user)
return self._devices
class DetailsMixin(DashboardView, TemplateView): class DetailsMixin(DashboardView, TemplateView):
@ -58,13 +65,13 @@ class DetailsMixin(DashboardView, TemplateView):
class InventaryMixin(DashboardView, TemplateView): class InventaryMixin(DashboardView, TemplateView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
devices = [int(x) for x in dict(self.request.POST).get("devices", [])] dev_ids = dict(self.request.POST).get("devices", [])
self.request.session["devices"] = devices self.request.session["devices"] = dev_ids
url = self.request.POST.get("url") url = self.request.POST.get("url")
if url: if url:
try: try:
resource = resolve(url) resource = resolve(url)
if resource and devices: if resource and dev_ids:
return redirect(url) return redirect(url)
except Exception: except Exception:
pass pass

View File

@ -50,6 +50,7 @@
</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> <button type="submit" value="/lot/devices/remove" name="url">Remove</button> <button type="submit" name="url" value="{% url 'lot:add_devices' %}">add</button>
</form>
</div> </div>
{% endblock %} {% endblock %}

22
lot/forms.py Normal file
View File

@ -0,0 +1,22 @@
from django import forms
from lot.models import Lot
class LotsForm(forms.Form):
lots = forms.ModelMultipleChoiceField(
queryset=Lot.objects.all(),
widget=forms.CheckboxSelectMultiple,
)
def clean(self):
# import pdb; pdb.set_trace()
self._lots = self.cleaned_data.get("lots")
return self._lots
def save(self, commit=True):
if not commit:
return
# import pdb; pdb.set_trace()
for dev in self.devices:
for lot in self._lots:
lot.devices.add(dev.id)
return

View File

@ -21,3 +21,21 @@ class Lot(models.Model):
closed = models.BooleanField(default=True) closed = models.BooleanField(default=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE) owner = models.ForeignKey(User, on_delete=models.CASCADE)
devices = models.ManyToManyField(Device) devices = models.ManyToManyField(Device)
@property
def is_incoming(self):
if self.type == self.Types.INCOMING:
return True
return False
@property
def is_outgoing(self):
if self.type == self.Types.OUTGOING:
return True
return False
@property
def is_temporal(self):
if self.type == self.Types.TEMPORAL:
return True
return False

View File

@ -0,0 +1,61 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<div class="row">
<div class="col">
<h3>{{ subtitle }}</h3>
</div>
</div>
<form method="post">
{% csrf_token %}
{% if incoming %}
<div class="row">
<div class="col-lg-3 col-md-4 label ">Incoming Lots</div>
</div>
{% for lot in lots %}
{% if lot.is_incoming %}
<div class="row">
<div class="col-lg-3 col-md-4 label "><input type="checkbox" name="lots" value="{{ lot.id }}" /></div>
<div class="col-lg-3 col-md-4 label ">{{ lot.name }}</div>
</div>
{% endif %}
{% endfor %}
{% endif %}
{% if outgoing %}
<div class="row">
<div class="col-lg-3 col-md-4 label ">Outgoing Lots</div>
</div>
{% for lot in lots %}
{% if lot.is_outgoing %}
<div class="row">
<div class="col-lg-3 col-md-4 label "><input type="checkbox" name="lots" value="{{ lot.id }}" /></div>
<div class="col-lg-3 col-md-4 label ">{{ lot.name }}</div>
</div>
{% endif %}
{% endfor %}
{% endif %}
{% if temporal %}
<div class="row">
<div class="col-lg-3 col-md-4 label ">Temporary Lots</div>
</div>
{% for lot in lots %}
{% if lot.is_temporal %}
<div class="row">
<div class="col-lg-3 col-md-4 label "><input type="checkbox" name="lots" value="{{ lot.id }}" /></div>
<div class="col-lg-3 col-md-4 label ">{{ lot.name }}</div>
</div>
{% endif %}
{% endfor %}
{% endif %}
<button type="submit">Save</button>
</form>
{% endblock %}

View File

@ -6,4 +6,5 @@ app_name = 'lot'
urlpatterns = [ urlpatterns = [
path("add/", views.NewLotView.as_view(), name="add"), path("add/", views.NewLotView.as_view(), name="add"),
path("edit/<int:pk>/", views.EditLotView.as_view(), name="edit"), path("edit/<int:pk>/", views.EditLotView.as_view(), name="edit"),
path("add/devices/", views.AddToLotView.as_view(), name="add_devices"),
] ]

View File

@ -4,9 +4,11 @@ from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import ( from django.views.generic.edit import (
CreateView, CreateView,
UpdateView, UpdateView,
FormView
) )
from dashboard.mixins import DashboardView, DetailsMixin from dashboard.mixins import DashboardView
from lot.models import Lot from lot.models import Lot
from lot.forms import LotsForm
class NewLotView(DashboardView, CreateView): class NewLotView(DashboardView, CreateView):
@ -49,3 +51,39 @@ class EditLotView(DashboardView, UpdateView):
# self.success_url = reverse_lazy('dashbiard:lot', args=[pk]) # self.success_url = reverse_lazy('dashbiard:lot', args=[pk])
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
return kwargs return kwargs
class AddToLotView(DashboardView, FormView):
template_name = "list_lots.html"
title = _("Add to lots")
breadcrumb = "lot / add to lots"
success_url = reverse_lazy('dashboard:unassigned_devices')
form_class = LotsForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
lots = Lot.objects.filter(owner=self.request.user)
lots_incoming = lots.filter(type=Lot.Types.INCOMING).exists()
lots_outgoing = lots.filter(type=Lot.Types.OUTGOING).exists()
lots_temporal = lots.filter(type=Lot.Types.TEMPORAL).exists()
context.update({
'lots': lots,
'incoming': lots_incoming,
'outgoing': lots_outgoing,
'temporal': lots_temporal
})
return context
def get_form(self):
form = super().get_form()
# import pdb; pdb.set_trace()
form.fields["lots"].queryset = Lot.objects.filter(owner=self.request.user)
return form
def form_valid(self, form):
form.devices = self.get_session_devices()
form.save()
response = super().form_valid(form)
return response