2018-04-16 14:32:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
2018-04-15 11:29:24 +00:00
|
|
|
|
2018-04-15 15:02:17 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-04-25 04:10:56 +00:00
|
|
|
|
|
|
|
from django.urls import reverse_lazy
|
2018-04-13 12:36:59 +00:00
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
from django.views.generic import DetailView, ListView
|
2018-04-13 12:36:59 +00:00
|
|
|
|
2018-04-22 13:07:23 +00:00
|
|
|
from company.models import Company
|
2018-04-15 15:02:17 +00:00
|
|
|
from .models import PartCategory, Part, BomItem
|
2018-04-22 11:54:12 +00:00
|
|
|
from .models import SupplierPart
|
|
|
|
|
2018-04-29 02:25:07 +00:00
|
|
|
from .forms import PartImageForm
|
2018-04-22 11:54:12 +00:00
|
|
|
from .forms import EditPartForm
|
|
|
|
from .forms import EditCategoryForm
|
|
|
|
from .forms import EditBomItemForm
|
2019-04-13 12:46:26 +00:00
|
|
|
from .forms import BomExportForm
|
2018-04-22 11:54:12 +00:00
|
|
|
|
|
|
|
from .forms import EditSupplierPartForm
|
2018-04-15 15:02:17 +00:00
|
|
|
|
2019-04-13 11:04:23 +00:00
|
|
|
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
|
2018-04-13 12:36:59 +00:00
|
|
|
|
2019-04-13 14:35:19 +00:00
|
|
|
from InvenTree.helpers import DownloadFile
|
2018-04-27 15:16:47 +00:00
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
class PartIndex(ListView):
|
|
|
|
model = Part
|
2018-05-04 08:53:39 +00:00
|
|
|
template_name = 'part/category.html'
|
2018-04-14 15:18:12 +00:00
|
|
|
context_object_name = 'parts'
|
2018-04-13 12:36:59 +00:00
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
def get_queryset(self):
|
2018-04-23 23:06:30 +00:00
|
|
|
return Part.objects.all() # filter(category=None)
|
2018-04-13 12:36:59 +00:00
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2018-04-13 14:08:30 +00:00
|
|
|
|
2018-04-15 10:10:49 +00:00
|
|
|
context = super(PartIndex, self).get_context_data(**kwargs).copy()
|
2018-04-13 14:08:30 +00:00
|
|
|
|
2018-04-15 01:40:03 +00:00
|
|
|
# View top-level categories
|
|
|
|
children = PartCategory.objects.filter(parent=None)
|
2018-04-13 14:08:30 +00:00
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
context['children'] = children
|
2018-04-14 11:58:01 +00:00
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
return context
|
2018-04-13 14:08:30 +00:00
|
|
|
|
|
|
|
|
2018-04-26 08:22:41 +00:00
|
|
|
class PartCreate(AjaxCreateView):
|
2018-04-15 00:08:44 +00:00
|
|
|
""" Create a new part
|
|
|
|
- Optionally provide a category object as initial data
|
|
|
|
"""
|
|
|
|
model = Part
|
|
|
|
form_class = EditPartForm
|
|
|
|
template_name = 'part/create.html'
|
|
|
|
|
2018-04-26 08:22:41 +00:00
|
|
|
ajax_form_title = 'Create new part'
|
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
|
2018-04-29 14:23:02 +00:00
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'success': "Created new part",
|
|
|
|
}
|
|
|
|
|
2018-04-15 00:08:44 +00:00
|
|
|
def get_category_id(self):
|
|
|
|
return self.request.GET.get('category', None)
|
|
|
|
|
|
|
|
# If a category is provided in the URL, pass that to the page context
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PartCreate, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
# Add category information to the page
|
|
|
|
cat_id = self.get_category_id()
|
|
|
|
|
|
|
|
if cat_id:
|
|
|
|
context['category'] = get_object_or_404(PartCategory, pk=cat_id)
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
# Pre-fill the category field if a valid category is provided
|
|
|
|
def get_initial(self):
|
|
|
|
|
|
|
|
initials = super(PartCreate, self).get_initial().copy()
|
|
|
|
|
|
|
|
if self.get_category_id():
|
|
|
|
initials['category'] = get_object_or_404(PartCategory, pk=self.get_category_id())
|
|
|
|
|
|
|
|
return initials
|
|
|
|
|
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
class PartDetail(DetailView):
|
|
|
|
context_object_name = 'part'
|
|
|
|
queryset = Part.objects.all()
|
|
|
|
template_name = 'part/detail.html'
|
2018-04-13 14:46:18 +00:00
|
|
|
|
2019-04-15 08:32:15 +00:00
|
|
|
# Add in some extra context information based on query params
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PartDetail, self).get_context_data(**kwargs)
|
|
|
|
|
2019-04-15 13:30:17 +00:00
|
|
|
if self.request.GET.get('edit', '').lower() in ['true', 'yes', '1']:
|
2019-04-15 08:41:48 +00:00
|
|
|
context['editing_enabled'] = 1
|
2019-04-15 08:32:15 +00:00
|
|
|
else:
|
2019-04-15 08:41:48 +00:00
|
|
|
context['editing_enabled'] = 0
|
2019-04-15 08:32:15 +00:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
2018-04-13 14:46:18 +00:00
|
|
|
|
2018-04-29 02:25:07 +00:00
|
|
|
class PartImage(AjaxUpdateView):
|
|
|
|
|
|
|
|
model = Part
|
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Upload Part Image'
|
|
|
|
form_class = PartImageForm
|
|
|
|
|
2018-05-03 13:40:27 +00:00
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'success': 'Updated part image',
|
|
|
|
}
|
|
|
|
|
2018-04-29 02:25:07 +00:00
|
|
|
|
2018-04-26 13:28:27 +00:00
|
|
|
class PartEdit(AjaxUpdateView):
|
2018-04-14 15:18:12 +00:00
|
|
|
model = Part
|
|
|
|
form_class = EditPartForm
|
|
|
|
template_name = 'part/edit.html'
|
2018-04-26 13:28:27 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Edit Part Properties'
|
2018-04-14 06:26:26 +00:00
|
|
|
|
|
|
|
|
2019-04-13 14:35:19 +00:00
|
|
|
class BomExport(AjaxView):
|
|
|
|
# TODO - This should no longer extend an AjaxView!
|
2019-04-13 11:04:23 +00:00
|
|
|
|
|
|
|
model = Part
|
2019-04-13 14:35:19 +00:00
|
|
|
#form_class = BomExportForm
|
|
|
|
#template_name = 'part/bom_export.html'
|
|
|
|
#ajax_form_title = 'Export Bill of Materials'
|
|
|
|
#ajax_submit_text = 'Export'
|
|
|
|
#context_object_name = 'part'
|
2019-04-13 11:04:23 +00:00
|
|
|
|
2019-04-13 12:22:04 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
part = get_object_or_404(Part, pk=self.kwargs['pk'])
|
|
|
|
|
2019-04-13 14:35:19 +00:00
|
|
|
export_format = request.GET.get('format', 'csv')
|
|
|
|
|
|
|
|
# Placeholder to test file export
|
|
|
|
filename = '"' + part.name + '_BOM.' + export_format + '"'
|
2019-04-13 12:22:04 +00:00
|
|
|
|
2019-04-13 14:50:43 +00:00
|
|
|
filedata = part.export_bom()
|
2019-04-13 12:46:26 +00:00
|
|
|
|
2019-04-13 14:35:19 +00:00
|
|
|
return DownloadFile(filedata, filename)
|
2019-04-13 11:04:23 +00:00
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'info': 'Exported BOM'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-26 14:06:44 +00:00
|
|
|
class PartDelete(AjaxDeleteView):
|
2018-04-14 22:45:50 +00:00
|
|
|
model = Part
|
|
|
|
template_name = 'part/delete.html'
|
2018-04-26 14:06:44 +00:00
|
|
|
ajax_template_name = 'part/partial_delete.html'
|
|
|
|
ajax_form_title = 'Confirm Part Deletion'
|
2018-04-14 22:45:50 +00:00
|
|
|
|
|
|
|
success_url = '/part/'
|
|
|
|
|
2018-04-29 14:23:02 +00:00
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'danger': 'Part was deleted',
|
|
|
|
}
|
2018-04-14 22:45:50 +00:00
|
|
|
|
2018-04-15 01:40:03 +00:00
|
|
|
|
|
|
|
class CategoryDetail(DetailView):
|
|
|
|
model = PartCategory
|
|
|
|
context_object_name = 'category'
|
|
|
|
queryset = PartCategory.objects.all()
|
2018-05-04 08:53:39 +00:00
|
|
|
template_name = 'part/category.html'
|
2018-04-15 01:40:03 +00:00
|
|
|
|
|
|
|
|
2018-04-27 10:42:12 +00:00
|
|
|
class CategoryEdit(AjaxUpdateView):
|
2018-04-15 01:40:03 +00:00
|
|
|
model = PartCategory
|
|
|
|
template_name = 'part/category_edit.html'
|
|
|
|
form_class = EditCategoryForm
|
2018-04-27 10:42:12 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Edit Part Category'
|
2018-04-15 01:40:03 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CategoryEdit, self).get_context_data(**kwargs).copy()
|
|
|
|
|
|
|
|
context['category'] = get_object_or_404(PartCategory, pk=self.kwargs['pk'])
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-04-27 10:42:12 +00:00
|
|
|
class CategoryDelete(AjaxDeleteView):
|
2018-04-15 01:40:03 +00:00
|
|
|
model = PartCategory
|
|
|
|
template_name = 'part/category_delete.html'
|
|
|
|
context_object_name = 'category'
|
2018-04-15 15:02:17 +00:00
|
|
|
success_url = '/part/'
|
2018-04-15 01:40:03 +00:00
|
|
|
|
2018-04-29 14:23:02 +00:00
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'danger': 'Part category was deleted',
|
|
|
|
}
|
|
|
|
|
2018-04-15 01:40:03 +00:00
|
|
|
|
2018-04-25 04:10:56 +00:00
|
|
|
class CategoryCreate(AjaxCreateView):
|
2018-04-15 01:40:03 +00:00
|
|
|
model = PartCategory
|
2018-04-25 04:10:56 +00:00
|
|
|
ajax_form_action = reverse_lazy('category-create')
|
|
|
|
ajax_form_title = 'Create new part category'
|
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
template_name = 'part/category_new.html'
|
2018-04-15 01:40:03 +00:00
|
|
|
form_class = EditCategoryForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CategoryCreate, self).get_context_data(**kwargs).copy()
|
|
|
|
|
|
|
|
parent_id = self.request.GET.get('category', None)
|
|
|
|
|
|
|
|
if parent_id:
|
|
|
|
context['category'] = get_object_or_404(PartCategory, pk=parent_id)
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
initials = super(CategoryCreate, self).get_initial().copy()
|
|
|
|
|
|
|
|
parent_id = self.request.GET.get('category', None)
|
|
|
|
|
|
|
|
if parent_id:
|
|
|
|
initials['parent'] = get_object_or_404(PartCategory, pk=parent_id)
|
|
|
|
|
|
|
|
return initials
|
2018-04-15 11:29:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BomItemDetail(DetailView):
|
2018-04-15 15:02:17 +00:00
|
|
|
context_object_name = 'item'
|
2018-04-15 11:29:24 +00:00
|
|
|
queryset = BomItem.objects.all()
|
|
|
|
template_name = 'part/bom-detail.html'
|
|
|
|
|
|
|
|
|
2018-04-25 23:26:43 +00:00
|
|
|
class BomItemCreate(AjaxCreateView):
|
2018-04-15 11:29:24 +00:00
|
|
|
model = BomItem
|
|
|
|
form_class = EditBomItemForm
|
|
|
|
template_name = 'part/bom-create.html'
|
2018-04-25 23:26:43 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Create BOM item'
|
2019-04-13 12:46:26 +00:00
|
|
|
ajax_submit_text = 'Create'
|
2018-04-15 11:29:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-04-25 23:26:43 +00:00
|
|
|
class BomItemEdit(AjaxUpdateView):
|
2018-04-15 11:29:24 +00:00
|
|
|
model = BomItem
|
|
|
|
form_class = EditBomItemForm
|
|
|
|
template_name = 'part/bom-edit.html'
|
2018-04-25 23:26:43 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Edit BOM item'
|
2018-04-15 11:29:24 +00:00
|
|
|
|
|
|
|
|
2018-04-26 14:54:01 +00:00
|
|
|
class BomItemDelete(AjaxDeleteView):
|
2018-04-15 11:29:24 +00:00
|
|
|
model = BomItem
|
|
|
|
template_name = 'part/bom-delete.html'
|
|
|
|
context_object_name = 'item'
|
2018-04-26 14:54:01 +00:00
|
|
|
ajax_form_title = 'Confim BOM item deletion'
|
2018-04-15 11:29:24 +00:00
|
|
|
|
2018-04-22 11:54:12 +00:00
|
|
|
|
|
|
|
class SupplierPartDetail(DetailView):
|
|
|
|
model = SupplierPart
|
|
|
|
template_name = 'company/partdetail.html'
|
|
|
|
context_object_name = 'part'
|
|
|
|
queryset = SupplierPart.objects.all()
|
|
|
|
|
|
|
|
|
2018-04-27 11:32:48 +00:00
|
|
|
class SupplierPartEdit(AjaxUpdateView):
|
2018-04-22 11:54:12 +00:00
|
|
|
model = SupplierPart
|
|
|
|
template_name = 'company/partedit.html'
|
|
|
|
context_object_name = 'part'
|
|
|
|
form_class = EditSupplierPartForm
|
2018-04-27 11:32:48 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Edit Supplier Part'
|
2018-04-22 11:54:12 +00:00
|
|
|
|
|
|
|
|
2018-04-27 11:32:48 +00:00
|
|
|
class SupplierPartCreate(AjaxCreateView):
|
2018-04-22 11:54:12 +00:00
|
|
|
model = SupplierPart
|
|
|
|
form_class = EditSupplierPartForm
|
2018-04-27 11:32:48 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Create new Supplier Part'
|
2018-04-27 15:16:47 +00:00
|
|
|
template_name = 'company/partcreate.html'
|
2018-04-22 11:54:12 +00:00
|
|
|
context_object_name = 'part'
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
initials = super(SupplierPartCreate, self).get_initial().copy()
|
|
|
|
|
|
|
|
supplier_id = self.request.GET.get('supplier', None)
|
|
|
|
part_id = self.request.GET.get('part', None)
|
|
|
|
|
|
|
|
if supplier_id:
|
2018-04-22 13:07:23 +00:00
|
|
|
initials['supplier'] = get_object_or_404(Company, pk=supplier_id)
|
2018-04-28 02:14:18 +00:00
|
|
|
# TODO
|
|
|
|
# self.fields['supplier'].disabled = True
|
2018-04-22 11:54:12 +00:00
|
|
|
if part_id:
|
|
|
|
initials['part'] = get_object_or_404(Part, pk=part_id)
|
2018-04-28 02:14:18 +00:00
|
|
|
# TODO
|
|
|
|
# self.fields['part'].disabled = True
|
2018-04-22 11:54:12 +00:00
|
|
|
|
|
|
|
return initials
|
|
|
|
|
|
|
|
|
2018-04-27 11:32:48 +00:00
|
|
|
class SupplierPartDelete(AjaxDeleteView):
|
2018-04-22 11:54:12 +00:00
|
|
|
model = SupplierPart
|
|
|
|
success_url = '/supplier/'
|
|
|
|
template_name = 'company/partdelete.html'
|