diff --git a/dashboard/mixins.py b/dashboard/mixins.py index 8a9cb8c..a6c372c 100644 --- a/dashboard/mixins.py +++ b/dashboard/mixins.py @@ -4,6 +4,7 @@ from django.utils.translation import gettext_lazy as _ from django.core.exceptions import PermissionDenied from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic.base import TemplateView +from device.models import Device class Http403(PermissionDenied): @@ -39,6 +40,12 @@ class DashboardView(LoginRequiredMixin): }) 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): @@ -58,13 +65,13 @@ class DetailsMixin(DashboardView, TemplateView): 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 + dev_ids = dict(self.request.POST).get("devices", []) + self.request.session["devices"] = dev_ids url = self.request.POST.get("url") if url: try: resource = resolve(url) - if resource and devices: + if resource and dev_ids: return redirect(url) except Exception: pass diff --git a/dashboard/templates/unassigned_devices.html b/dashboard/templates/unassigned_devices.html index 9ad7505..d697e78 100644 --- a/dashboard/templates/unassigned_devices.html +++ b/dashboard/templates/unassigned_devices.html @@ -50,6 +50,7 @@ {% endfor %} - + + {% endblock %} diff --git a/lot/forms.py b/lot/forms.py new file mode 100644 index 0000000..119192c --- /dev/null +++ b/lot/forms.py @@ -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 diff --git a/lot/models.py b/lot/models.py index 9f33e57..860bdbf 100644 --- a/lot/models.py +++ b/lot/models.py @@ -21,3 +21,21 @@ class Lot(models.Model): closed = models.BooleanField(default=True) owner = models.ForeignKey(User, on_delete=models.CASCADE) 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 diff --git a/lot/templates/list_lots.html b/lot/templates/list_lots.html new file mode 100644 index 0000000..126e7bb --- /dev/null +++ b/lot/templates/list_lots.html @@ -0,0 +1,61 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} +
+
+

{{ subtitle }}

+
+
+ +
+ {% csrf_token %} + +{% if incoming %} +
+
Incoming Lots
+
+ +{% for lot in lots %} +{% if lot.is_incoming %} +
+
+
{{ lot.name }}
+
+{% endif %} +{% endfor %} +{% endif %} + +{% if outgoing %} +
+
Outgoing Lots
+
+ +{% for lot in lots %} +{% if lot.is_outgoing %} +
+
+
{{ lot.name }}
+
+{% endif %} +{% endfor %} +{% endif %} + +{% if temporal %} +
+
Temporary Lots
+
+ +{% for lot in lots %} +{% if lot.is_temporal %} +
+
+
{{ lot.name }}
+
+{% endif %} +{% endfor %} +{% endif %} + +
+ +{% endblock %} diff --git a/lot/urls.py b/lot/urls.py index 313a23d..0a641d4 100644 --- a/lot/urls.py +++ b/lot/urls.py @@ -6,4 +6,5 @@ app_name = 'lot' urlpatterns = [ path("add/", views.NewLotView.as_view(), name="add"), path("edit//", views.EditLotView.as_view(), name="edit"), + path("add/devices/", views.AddToLotView.as_view(), name="add_devices"), ] diff --git a/lot/views.py b/lot/views.py index 12e7872..d570883 100644 --- a/lot/views.py +++ b/lot/views.py @@ -4,9 +4,11 @@ from django.utils.translation import gettext_lazy as _ from django.views.generic.edit import ( CreateView, UpdateView, + FormView ) -from dashboard.mixins import DashboardView, DetailsMixin +from dashboard.mixins import DashboardView from lot.models import Lot +from lot.forms import LotsForm class NewLotView(DashboardView, CreateView): @@ -49,3 +51,39 @@ class EditLotView(DashboardView, UpdateView): # self.success_url = reverse_lazy('dashbiard:lot', args=[pk]) kwargs = super().get_form_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 + +