edit device and physicalproperties
This commit is contained in:
parent
df63af3ee0
commit
cd4156aac3
|
@ -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",
|
||||
]
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
<h5 class="card-title">Details</h5>
|
||||
<div class="row mb-3">
|
||||
<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 class="col-lg-9 col-md-8">
|
||||
{%if object.hid %}Snapshot{% else %}Placeholder{% endif %}
|
||||
|
@ -92,46 +92,57 @@
|
|||
<h5 class="card-title">Physical Properties</h5>
|
||||
<div class="row mb-3">
|
||||
<div class="col-lg-3 col-md-4 label ">
|
||||
<a href="{% url 'device:details' object.pk %}">{{ object.type }} {{ object.manufacturer }} {{ object.model }}</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>)
|
||||
(<a href="{% url 'device:physical_edit' object.pk %}">Edit Physical Properties</a>)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mb-3">
|
||||
{% if object.has_physical_properties %}
|
||||
<div class="row">
|
||||
<div class="col-lg-3 col-md-4 label ">
|
||||
Weight:
|
||||
</div>
|
||||
<div class="col-lg-9 col-md-8">
|
||||
{{ object.physicalproperties.weight }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<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 class="row">
|
||||
<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 class="row">
|
||||
<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 class="row">
|
||||
<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 class="row">
|
||||
<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>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<div class="col">
|
||||
<div class="nav nav-tabs nav-tabs-bordered">
|
||||
<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 class="nav-items">
|
||||
<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>
|
||||
<div class="row mb-3">
|
||||
<div class="col-lg-3 col-md-4 label ">
|
||||
<a href="{% url 'device:details' object.pk %}">{{ object.type }} {{ object.manufacturer }} {{ object.model }}</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>)
|
||||
(<a href="{% url 'device:physical_edit' object.pk %}">Edit Physical Properties</a>)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mb-3">
|
||||
<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 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>
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ app_name = 'device'
|
|||
|
||||
urlpatterns = [
|
||||
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("physical/<int:pk>/", views.PhysicalView.as_view(), name="physical_edit"),
|
||||
]
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue