Add view for BomItem model

- Create BOM item (auto-add to a parent part)
- Edit / delete
- View details
This commit is contained in:
Oliver 2018-04-15 21:29:24 +10:00
parent 0632609a80
commit 5861296974
10 changed files with 136 additions and 5 deletions

View File

@ -2,7 +2,7 @@ from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from .models import Part, PartCategory
from .models import Part, PartCategory, BomItem
class EditPartForm(forms.ModelForm):
@ -50,4 +50,26 @@ class EditCategoryForm(forms.ModelForm):
'parent',
'name',
'description'
]
class EditBomItemForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EditBomItemForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-edit-part-form'
#self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
#self.helper.form_action = 'submit'
self.helper.add_input(Submit('submit', 'Submit'))
class Meta:
model = BomItem
fields = [
'part',
'sub_part',
'quantity'
]

View File

@ -201,6 +201,9 @@ class BomItem(models.Model):
which parts are required (and in what quatity) to make it
"""
def get_absolute_url(self):
return '/part/bom/{id}/'.format(id=self.id)
# A link to the parent part
# Each part will get a reverse lookup field 'bom_items'
part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='bom_items')

View File

@ -0,0 +1,5 @@
{% extends 'create_edit_obj.html' %}
{% block obj_title %}
Create a new BOM item
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends "delete_obj.html" %}
{% block del_title %}
Are you sure you want to delete this BOM item?
{% endblock %}
{% block del_body %}
Deleting this entry will remove the BOM row from the following part:
<ul class='list-group'>
<li class='list-group-item'>
<b>{{ item.part.name }}</b> - <i>{{ item.part.description }}</i>
</li>
</ul>
{% endblock %}

View File

@ -0,0 +1,18 @@
{% extends "base.html" %}
{% block content %}
<h3>BOM Item</h3>
<table class="table table-striped">
<tr><td>Parent</td><td><a href="{% url 'part-detail' item.part.id %}">{{ item.part.name }}</a></td></tr>
<tr><td>Child</td><td><a href="{% url 'part-detail' item.sub_part.id %}">{{ item.sub_part.name }}</a></td></tr>
<tr><td>Quantity</td><td>{{ item.quantity }}</td></tr>
</table>
<div class='container-fluid'>
<a href="{% url 'bom-item-edit' item.id %}"><button class="btn btn-info">Edit BOM item</button></a>
<a href="{% url 'bom-item-delete' item.id %}"><button class="btn btn-danger">Delete BOM item</button></a>
</div>
{% endblock %}

View File

@ -0,0 +1,5 @@
{% extends 'create_edit_obj.html' %}
{% block obj_title %}
Edit details for BOM item
{% endblock %}

View File

@ -21,4 +21,10 @@
{% endfor %}
</table>
<div class='container-fluid'>
<a href="{% url 'bom-item-create' %}?parent={{ part.id }}">
<button class='btn btn-success'>Add BOM Item</button>
</a>
</div>
{% endblock %}

View File

@ -13,7 +13,7 @@ This part is used to make the following parts:
</tr>
{% for item in part.used_in.all %}
<tr>
<td><a href="{% url 'part-detail' item.part.id %}">{{ item.part.name }}</a></td>
<td><a href="{% url 'part-bom' item.part.id %}">{{ item.part.name }}</a></td>
<td>{{ item.part.description }}</td>
</tr>
{% endfor %}

View File

@ -57,6 +57,13 @@ part_category_urls = [
url('^.*$', views.CategoryDetail.as_view(), name='category-detail'),
]
part_bom_urls = [
url(r'^edit/?', views.BomItemEdit.as_view(), name='bom-item-edit'),
url('^delete/?', views.BomItemDelete.as_view(), name='bom-item-delete'),
url(r'^.*$', views.BomItemDetail.as_view(), name='bom-item-detail'),
]
# URL list for part web interface
part_urls = [
@ -66,12 +73,17 @@ part_urls = [
# Create a new part
url(r'^new/?', views.PartCreate.as_view(), name='part-create'),
# Individual
# Create a new BOM item
url(r'^bom/new/?', views.BomItemCreate.as_view(), name='bom-item-create'),
# Individual part
url(r'^(?P<pk>\d+)/', include(part_detail_urls)),
# Part category
url(r'^category/(?P<pk>\d+)/', include(part_category_urls)),
url(r'^bom/(?P<pk>\d+)/', include(part_bom_urls)),
# Top level part list (display top level parts and categories)
url('', views.PartIndex.as_view(), name='part-index'),

View File

@ -1,5 +1,6 @@
from InvenTree.models import FilterChildren
from .models import PartCategory, Part
from .models import PartCategory, Part, BomItem
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
@ -8,7 +9,7 @@ from django.urls import reverse
from django.views.generic import DetailView, ListView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from .forms import EditPartForm, EditCategoryForm
from .forms import EditPartForm, EditCategoryForm, EditBomItemForm
class PartIndex(ListView):
model = Part
@ -146,3 +147,47 @@ class CategoryCreate(CreateView):
initials['parent'] = get_object_or_404(PartCategory, pk=parent_id)
return initials
class BomItemDetail(DetailView):
context_object_name ='item'
queryset = BomItem.objects.all()
template_name = 'part/bom-detail.html'
class BomItemCreate(CreateView):
model = BomItem
form_class = EditBomItemForm
template_name = 'part/bom-create.html'
def get_initial(self):
# Look for initial values
initials = super(BomItemCreate, self).get_initial().copy()
# Parent part for this item?
parent_id = self.request.GET.get('parent', None)
if parent_id:
initials['part'] = get_object_or_404(Part, pk=parent_id)
return initials
class BomItemEdit(UpdateView):
model = BomItem
form_class = EditBomItemForm
template_name = 'part/bom-edit.html'
class BomItemDelete(DeleteView):
model = BomItem
template_name = 'part/bom-delete.html'
context_object_name = 'item'
success_url = '/part'
def post(self, request, *args, **kwargs):
if 'confirm' in request.POST:
return super(BomItemDelete, self).post(request, *args, **kwargs)
else:
return HttpResponseRedirect(self.get_object().get_absolute_url())