From cd4156aac33d6dfdc0ba611b71388f5cffffcace Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 8 Jul 2024 12:59:32 +0200 Subject: [PATCH] edit device and physicalproperties --- device/forms.py | 39 ---------- device/models.py | 9 +++ device/templates/details.html | 35 ++++++--- device/templates/physical_properties.html | 41 +++++----- device/urls.py | 1 + device/views.py | 94 +++++++++++++++-------- 6 files changed, 118 insertions(+), 101 deletions(-) diff --git a/device/forms.py b/device/forms.py index a364709..32d64dc 100644 --- a/device/forms.py +++ b/device/forms.py @@ -1,42 +1,3 @@ from django import forms -# from django.utils.translation import gettext_lazy as _ -# from django.core.exceptions import ValidationError -# from user.models import User -from device.models import ( - Device, - PhysicalProperties -) -class DeviceForm(forms.ModelForm): - - class Meta: - model = Device - fields = [ - 'type', - "model", - "manufacturer", - "serial_number", - "part_number", - "brand", - "generation", - "version", - "production_date", - "variant", - "family", - ] - - -class PhysicalPropsForm(forms.Form): - - class Meta: - model = PhysicalProperties - fields = [ - "device", - "weight", - "width", - "height", - "depth", - "color", - "image", - ] diff --git a/device/models.py b/device/models.py index 2215a3c..50d5f8c 100644 --- a/device/models.py +++ b/device/models.py @@ -41,6 +41,15 @@ class Device(models.Model): active = models.BooleanField(default=True) owner = models.ForeignKey(User, on_delete=models.CASCADE) + def has_physical_properties(self): + try: + if self.physicalproperties: + return True + else: + return False + except Exception: + return False + class PhysicalProperties(models.Model): device = models.OneToOneField(Device, models.CASCADE, primary_key=True) diff --git a/device/templates/details.html b/device/templates/details.html index 65fffbf..f5d484d 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -45,7 +45,7 @@
Details
{%if object.hid %}Snapshot{% else %}Placeholder{% endif %} @@ -92,46 +92,57 @@
Physical Properties
-
+ {% if object.has_physical_properties %} +
Weight:
+ {{ object.physicalproperties.weight }}
width:
-
+
+ {{ object.physicalproperties.width }} +
height:
-
+
+ {{ object.physicalproperties.height }} +
depth:
-
+
+ {{ object.physicalproperties.depth }} +
color:
-
+
+ {{ object.physicalproperties.color }} +
image:
-
+
+ {% if object.physicalproperties.image %} + + {% endif %} +
+ {% endif %}
diff --git a/device/templates/physical_properties.html b/device/templates/physical_properties.html index 63f11c0..cd4aedd 100644 --- a/device/templates/physical_properties.html +++ b/device/templates/physical_properties.html @@ -12,7 +12,7 @@
diff --git a/device/urls.py b/device/urls.py index 5e03af5..8445449 100644 --- a/device/urls.py +++ b/device/urls.py @@ -5,6 +5,7 @@ app_name = 'device' urlpatterns = [ path("add/", views.NewDeviceView.as_view(), name="add"), + path("edit//", views.EditDeviceView.as_view(), name="edit"), path("/", views.DetailsView.as_view(), name="details"), path("physical//", views.PhysicalView.as_view(), name="physical_edit"), ] diff --git a/device/views.py b/device/views.py index 1585e40..249ee1e 100644 --- a/device/views.py +++ b/device/views.py @@ -6,7 +6,6 @@ from django.views.generic.edit import ( UpdateView, ) from dashboard.mixins import DashboardView, DetailsMixin -from device.forms import DeviceForm, PhysicalPropsForm from device.models import Device, PhysicalProperties @@ -14,8 +13,21 @@ class NewDeviceView(DashboardView, CreateView): template_name = "new_device.html" title = _("New Device") breadcrumb = "Device / New Device" - form_class = DeviceForm success_url = reverse_lazy('dashboard:unassigned_devices') + model = Device + fields = ( + 'type', + "model", + "manufacturer", + "serial_number", + "part_number", + "brand", + "generation", + "version", + "production_date", + "variant", + "family", + ) def form_valid(self, form): form.instance.owner = self.request.user @@ -24,6 +36,34 @@ class NewDeviceView(DashboardView, CreateView): return response +class EditDeviceView(DashboardView, UpdateView): + template_name = "new_device.html" + title = _("Update Device") + breadcrumb = "Device / Update Device" + success_url = reverse_lazy('dashboard:unassigned_devices') + model = Device + fields = ( + 'type', + "model", + "manufacturer", + "serial_number", + "part_number", + "brand", + "generation", + "version", + "production_date", + "variant", + "family", + ) + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') + self.object = get_object_or_404(self.model, pk=pk) + self.success_url = reverse_lazy('device:details', args=[pk]) + kwargs = super().get_form_kwargs() + return kwargs + + class DetailsView(DetailsMixin): template_name = "details.html" title = _("Device") @@ -35,46 +75,36 @@ class PhysicalView(DashboardView, UpdateView): template_name = "physical_properties.html" title = _("Physical Properties") breadcrumb = "Device / Physical properties" - form_class = PhysicalPropsForm success_url = reverse_lazy('dashboard:unassigned_devices') model = PhysicalProperties + fields = ( + "weight", + "width", + "height", + "depth", + "color", + "image", + ) - def get(self, request, *args, **kwargs): - pk = kwargs['pk'] + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'device': self.device, + }) + return context + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') self.device = get_object_or_404(Device, pk=pk) try: self.object = self.device.physicalproperties except Exception: self.object = PhysicalProperties.objects.create(device=self.device) - self.initial.update({'instance': self.object}) - return super().get(request, *args, **kwargs) - - def get_form(self, form_class=None): - """Return an instance of the form to be used in this view.""" - if form_class is None: - form_class = self.get_form_class() - # import pdb; pdb.set_trace() - return form_class(**self.get_form_kwargs()) - - def get_form_kwargs(self): - """Return the keyword arguments for instantiating the form.""" - kwargs = { - "initial": self.get_initial(), - "prefix": self.get_prefix(), - } - - if self.request.method in ("POST", "PUT"): - kwargs.update( - { - "data": self.request.POST, - "files": self.request.FILES, - } - ) + kwargs = super().get_form_kwargs() return kwargs - + def form_valid(self, form): - self.success_url = reverse_lazy('device:details', self.device.id) - form.instance.owner = self.request.user + self.success_url = reverse_lazy('device:details', args=[self.device.id]) response = super().form_valid(form) return response