InvenTree/InvenTree/build/views.py

265 lines
7.2 KiB
Python
Raw Normal View History

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
from django.forms import HiddenInput
2018-04-16 14:32:02 +00:00
from part.models import Part
from .models import Build, BuildItem
from .forms import EditBuildForm, EditBuildItemForm
2018-04-16 14:32:02 +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'
def get_queryset(self):
2019-04-27 10:35:14 +00:00
""" Return all Build objects (order by date, newest first) """
return Build.objects.order_by('status', '-completion_date')
def get_context_data(self, **kwargs):
context = super(BuildIndex, self).get_context_data(**kwargs).copy()
"""
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)
"""
return context
2018-04-16 14:32:02 +00:00
class BuildCancel(AjaxView):
2019-04-27 10:35:14 +00:00
""" View to cancel a Build.
Provides a cancellation information dialog
"""
model = Build
ajax_template_name = 'build/cancel.html'
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 """
build = get_object_or_404(Build, pk=self.kwargs['pk'])
build.cancelBuild()
return self.renderJsonResponse(request, None)
def get_data(self):
2019-04-27 10:35:14 +00:00
""" Provide JSON context data. """
return {
'info': 'Build was cancelled'
}
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'
class BuildAllocate(DetailView):
2019-04-27 10:35:14 +00:00
""" View for allocating parts to a Build """
model = Build
context_object_name = 'build'
template_name = 'build/allocate.html'
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-27 15:06:42 +00:00
class BuildCreate(AjaxCreateView):
2019-04-27 10:35:14 +00:00
""" View to create a new Build object """
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'
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
"""
initials = super(BuildCreate, self).get_initial().copy()
part_id = self.request.GET.get('part', None)
if part_id:
try:
initials['part'] = Part.objects.get(pk=part_id)
except Part.DoesNotExist:
pass
return initials
def get_data(self):
return {
'success': 'Created new build',
}
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 """
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'
def get_data(self):
return {
'info': 'Edited build',
}
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'
}
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'
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
build_id = form['build'].value()
if build_id is not None:
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:
Part.objects.get(pk=part_id)
query = form.fields['stock_item'].queryset
# Only allow StockItem objects which match the current part
query = query.filter(part=part_id)
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)])
form.fields['stock_item'].queryset = query
except Part.DoesNotExist:
pass
return form
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
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
}