2019-04-27 12:18:07 +00:00
|
|
|
"""
|
|
|
|
Django views for interacting with Part app
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
2019-05-06 14:04:35 +00:00
|
|
|
from django.core.validators import URLValidator
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
2019-05-06 11:49:01 +00:00
|
|
|
from django.http import JsonResponse
|
2019-04-16 12:32:43 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-04-14 15:18:12 +00:00
|
|
|
from django.views.generic import DetailView, ListView
|
2019-04-18 13:12:29 +00:00
|
|
|
from django.forms.models import model_to_dict
|
2019-04-29 08:35:16 +00:00
|
|
|
from django.forms import HiddenInput
|
2018-04-13 12:36:59 +00:00
|
|
|
|
2018-04-22 13:07:23 +00:00
|
|
|
from company.models import Company
|
2019-05-02 07:29:21 +00:00
|
|
|
from .models import PartCategory, Part, PartAttachment
|
|
|
|
from .models import 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
|
2019-05-02 07:29:21 +00:00
|
|
|
from .forms import EditPartAttachmentForm
|
2018-04-22 11:54:12 +00:00
|
|
|
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-16 12:32:43 +00:00
|
|
|
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
|
2019-05-04 08:46:57 +00:00
|
|
|
from InvenTree.views import QRCodeView
|
2018-04-13 12:36:59 +00:00
|
|
|
|
2019-05-06 14:04:35 +00:00
|
|
|
from InvenTree.helpers import DownloadFile, str2bool, TestIfImage, TestIfImageURL
|
2018-04-27 15:16:47 +00:00
|
|
|
|
2019-04-16 12:32:43 +00:00
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
class PartIndex(ListView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" View for displaying list of Part objects
|
|
|
|
"""
|
2018-04-14 15:18:12 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2019-05-02 07:29:21 +00:00
|
|
|
class PartAttachmentCreate(AjaxCreateView):
|
|
|
|
""" View for creating a new PartAttachment object
|
|
|
|
|
|
|
|
- The view only makes sense if a Part object is passed to it
|
|
|
|
"""
|
|
|
|
model = PartAttachment
|
|
|
|
form_class = EditPartAttachmentForm
|
|
|
|
ajax_form_title = "Add part attachment"
|
|
|
|
ajax_template_name = "modal_form.html"
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'success': 'Added attachment'
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
""" Get initial data for new PartAttachment object.
|
|
|
|
|
|
|
|
- Client should have requested this form with a parent part in mind
|
|
|
|
- e.g. ?part=<pk>
|
|
|
|
"""
|
|
|
|
|
|
|
|
initials = super(AjaxCreateView, self).get_initial()
|
|
|
|
|
|
|
|
# TODO - If the proper part was not sent, return an error message
|
|
|
|
initials['part'] = Part.objects.get(id=self.request.GET.get('part'))
|
|
|
|
|
|
|
|
return initials
|
|
|
|
|
|
|
|
def get_form(self):
|
|
|
|
""" Create a form to upload a new PartAttachment
|
|
|
|
|
|
|
|
- Hide the 'part' field
|
|
|
|
"""
|
|
|
|
|
|
|
|
form = super(AjaxCreateView, self).get_form()
|
|
|
|
|
|
|
|
form.fields['part'].widget = HiddenInput()
|
|
|
|
|
|
|
|
return form
|
|
|
|
|
|
|
|
|
|
|
|
class PartAttachmentEdit(AjaxUpdateView):
|
|
|
|
""" View for editing a PartAttachment object """
|
|
|
|
model = PartAttachment
|
|
|
|
form_class = EditPartAttachmentForm
|
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Edit attachment'
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'success': 'Part attachment updated'
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_form(self):
|
|
|
|
form = super(AjaxUpdateView, self).get_form()
|
|
|
|
|
|
|
|
form.fields['part'].widget = HiddenInput()
|
|
|
|
|
|
|
|
return form
|
|
|
|
|
|
|
|
|
|
|
|
class PartAttachmentDelete(AjaxDeleteView):
|
|
|
|
""" View for deleting a PartAttachment """
|
|
|
|
|
|
|
|
model = PartAttachment
|
2019-05-03 15:03:43 +00:00
|
|
|
ajax_form_title = "Delete Part Attachment"
|
2019-05-02 07:29:21 +00:00
|
|
|
ajax_template_name = "part/attachment_delete.html"
|
|
|
|
context_object_name = "attachment"
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'danger': 'Deleted part attachment'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-26 08:22:41 +00:00
|
|
|
class PartCreate(AjaxCreateView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" View for creating a new Part object.
|
|
|
|
|
|
|
|
Options for providing initial conditions:
|
|
|
|
|
|
|
|
- Provide a category object as initial data
|
|
|
|
- Copy an existing Part
|
2018-04-15 00:08:44 +00:00
|
|
|
"""
|
|
|
|
model = Part
|
|
|
|
form_class = EditPartForm
|
2019-04-18 13:47:04 +00:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Provide extra context information for the form to display:
|
|
|
|
|
|
|
|
- Add category information (if provided)
|
|
|
|
"""
|
2018-04-15 00:08:44 +00:00
|
|
|
context = super(PartCreate, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
# Add category information to the page
|
|
|
|
cat_id = self.get_category_id()
|
|
|
|
|
|
|
|
if cat_id:
|
2019-04-28 01:09:19 +00:00
|
|
|
try:
|
|
|
|
context['category'] = PartCategory.objects.get(pk=cat_id)
|
|
|
|
except PartCategory.DoesNotExist:
|
|
|
|
pass
|
2018-04-15 00:08:44 +00:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
2019-04-28 13:55:21 +00:00
|
|
|
def get_form(self):
|
2019-04-28 13:57:29 +00:00
|
|
|
""" Create Form for making new Part object.
|
|
|
|
Remove the 'default_supplier' field as there are not yet any matching SupplierPart objects
|
|
|
|
"""
|
2019-04-28 13:55:21 +00:00
|
|
|
form = super(AjaxCreateView, self).get_form()
|
|
|
|
|
|
|
|
# Hide the default_supplier field (there are no matching supplier parts yet!)
|
2019-04-30 23:40:49 +00:00
|
|
|
form.fields['default_supplier'].widget = HiddenInput()
|
2019-04-28 13:57:29 +00:00
|
|
|
|
2019-04-28 13:55:21 +00:00
|
|
|
return form
|
|
|
|
|
2018-04-15 00:08:44 +00:00
|
|
|
def get_initial(self):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Get initial data for the new Part object:
|
|
|
|
|
|
|
|
- If a category is provided, pre-fill the Category field
|
|
|
|
- If 'copy' parameter is provided, copy from referenced Part
|
|
|
|
"""
|
2018-04-15 00:08:44 +00:00
|
|
|
|
2019-04-18 13:12:29 +00:00
|
|
|
# Is the client attempting to copy an existing part?
|
2019-04-18 13:28:46 +00:00
|
|
|
part_to_copy = self.request.GET.get('copy', None)
|
2019-04-18 13:12:29 +00:00
|
|
|
|
|
|
|
if part_to_copy:
|
|
|
|
try:
|
|
|
|
original = Part.objects.get(pk=part_to_copy)
|
|
|
|
initials = model_to_dict(original)
|
2019-04-18 13:22:58 +00:00
|
|
|
self.ajax_form_title = "Copy Part '{p}'".format(p=original.name)
|
2019-04-18 13:12:29 +00:00
|
|
|
except Part.DoesNotExist:
|
|
|
|
initials = super(PartCreate, self).get_initial()
|
|
|
|
|
|
|
|
else:
|
2019-04-18 13:47:04 +00:00
|
|
|
initials = super(PartCreate, self).get_initial()
|
2018-04-15 00:08:44 +00:00
|
|
|
|
|
|
|
if self.get_category_id():
|
2019-04-28 01:09:19 +00:00
|
|
|
try:
|
|
|
|
initials['category'] = PartCategory.objects.get(pk=self.get_category_id())
|
|
|
|
except PartCategory.DoesNotExist:
|
|
|
|
pass
|
2018-04-15 00:08:44 +00:00
|
|
|
|
|
|
|
return initials
|
|
|
|
|
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
class PartDetail(DetailView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Detail view for Part object
|
|
|
|
"""
|
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Provide extra context data to template
|
|
|
|
|
|
|
|
- If '?editing=True', set 'editing_enabled' context variable
|
|
|
|
"""
|
2019-04-15 08:32:15 +00:00
|
|
|
context = super(PartDetail, self).get_context_data(**kwargs)
|
|
|
|
|
2019-04-27 12:18:07 +00:00
|
|
|
if str2bool(self.request.GET.get('edit', '')):
|
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
|
|
|
|
2019-05-05 00:54:21 +00:00
|
|
|
part = self.get_object()
|
|
|
|
|
|
|
|
context['starred'] = part.isStarredBy(self.request.user)
|
|
|
|
|
2019-04-15 08:32:15 +00:00
|
|
|
return context
|
|
|
|
|
2018-04-13 14:46:18 +00:00
|
|
|
|
2019-05-04 08:46:57 +00:00
|
|
|
class PartQRCode(QRCodeView):
|
|
|
|
""" View for displaying a QR code for a Part object """
|
|
|
|
|
|
|
|
ajax_form_title = "Part QR Code"
|
|
|
|
|
|
|
|
def get_qr_data(self):
|
|
|
|
""" Generate QR code data for the Part """
|
|
|
|
|
|
|
|
try:
|
|
|
|
part = Part.objects.get(id=self.pk)
|
|
|
|
return part.format_barcode()
|
|
|
|
except Part.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-04-29 02:25:07 +00:00
|
|
|
class PartImage(AjaxUpdateView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" View for uploading Part image """
|
2018-04-29 02:25:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-05-06 11:49:01 +00:00
|
|
|
class UploadPartImage(AjaxView):
|
2019-05-06 14:04:35 +00:00
|
|
|
""" View for uploading a Part image via AJAX request.
|
|
|
|
e.g. via a "drag and drop" event.
|
|
|
|
|
|
|
|
There are two ways to upload a file:
|
|
|
|
|
|
|
|
1. Attach an image file as request.FILES['image_file']
|
|
|
|
- Image is validated saved
|
|
|
|
- Part object is saved
|
|
|
|
2. Attach an iamge URL as request.POST['image_url']
|
|
|
|
NOT YET IMPLEMENTED
|
|
|
|
- Image URL is valiated
|
|
|
|
- Image is downloaded and validated
|
|
|
|
- Part object is saved
|
|
|
|
"""
|
2019-05-06 11:49:01 +00:00
|
|
|
|
|
|
|
model = Part
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2019-05-06 12:20:06 +00:00
|
|
|
|
|
|
|
response = {}
|
|
|
|
status = 200
|
|
|
|
|
2019-05-06 11:49:01 +00:00
|
|
|
try:
|
|
|
|
part = Part.objects.get(pk=kwargs.get('pk'))
|
|
|
|
except Part.DoesNotExist:
|
2019-05-06 12:20:06 +00:00
|
|
|
response['error'] = 'Part not found'
|
2019-05-06 14:11:27 +00:00
|
|
|
return JsonResponse(response, status=404)
|
2019-05-06 11:49:01 +00:00
|
|
|
|
2019-05-06 14:04:35 +00:00
|
|
|
# Direct image upload
|
|
|
|
if 'image_file' in request.FILES:
|
|
|
|
image = request.FILES['image_file']
|
2019-05-06 11:49:01 +00:00
|
|
|
|
2019-05-06 14:04:35 +00:00
|
|
|
if TestIfImage(image):
|
|
|
|
part.image = image
|
|
|
|
part.clean()
|
|
|
|
part.save()
|
2019-05-06 12:20:06 +00:00
|
|
|
|
2019-05-06 14:04:35 +00:00
|
|
|
response['success'] = 'File was uploaded successfully'
|
|
|
|
status = 200
|
|
|
|
else:
|
|
|
|
response['error'] = 'Not a valid image file'
|
|
|
|
status = 400
|
|
|
|
|
|
|
|
return JsonResponse(response, status=status)
|
|
|
|
|
|
|
|
elif 'image_url' in request.POST:
|
2019-05-06 22:07:33 +00:00
|
|
|
image_url = str(request.POST['image_url']).split('?')[0]
|
2019-05-06 14:04:35 +00:00
|
|
|
|
|
|
|
validator = URLValidator()
|
|
|
|
|
|
|
|
try:
|
|
|
|
validator(image_url)
|
2019-05-06 14:11:27 +00:00
|
|
|
except ValidationError:
|
2019-05-06 14:04:35 +00:00
|
|
|
response['error'] = 'Invalid image URL'
|
|
|
|
response['url'] = image_url
|
|
|
|
|
|
|
|
return JsonResponse(response, status=400)
|
|
|
|
|
|
|
|
# Test the the URL at least looks like an image
|
|
|
|
if not TestIfImageURL(image_url):
|
|
|
|
response['error'] = 'Invalid image URL'
|
2019-05-06 22:07:33 +00:00
|
|
|
response['url'] = image_url
|
2019-05-06 14:04:35 +00:00
|
|
|
return JsonResponse(response, status=400)
|
|
|
|
|
|
|
|
response['error'] = 'Cannot download cross-site images (yet)'
|
|
|
|
response['url'] = image_url
|
|
|
|
response['apology'] = 'deepest'
|
|
|
|
|
|
|
|
return JsonResponse(response, status=400)
|
|
|
|
|
|
|
|
# TODO - Attempt to download the image file here
|
|
|
|
|
|
|
|
"""
|
|
|
|
head = requests.head(url, stream=True, headers={'User-agent': 'Mozilla/5.0'})
|
|
|
|
|
|
|
|
if head.headers['Content-Length'] < SOME_MAX_LENGTH:
|
|
|
|
|
|
|
|
image = requests.get(url, stream=True, headers={'User-agent': 'Mozilla/5.0'})
|
|
|
|
|
|
|
|
file = io.BytesIO(image.raw.read())
|
|
|
|
|
|
|
|
- Save the file?
|
|
|
|
|
|
|
|
"""
|
2019-05-06 11:49:01 +00:00
|
|
|
|
2019-05-06 14:04:35 +00:00
|
|
|
# Default response
|
2019-05-06 12:20:06 +00:00
|
|
|
return JsonResponse(response, status=status)
|
2019-05-06 11:49:01 +00:00
|
|
|
|
|
|
|
|
2018-04-26 13:28:27 +00:00
|
|
|
class PartEdit(AjaxUpdateView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" View for editing Part object """
|
|
|
|
|
2018-04-14 15:18:12 +00:00
|
|
|
model = Part
|
2019-04-16 11:25:20 +00:00
|
|
|
form_class = EditPartForm
|
2018-04-26 13:28:27 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Edit Part Properties'
|
2019-04-16 11:25:20 +00:00
|
|
|
context_object_name = 'part'
|
2018-04-14 06:26:26 +00:00
|
|
|
|
2019-04-28 13:50:35 +00:00
|
|
|
def get_form(self):
|
|
|
|
""" Create form for Part editing.
|
|
|
|
Overrides default get_form() method to limit the choices
|
|
|
|
for the 'default_supplier' field to SupplierParts that reference this part
|
|
|
|
"""
|
|
|
|
|
|
|
|
form = super(AjaxUpdateView, self).get_form()
|
|
|
|
|
|
|
|
part = self.get_object()
|
|
|
|
|
|
|
|
form.fields['default_supplier'].queryset = SupplierPart.objects.filter(part=part)
|
|
|
|
|
|
|
|
return form
|
|
|
|
|
|
|
|
|
2019-04-13 14:35:19 +00:00
|
|
|
class BomExport(AjaxView):
|
2019-04-16 11:25:20 +00:00
|
|
|
|
|
|
|
model = Part
|
|
|
|
ajax_form_title = 'Export BOM'
|
2019-04-16 11:46:12 +00:00
|
|
|
ajax_template_name = 'part/bom_export.html'
|
2019-04-16 11:25:20 +00:00
|
|
|
context_object_name = 'part'
|
|
|
|
form_class = BomExportForm
|
|
|
|
|
2019-04-16 11:46:12 +00:00
|
|
|
def get_object(self):
|
|
|
|
return get_object_or_404(Part, pk=self.kwargs['pk'])
|
|
|
|
|
2019-04-16 11:25:20 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class()
|
|
|
|
|
2019-04-16 12:32:43 +00:00
|
|
|
"""
|
2019-04-16 11:46:12 +00:00
|
|
|
part = self.get_object()
|
2019-04-16 11:25:20 +00:00
|
|
|
|
|
|
|
context = {
|
|
|
|
'part': part
|
|
|
|
}
|
|
|
|
|
2019-04-16 11:46:12 +00:00
|
|
|
if request.is_ajax():
|
2019-04-16 12:32:43 +00:00
|
|
|
passs
|
|
|
|
"""
|
2019-04-16 11:46:12 +00:00
|
|
|
|
2019-04-16 12:32:43 +00:00
|
|
|
return self.renderJsonResponse(request, form)
|
2019-04-16 11:25:20 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2019-04-16 11:46:12 +00:00
|
|
|
"""
|
|
|
|
User has now submitted the BOM export data
|
2019-04-16 12:32:43 +00:00
|
|
|
"""
|
2019-04-16 11:25:20 +00:00
|
|
|
|
2019-04-16 12:32:43 +00:00
|
|
|
# part = self.get_object()
|
2019-04-16 11:25:20 +00:00
|
|
|
|
2019-04-16 11:46:12 +00:00
|
|
|
return super(AjaxView, self).post(request, *args, **kwargs)
|
2019-04-16 11:25:20 +00:00
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
2019-04-16 12:32:43 +00:00
|
|
|
# 'form_valid': True,
|
|
|
|
# 'redirect': '/'
|
|
|
|
# 'redirect': reverse('bom-download', kwargs={'pk': self.request.GET.get('pk')})
|
2019-04-16 11:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class BomDownload(AjaxView):
|
|
|
|
"""
|
|
|
|
Provide raw download of a BOM file.
|
|
|
|
- File format should be passed as a query param e.g. ?format=csv
|
|
|
|
"""
|
|
|
|
|
2019-04-13 14:35:19 +00:00
|
|
|
# TODO - This should no longer extend an AjaxView!
|
2019-04-13 11:04:23 +00:00
|
|
|
|
|
|
|
model = Part
|
2019-04-16 12:32:43 +00:00
|
|
|
# form_class = BomExportForm
|
|
|
|
# template_name = 'part/bom_export.html'
|
|
|
|
# ajax_form_title = 'Export Bill of Materials'
|
|
|
|
# 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')
|
2019-04-16 11:46:12 +00:00
|
|
|
|
2019-04-13 14:35:19 +00:00
|
|
|
# Placeholder to test file export
|
|
|
|
filename = '"' + part.name + '_BOM.' + export_format + '"'
|
2019-04-13 12:22:04 +00:00
|
|
|
|
2019-04-16 11:46:12 +00:00
|
|
|
filedata = part.export_bom(format=export_format)
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" View to delete a Part object """
|
|
|
|
|
2018-04-14 22:45:50 +00:00
|
|
|
model = Part
|
2018-04-26 14:06:44 +00:00
|
|
|
ajax_template_name = 'part/partial_delete.html'
|
|
|
|
ajax_form_title = 'Confirm Part Deletion'
|
2019-04-16 11:25:20 +00:00
|
|
|
context_object_name = 'part'
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Detail view for PartCategory """
|
2018-04-15 01:40:03 +00:00
|
|
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Update view to edit a PartCategory """
|
2018-04-15 01:40:03 +00:00
|
|
|
model = PartCategory
|
|
|
|
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()
|
|
|
|
|
2019-04-28 01:09:19 +00:00
|
|
|
try:
|
|
|
|
context['category'] = PartCategory.objects.get(pk=self.kwargs['pk'])
|
|
|
|
except:
|
|
|
|
pass
|
2018-04-15 01:40:03 +00:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-04-27 10:42:12 +00:00
|
|
|
class CategoryDelete(AjaxDeleteView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Delete view to delete a PartCategory """
|
2018-04-15 01:40:03 +00:00
|
|
|
model = PartCategory
|
2019-04-27 23:00:54 +00:00
|
|
|
ajax_template_name = 'part/category_delete.html'
|
2019-05-04 07:20:05 +00:00
|
|
|
ajax_form_title = 'Delete Part Category'
|
2018-04-15 01:40:03 +00:00
|
|
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Create view to make a new PartCategory """
|
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'
|
2018-04-15 01:40:03 +00:00
|
|
|
form_class = EditCategoryForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Add extra context data to template.
|
|
|
|
|
|
|
|
- If parent category provided, pass the category details to the template
|
|
|
|
"""
|
2018-04-15 01:40:03 +00:00
|
|
|
context = super(CategoryCreate, self).get_context_data(**kwargs).copy()
|
|
|
|
|
|
|
|
parent_id = self.request.GET.get('category', None)
|
|
|
|
|
|
|
|
if parent_id:
|
2019-04-28 01:09:19 +00:00
|
|
|
try:
|
|
|
|
context['category'] = PartCategory.objects.get(pk=parent_id)
|
|
|
|
except PartCategory.DoesNotExist:
|
|
|
|
pass
|
2018-04-15 01:40:03 +00:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_initial(self):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Get initial data for new PartCategory
|
|
|
|
|
|
|
|
- If parent provided, pre-fill the parent category
|
|
|
|
"""
|
2018-04-15 01:40:03 +00:00
|
|
|
initials = super(CategoryCreate, self).get_initial().copy()
|
|
|
|
|
|
|
|
parent_id = self.request.GET.get('category', None)
|
|
|
|
|
|
|
|
if parent_id:
|
2019-04-28 01:09:19 +00:00
|
|
|
try:
|
|
|
|
initials['parent'] = PartCategory.objects.get(pk=parent_id)
|
|
|
|
except PartCategory.DoesNotExist:
|
|
|
|
pass
|
2018-04-15 01:40:03 +00:00
|
|
|
|
|
|
|
return initials
|
2018-04-15 11:29:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BomItemDetail(DetailView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Detail view for BomItem """
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Create view for making a new BomItem object """
|
2018-04-15 11:29:24 +00:00
|
|
|
model = BomItem
|
|
|
|
form_class = EditBomItemForm
|
2018-04-25 23:26:43 +00:00
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Create BOM item'
|
2018-04-15 11:29:24 +00:00
|
|
|
|
2019-04-29 14:16:20 +00:00
|
|
|
def get_form(self):
|
|
|
|
""" Override get_form() method to reduce Part selection options.
|
|
|
|
|
|
|
|
- Do not allow part to be added to its own BOM
|
|
|
|
- Remove any Part items that are already in the BOM
|
|
|
|
"""
|
2019-04-29 14:18:58 +00:00
|
|
|
|
2019-04-29 14:16:20 +00:00
|
|
|
form = super(AjaxCreateView, self).get_form()
|
|
|
|
|
|
|
|
part_id = form['part'].value()
|
|
|
|
|
|
|
|
try:
|
|
|
|
part = Part.objects.get(id=part_id)
|
|
|
|
|
|
|
|
# Don't allow selection of sub_part objects which are already added to the Bom!
|
|
|
|
query = form.fields['sub_part'].queryset
|
|
|
|
|
|
|
|
# Don't allow a part to be added to its own BOM
|
|
|
|
query = query.exclude(id=part.id)
|
|
|
|
|
|
|
|
# Eliminate any options that are already in the BOM!
|
2019-04-29 14:18:58 +00:00
|
|
|
query = query.exclude(id__in=[item.id for item in part.required_parts()])
|
2019-04-29 14:16:20 +00:00
|
|
|
|
|
|
|
form.fields['sub_part'].queryset = query
|
|
|
|
except Part.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return form
|
|
|
|
|
2018-04-15 11:29:24 +00:00
|
|
|
def get_initial(self):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Provide initial data for the BomItem:
|
|
|
|
|
|
|
|
- If 'parent' provided, set the parent part field
|
|
|
|
"""
|
|
|
|
|
2018-04-15 11:29:24 +00:00
|
|
|
# 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:
|
2019-04-28 01:09:19 +00:00
|
|
|
try:
|
|
|
|
initials['part'] = Part.objects.get(pk=parent_id)
|
|
|
|
except Part.DoesNotExist:
|
|
|
|
pass
|
2018-04-15 11:29:24 +00:00
|
|
|
|
|
|
|
return initials
|
|
|
|
|
|
|
|
|
2018-04-25 23:26:43 +00:00
|
|
|
class BomItemEdit(AjaxUpdateView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Update view for editing BomItem """
|
|
|
|
|
2018-04-15 11:29:24 +00:00
|
|
|
model = BomItem
|
|
|
|
form_class = EditBomItemForm
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Delete view for removing BomItem """
|
2018-04-15 11:29:24 +00:00
|
|
|
model = BomItem
|
2019-04-27 23:00:54 +00:00
|
|
|
ajax_template_name = 'part/bom-delete.html'
|
2018-04-15 11:29:24 +00:00
|
|
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Detail view for SupplierPart """
|
2018-04-22 11:54:12 +00:00
|
|
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Update view for editing SupplierPart """
|
|
|
|
|
2018-04-22 11:54:12 +00:00
|
|
|
model = SupplierPart
|
|
|
|
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):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Create view for making new SupplierPart """
|
|
|
|
|
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-22 11:54:12 +00:00
|
|
|
context_object_name = 'part'
|
|
|
|
|
2019-04-28 00:57:32 +00:00
|
|
|
def get_form(self):
|
2019-04-28 13:57:29 +00:00
|
|
|
""" Create Form instance to create a new SupplierPart object.
|
|
|
|
Hide some fields if they are not appropriate in context
|
|
|
|
"""
|
2019-04-28 00:57:32 +00:00
|
|
|
form = super(AjaxCreateView, self).get_form()
|
|
|
|
|
|
|
|
if form.initial.get('supplier', None):
|
|
|
|
# Hide the supplier field
|
2019-04-29 08:35:16 +00:00
|
|
|
form.fields['supplier'].widget = HiddenInput()
|
2019-04-28 00:57:32 +00:00
|
|
|
|
|
|
|
if form.initial.get('part', None):
|
|
|
|
# Hide the part field
|
2019-04-29 08:35:16 +00:00
|
|
|
form.fields['part'].widget = HiddenInput()
|
2019-04-28 00:57:32 +00:00
|
|
|
|
|
|
|
return form
|
2019-04-27 23:53:42 +00:00
|
|
|
|
2018-04-22 11:54:12 +00:00
|
|
|
def get_initial(self):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Provide initial data for new SupplierPart:
|
|
|
|
|
|
|
|
- If 'supplier_id' provided, pre-fill supplier field
|
|
|
|
- If 'part_id' provided, pre-fill part field
|
|
|
|
"""
|
2018-04-22 11:54:12 +00:00
|
|
|
initials = super(SupplierPartCreate, self).get_initial().copy()
|
|
|
|
|
2019-04-27 23:53:42 +00:00
|
|
|
supplier_id = self.get_param('supplier')
|
|
|
|
part_id = self.get_param('part')
|
2018-04-22 11:54:12 +00:00
|
|
|
|
|
|
|
if supplier_id:
|
2019-04-27 23:53:42 +00:00
|
|
|
try:
|
|
|
|
initials['supplier'] = Company.objects.get(pk=supplier_id)
|
|
|
|
except Company.DoesNotExist:
|
|
|
|
initials['supplier'] = None
|
|
|
|
|
2018-04-22 11:54:12 +00:00
|
|
|
if part_id:
|
2019-04-27 23:53:42 +00:00
|
|
|
try:
|
|
|
|
initials['part'] = Part.objects.get(pk=part_id)
|
|
|
|
except Part.DoesNotExist:
|
|
|
|
initials['part'] = None
|
|
|
|
|
2018-04-22 11:54:12 +00:00
|
|
|
return initials
|
|
|
|
|
|
|
|
|
2018-04-27 11:32:48 +00:00
|
|
|
class SupplierPartDelete(AjaxDeleteView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Delete view for removing a SupplierPart """
|
2018-04-22 11:54:12 +00:00
|
|
|
model = SupplierPart
|
|
|
|
success_url = '/supplier/'
|
2019-04-27 23:00:54 +00:00
|
|
|
ajax_template_name = 'company/partdelete.html'
|
2019-05-04 14:46:11 +00:00
|
|
|
ajax_form_title = 'Delete Supplier Part'
|