2019-04-27 10:35:14 +00:00
|
|
|
"""
|
2019-04-27 12:18:07 +00:00
|
|
|
Django views for interacting with Build objects
|
2019-04-27 10:35:14 +00:00
|
|
|
"""
|
|
|
|
|
2018-04-16 14:32:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
|
|
|
from django.views.generic import DetailView, ListView
|
2019-04-30 05:48:07 +00:00
|
|
|
from django.forms import HiddenInput
|
2018-04-16 14:32:02 +00:00
|
|
|
|
2018-04-17 13:13:41 +00:00
|
|
|
from part.models import Part
|
2019-04-30 05:35:35 +00:00
|
|
|
from .models import Build, BuildItem
|
|
|
|
from .forms import EditBuildForm, EditBuildItemForm
|
2018-04-16 14:32:02 +00:00
|
|
|
|
2019-04-30 08:51:05 +00:00
|
|
|
from InvenTree.views import AjaxView, AjaxUpdateView, AjaxCreateView, AjaxDeleteView
|
2018-04-17 14:03:42 +00:00
|
|
|
|
2018-04-27 15:16:47 +00:00
|
|
|
|
2018-04-16 14:32:02 +00:00
|
|
|
class BuildIndex(ListView):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" View for displaying list of Builds
|
|
|
|
"""
|
2018-04-16 14:32:02 +00:00
|
|
|
model = Build
|
|
|
|
template_name = 'build/index.html'
|
|
|
|
context_object_name = 'builds'
|
|
|
|
|
2018-04-17 13:13:41 +00:00
|
|
|
def get_queryset(self):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" Return all Build objects (order by date, newest first) """
|
2018-04-17 13:13:41 +00:00
|
|
|
return Build.objects.order_by('status', '-completion_date')
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
|
|
|
context = super(BuildIndex, self).get_context_data(**kwargs).copy()
|
|
|
|
|
2018-04-28 01:43:26 +00:00
|
|
|
"""
|
2018-04-17 13:13:41 +00:00
|
|
|
context['active'] = self.get_queryset().filter(status__in=[Build.PENDING, Build.HOLDING])
|
|
|
|
|
|
|
|
context['complete'] = self.get_queryset().filter(status=Build.COMPLETE)
|
|
|
|
context['cancelled'] = self.get_queryset().filter(status=Build.CANCELLED)
|
2018-04-28 01:43:26 +00:00
|
|
|
"""
|
2018-04-17 13:13:41 +00:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
2018-04-16 14:32:02 +00:00
|
|
|
|
2018-04-29 14:23:44 +00:00
|
|
|
class BuildCancel(AjaxView):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" View to cancel a Build.
|
|
|
|
Provides a cancellation information dialog
|
|
|
|
"""
|
2018-04-29 14:23:44 +00:00
|
|
|
model = Build
|
2019-04-30 10:39:01 +00:00
|
|
|
ajax_template_name = 'build/cancel.html'
|
2018-04-29 14:23:44 +00:00
|
|
|
ajax_form_title = 'Cancel Build'
|
|
|
|
context_object_name = 'build'
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" Handle POST request. Mark the build status as CANCELLED """
|
2018-04-29 14:23:44 +00:00
|
|
|
|
|
|
|
build = get_object_or_404(Build, pk=self.kwargs['pk'])
|
|
|
|
|
2019-04-30 10:39:01 +00:00
|
|
|
build.cancelBuild()
|
2018-04-29 14:23:44 +00:00
|
|
|
|
|
|
|
return self.renderJsonResponse(request, None)
|
|
|
|
|
|
|
|
def get_data(self):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" Provide JSON context data. """
|
2018-04-29 14:23:44 +00:00
|
|
|
return {
|
|
|
|
'info': 'Build was cancelled'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-30 10:39:01 +00:00
|
|
|
class BuildComplete(AjaxView):
|
|
|
|
""" View to mark a build as Complete.
|
|
|
|
|
|
|
|
- Notifies the user of which parts will be removed from stock.
|
|
|
|
- Removes allocated items from stock
|
|
|
|
- Deletes pending BuildItem objects
|
|
|
|
"""
|
|
|
|
|
|
|
|
model = Build
|
|
|
|
ajax_template_name = "build/complete.html"
|
|
|
|
ajax_form_title = "Complete Build"
|
|
|
|
context_object_name = "build"
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
""" Handle POST request. Mark the build as COMPLETE """
|
|
|
|
|
|
|
|
build = get_object_or_404(Build, pk=self.kwargs['pk'])
|
|
|
|
|
|
|
|
build.complete()
|
|
|
|
|
|
|
|
return self.renderJsonResponse(request, None)
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
""" Provide feedback data back to the form """
|
|
|
|
return {
|
|
|
|
'info': 'Build marked as COMPLETE'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-16 14:32:02 +00:00
|
|
|
class BuildDetail(DetailView):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" Detail view of a single Build object. """
|
2018-04-16 14:32:02 +00:00
|
|
|
model = Build
|
|
|
|
template_name = 'build/detail.html'
|
|
|
|
context_object_name = 'build'
|
2018-04-17 13:13:41 +00:00
|
|
|
|
|
|
|
|
2018-04-29 14:23:44 +00:00
|
|
|
class BuildAllocate(DetailView):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" View for allocating parts to a Build """
|
2018-04-29 14:23:44 +00:00
|
|
|
model = Build
|
|
|
|
context_object_name = 'build'
|
|
|
|
template_name = 'build/allocate.html'
|
|
|
|
|
2019-04-30 21:48:46 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
""" Provide extra context information for the Build allocation page """
|
|
|
|
|
|
|
|
context = super(DetailView, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
build = self.get_object()
|
|
|
|
part = build.part
|
|
|
|
bom_items = part.bom_items
|
|
|
|
|
|
|
|
context['part'] = part
|
|
|
|
context['bom_items'] = bom_items
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
2018-04-29 14:23:44 +00:00
|
|
|
|
2018-04-27 15:06:42 +00:00
|
|
|
class BuildCreate(AjaxCreateView):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" View to create a new Build object """
|
2018-04-17 13:13:41 +00:00
|
|
|
model = Build
|
|
|
|
context_object_name = 'build'
|
|
|
|
form_class = EditBuildForm
|
2018-04-27 15:06:42 +00:00
|
|
|
ajax_form_title = 'Start new Build'
|
|
|
|
ajax_template_name = 'modal_form.html'
|
2018-04-17 13:13:41 +00:00
|
|
|
|
|
|
|
def get_initial(self):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" Get initial parameters for Build creation.
|
|
|
|
|
|
|
|
If 'part' is specified in the GET query, initialize the Build with the specified Part
|
|
|
|
"""
|
|
|
|
|
2018-04-17 13:13:41 +00:00
|
|
|
initials = super(BuildCreate, self).get_initial().copy()
|
|
|
|
|
|
|
|
part_id = self.request.GET.get('part', None)
|
|
|
|
|
|
|
|
if part_id:
|
2019-04-28 01:09:19 +00:00
|
|
|
try:
|
|
|
|
initials['part'] = Part.objects.get(pk=part_id)
|
|
|
|
except Part.DoesNotExist:
|
|
|
|
pass
|
2018-04-17 13:13:41 +00:00
|
|
|
|
|
|
|
return initials
|
|
|
|
|
2018-04-29 14:23:44 +00:00
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'success': 'Created new build',
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:13:41 +00:00
|
|
|
|
2018-04-27 15:06:42 +00:00
|
|
|
class BuildUpdate(AjaxUpdateView):
|
2019-04-27 10:35:14 +00:00
|
|
|
""" View for editing a Build object """
|
|
|
|
|
2018-04-17 13:13:41 +00:00
|
|
|
model = Build
|
|
|
|
form_class = EditBuildForm
|
|
|
|
context_object_name = 'build'
|
2018-04-27 15:06:42 +00:00
|
|
|
ajax_form_title = 'Edit Build Details'
|
|
|
|
ajax_template_name = 'modal_form.html'
|
2018-04-29 14:23:44 +00:00
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'info': 'Edited build',
|
|
|
|
}
|
2019-04-30 05:35:35 +00:00
|
|
|
|
|
|
|
|
2019-04-30 08:51:05 +00:00
|
|
|
class BuildItemDelete(AjaxDeleteView):
|
|
|
|
""" View to 'unallocate' a BuildItem.
|
|
|
|
Really we are deleting the BuildItem object from the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
model = BuildItem
|
|
|
|
ajax_template_name = 'build/delete_build_item.html'
|
|
|
|
ajax_form_title = 'Unallocate Stock'
|
|
|
|
context_object_name = 'item'
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'danger': 'Removed parts from build allocation'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-30 05:35:35 +00:00
|
|
|
class BuildItemCreate(AjaxCreateView):
|
|
|
|
""" View for allocating a new part to a build """
|
|
|
|
|
|
|
|
model = BuildItem
|
|
|
|
form_class = EditBuildItemForm
|
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
ajax_form_title = 'Allocate new Part'
|
|
|
|
|
2019-04-30 05:48:07 +00:00
|
|
|
def get_form(self):
|
|
|
|
""" Create Form for making / editing new Part object """
|
|
|
|
|
|
|
|
form = super(AjaxCreateView, self).get_form()
|
|
|
|
|
|
|
|
# If the Build object is specified, hide the input field.
|
|
|
|
# We do not want the users to be able to move a BuildItem to a different build
|
2019-04-30 06:38:09 +00:00
|
|
|
build_id = form['build'].value()
|
|
|
|
|
|
|
|
if build_id is not None:
|
2019-04-30 05:48:07 +00:00
|
|
|
form.fields['build'].widget = HiddenInput()
|
|
|
|
|
|
|
|
# If the sub_part is supplied, limit to matching stock items
|
|
|
|
part_id = self.get_param('part')
|
|
|
|
|
|
|
|
if part_id:
|
|
|
|
try:
|
2019-04-30 06:38:09 +00:00
|
|
|
Part.objects.get(pk=part_id)
|
|
|
|
|
2019-04-30 05:48:07 +00:00
|
|
|
query = form.fields['stock_item'].queryset
|
2019-04-30 06:38:09 +00:00
|
|
|
|
|
|
|
# Only allow StockItem objects which match the current part
|
2019-04-30 05:48:07 +00:00
|
|
|
query = query.filter(part=part_id)
|
2019-04-30 06:38:09 +00:00
|
|
|
|
|
|
|
if build_id is not None:
|
|
|
|
# Exclude StockItem objects which are already allocated to this build and part
|
|
|
|
query = query.exclude(id__in=[item.stock_item.id for item in BuildItem.objects.filter(build=build_id, stock_item__part=part_id)])
|
|
|
|
|
2019-04-30 05:48:07 +00:00
|
|
|
form.fields['stock_item'].queryset = query
|
|
|
|
except Part.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return form
|
|
|
|
|
2019-04-30 05:35:35 +00:00
|
|
|
def get_initial(self):
|
|
|
|
""" Provide initial data for BomItem. Look for the folllowing in the GET data:
|
|
|
|
|
|
|
|
- build: pk of the Build object
|
|
|
|
"""
|
|
|
|
|
|
|
|
initials = super(AjaxCreateView, self).get_initial().copy()
|
|
|
|
|
|
|
|
build_id = self.get_param('build')
|
|
|
|
|
|
|
|
if build_id:
|
|
|
|
try:
|
|
|
|
initials['build'] = Build.objects.get(pk=build_id)
|
|
|
|
except Build.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2019-04-30 08:51:05 +00:00
|
|
|
return initials
|
|
|
|
|
|
|
|
|
|
|
|
class BuildItemEdit(AjaxUpdateView):
|
|
|
|
""" View to edit a BuildItem object """
|
|
|
|
|
|
|
|
model = BuildItem
|
|
|
|
ajax_template_name = 'modal_form.html'
|
|
|
|
form_class = EditBuildItemForm
|
|
|
|
ajax_form_title = 'Edit Stock Allocation'
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return {
|
|
|
|
'info': 'Updated Build Item',
|
2019-04-30 09:17:54 +00:00
|
|
|
}
|