edit device and physicalproperties
This commit is contained in:
parent
df63af3ee0
commit
cd4156aac3
|
@ -1,42 +1,3 @@
|
||||||
from django import forms
|
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",
|
|
||||||
]
|
|
||||||
|
|
|
@ -41,6 +41,15 @@ class Device(models.Model):
|
||||||
active = models.BooleanField(default=True)
|
active = models.BooleanField(default=True)
|
||||||
owner = models.ForeignKey(User, on_delete=models.CASCADE)
|
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):
|
class PhysicalProperties(models.Model):
|
||||||
device = models.OneToOneField(Device, models.CASCADE, primary_key=True)
|
device = models.OneToOneField(Device, models.CASCADE, primary_key=True)
|
||||||
|
|
|
@ -45,7 +45,7 @@
|
||||||
<h5 class="card-title">Details</h5>
|
<h5 class="card-title">Details</h5>
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
<div class="col-lg-3 col-md-4 label ">
|
<div class="col-lg-3 col-md-4 label ">
|
||||||
(<a href="/inventory/device/edit/4W8D3/">Edit Device</a>)
|
(<a href="{% url 'device:edit' object.id %}">Edit Device</a>)
|
||||||
</div>
|
</div>
|
||||||
<div class="col-lg-9 col-md-8">
|
<div class="col-lg-9 col-md-8">
|
||||||
{%if object.hid %}Snapshot{% else %}Placeholder{% endif %}
|
{%if object.hid %}Snapshot{% else %}Placeholder{% endif %}
|
||||||
|
@ -92,46 +92,57 @@
|
||||||
<h5 class="card-title">Physical Properties</h5>
|
<h5 class="card-title">Physical Properties</h5>
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
<div class="col-lg-3 col-md-4 label ">
|
<div class="col-lg-3 col-md-4 label ">
|
||||||
<a href="{% url 'device:details' object.pk %}">{{ object.type }} {{ object.manufacturer }} {{ object.model }}</a>
|
(<a href="{% url 'device:physical_edit' object.pk %}">Edit Physical Properties</a>)
|
||||||
<br>
|
|
||||||
<a href="{% url 'device:physical_edit' object.pk %}">{{ object.type }} {{ object.manufacturer }} {{ object.model }}</a>
|
|
||||||
<br>
|
|
||||||
(<a href="{# url('device:physical_edit', object.id) #}">Edit Physical Properties</a>)
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row mb-3">
|
{% if object.has_physical_properties %}
|
||||||
|
<div class="row">
|
||||||
<div class="col-lg-3 col-md-4 label ">
|
<div class="col-lg-3 col-md-4 label ">
|
||||||
Weight:
|
Weight:
|
||||||
</div>
|
</div>
|
||||||
<div class="col-lg-9 col-md-8">
|
<div class="col-lg-9 col-md-8">
|
||||||
|
{{ object.physicalproperties.weight }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-3 col-md-4 label ">width:</div>
|
<div class="col-lg-3 col-md-4 label ">width:</div>
|
||||||
<div class="col-lg-9 col-md-8"></div>
|
<div class="col-lg-9 col-md-8">
|
||||||
|
{{ object.physicalproperties.width }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-3 col-md-4 label ">height:</div>
|
<div class="col-lg-3 col-md-4 label ">height:</div>
|
||||||
<div class="col-lg-9 col-md-8"></div>
|
<div class="col-lg-9 col-md-8">
|
||||||
|
{{ object.physicalproperties.height }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-3 col-md-4 label ">depth:</div>
|
<div class="col-lg-3 col-md-4 label ">depth:</div>
|
||||||
<div class="col-lg-9 col-md-8"></div>
|
<div class="col-lg-9 col-md-8">
|
||||||
|
{{ object.physicalproperties.depth }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-3 col-md-4 label ">color:</div>
|
<div class="col-lg-3 col-md-4 label ">color:</div>
|
||||||
<div class="col-lg-9 col-md-8"></div>
|
<div class="col-lg-9 col-md-8">
|
||||||
|
{{ object.physicalproperties.color }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-3 col-md-4 label ">image:</div>
|
<div class="col-lg-3 col-md-4 label ">image:</div>
|
||||||
<div class="col-lg-9 col-md-8"></div>
|
<div class="col-lg-9 col-md-8">
|
||||||
|
{% if object.physicalproperties.image %}
|
||||||
|
<img width="200px" src="{{ object.physicalproperties.image }}" />
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="nav nav-tabs nav-tabs-bordered">
|
<div class="nav nav-tabs nav-tabs-bordered">
|
||||||
<li class="nav-items">
|
<li class="nav-items">
|
||||||
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#details">General details</button>
|
<a class="nav-link" href="{% url 'device:details' device.pk %}">General details</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-items">
|
<li class="nav-items">
|
||||||
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#physicalproperties">Physical properties</button>
|
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#physicalproperties">Physical properties</button>
|
||||||
|
@ -92,30 +92,35 @@
|
||||||
<h5 class="card-title">Physical Properties</h5>
|
<h5 class="card-title">Physical Properties</h5>
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
<div class="col-lg-3 col-md-4 label ">
|
<div class="col-lg-3 col-md-4 label ">
|
||||||
<a href="{% url 'device:details' object.pk %}">{{ object.type }} {{ object.manufacturer }} {{ object.model }}</a>
|
(<a href="{% url 'device:physical_edit' object.pk %}">Edit Physical Properties</a>)
|
||||||
<br>
|
|
||||||
<a href="{% url 'device:physical_edit' object.pk %}">{{ object.type }} {{ object.manufacturer }} {{ object.model }}</a>
|
|
||||||
<br>
|
|
||||||
(<a href="{# url('device:physical_edit', object.id) #}">Edit Physical Properties</a>)
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
<div class="col-lg-3 col-md-4 label ">
|
<div class="col-lg-3 col-md-4 label ">
|
||||||
{{ form }}
|
{% load django_bootstrap5 %}
|
||||||
|
<form role="form" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{% if form.errors %}
|
||||||
|
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
|
||||||
|
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
|
||||||
|
<div class="message">
|
||||||
|
{% for field, error in form.errors.items %}
|
||||||
|
{{ error }}<br />
|
||||||
|
{% endfor %}
|
||||||
|
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% bootstrap_form form %}
|
||||||
|
<div class="form-actions-no-box">
|
||||||
|
<a class="btn btn-grey" href="{% url 'device:details' device.pk %}">{% translate "Cancel" %}</a>
|
||||||
|
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-lg-3 col-md-4 label ">color:</div>
|
|
||||||
<div class="col-lg-9 col-md-8"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-lg-3 col-md-4 label ">image:</div>
|
|
||||||
<div class="col-lg-9 col-md-8"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ app_name = 'device'
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("add/", views.NewDeviceView.as_view(), name="add"),
|
path("add/", views.NewDeviceView.as_view(), name="add"),
|
||||||
|
path("edit/<int:pk>/", views.EditDeviceView.as_view(), name="edit"),
|
||||||
path("<int:pk>/", views.DetailsView.as_view(), name="details"),
|
path("<int:pk>/", views.DetailsView.as_view(), name="details"),
|
||||||
path("physical/<int:pk>/", views.PhysicalView.as_view(), name="physical_edit"),
|
path("physical/<int:pk>/", views.PhysicalView.as_view(), name="physical_edit"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -6,7 +6,6 @@ from django.views.generic.edit import (
|
||||||
UpdateView,
|
UpdateView,
|
||||||
)
|
)
|
||||||
from dashboard.mixins import DashboardView, DetailsMixin
|
from dashboard.mixins import DashboardView, DetailsMixin
|
||||||
from device.forms import DeviceForm, PhysicalPropsForm
|
|
||||||
from device.models import Device, PhysicalProperties
|
from device.models import Device, PhysicalProperties
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,8 +13,21 @@ class NewDeviceView(DashboardView, CreateView):
|
||||||
template_name = "new_device.html"
|
template_name = "new_device.html"
|
||||||
title = _("New Device")
|
title = _("New Device")
|
||||||
breadcrumb = "Device / New Device"
|
breadcrumb = "Device / New Device"
|
||||||
form_class = DeviceForm
|
|
||||||
success_url = reverse_lazy('dashboard:unassigned_devices')
|
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):
|
def form_valid(self, form):
|
||||||
form.instance.owner = self.request.user
|
form.instance.owner = self.request.user
|
||||||
|
@ -24,6 +36,34 @@ class NewDeviceView(DashboardView, CreateView):
|
||||||
return response
|
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):
|
class DetailsView(DetailsMixin):
|
||||||
template_name = "details.html"
|
template_name = "details.html"
|
||||||
title = _("Device")
|
title = _("Device")
|
||||||
|
@ -35,46 +75,36 @@ class PhysicalView(DashboardView, UpdateView):
|
||||||
template_name = "physical_properties.html"
|
template_name = "physical_properties.html"
|
||||||
title = _("Physical Properties")
|
title = _("Physical Properties")
|
||||||
breadcrumb = "Device / Physical properties"
|
breadcrumb = "Device / Physical properties"
|
||||||
form_class = PhysicalPropsForm
|
|
||||||
success_url = reverse_lazy('dashboard:unassigned_devices')
|
success_url = reverse_lazy('dashboard:unassigned_devices')
|
||||||
model = PhysicalProperties
|
model = PhysicalProperties
|
||||||
|
fields = (
|
||||||
|
"weight",
|
||||||
|
"width",
|
||||||
|
"height",
|
||||||
|
"depth",
|
||||||
|
"color",
|
||||||
|
"image",
|
||||||
|
)
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
pk = kwargs['pk']
|
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)
|
self.device = get_object_or_404(Device, pk=pk)
|
||||||
try:
|
try:
|
||||||
self.object = self.device.physicalproperties
|
self.object = self.device.physicalproperties
|
||||||
except Exception:
|
except Exception:
|
||||||
self.object = PhysicalProperties.objects.create(device=self.device)
|
self.object = PhysicalProperties.objects.create(device=self.device)
|
||||||
self.initial.update({'instance': self.object})
|
kwargs = super().get_form_kwargs()
|
||||||
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,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
self.success_url = reverse_lazy('device:details', self.device.id)
|
self.success_url = reverse_lazy('device:details', args=[self.device.id])
|
||||||
form.instance.owner = self.request.user
|
|
||||||
response = super().form_valid(form)
|
response = super().form_valid(form)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue