mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 04:55:44 +00:00
Delete a build output entirely
TODO: Needs to describe in the confirmation dialog what is going to happen!
This commit is contained in:
@ -46,6 +46,29 @@ class EditBuildForm(HelperForm):
|
||||
]
|
||||
|
||||
|
||||
class BuildOutputDeleteForm(HelperForm):
|
||||
"""
|
||||
Form for deleting a build output.
|
||||
"""
|
||||
|
||||
confirm = forms.BooleanField(
|
||||
required=False,
|
||||
help_text=_('Confirm deletion of build output')
|
||||
)
|
||||
|
||||
output_id = forms.IntegerField(
|
||||
required=True,
|
||||
widget=forms.HiddenInput()
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Build
|
||||
fields = [
|
||||
'confirm',
|
||||
'output_id',
|
||||
]
|
||||
|
||||
|
||||
class UnallocateBuildForm(HelperForm):
|
||||
"""
|
||||
Form for auto-de-allocation of stock from a build
|
||||
|
@ -380,6 +380,29 @@ class Build(MPTTModel):
|
||||
# Remove all the allocations
|
||||
allocations.delete()
|
||||
|
||||
def deleteBuildOutput(self, output):
|
||||
"""
|
||||
Remove a build output from the database:
|
||||
|
||||
- Unallocate any build items against the output
|
||||
- Delete the output StockItem
|
||||
"""
|
||||
|
||||
if not output:
|
||||
raise ValidationError(_("No build output specified"))
|
||||
|
||||
if not output.is_building:
|
||||
raise ValidationError(_("Build output is already completed"))
|
||||
|
||||
if not output.build == self:
|
||||
raise ValidationError(_("Build output does not match Build Order"))
|
||||
|
||||
# Unallocate all build items against the output
|
||||
self.unallocateStock(output)
|
||||
|
||||
# Remove the build output from the database
|
||||
output.delete()
|
||||
|
||||
@transaction.atomic
|
||||
def autoAllocate(self):
|
||||
""" Run auto-allocation routine to allocate StockItems to this Build.
|
||||
|
@ -11,6 +11,7 @@ build_detail_urls = [
|
||||
url(r'^allocate/', views.BuildAllocate.as_view(), name='build-allocate'),
|
||||
url(r'^cancel/', views.BuildCancel.as_view(), name='build-cancel'),
|
||||
url(r'^delete/', views.BuildDelete.as_view(), name='build-delete'),
|
||||
url(r'^delete-output/', views.BuildOutputDelete.as_view(), name='build-output-delete'),
|
||||
url(r'^complete/?', views.BuildComplete.as_view(), name='build-complete'),
|
||||
url(r'^auto-allocate/?', views.BuildAutoAllocate.as_view(), name='build-auto-allocate'),
|
||||
url(r'^unallocate/', views.BuildUnallocate.as_view(), name='build-unallocate'),
|
||||
|
@ -141,6 +141,56 @@ class BuildAutoAllocate(AjaxUpdateView):
|
||||
return self.renderJsonResponse(request, form, data, context=self.get_context_data())
|
||||
|
||||
|
||||
class BuildOutputDelete(AjaxUpdateView):
|
||||
"""
|
||||
Delete a build output (StockItem) for a given build.
|
||||
|
||||
Form is a simple confirmation dialog
|
||||
"""
|
||||
|
||||
model = Build
|
||||
form_class = forms.BuildOutputDeleteForm
|
||||
ajax_form_title = _('Delete build output')
|
||||
role_required = 'build.delete'
|
||||
|
||||
def get_initial(self):
|
||||
|
||||
initials = super().get_initial()
|
||||
|
||||
output = self.get_param('output')
|
||||
|
||||
initials['output_id'] = output
|
||||
|
||||
return initials
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
build = self.get_object()
|
||||
form = self.get_form()
|
||||
|
||||
confirm = request.POST.get('confirm', False)
|
||||
|
||||
output_id = request.POST.get('output_id', None)
|
||||
|
||||
try:
|
||||
output = StockItem.objects.get(pk=output_id)
|
||||
except (ValueError, StockItem.DoesNotExist):
|
||||
output = None
|
||||
|
||||
valid = False
|
||||
|
||||
if output:
|
||||
build.deleteBuildOutput(output)
|
||||
valid = True
|
||||
else:
|
||||
form.non_field_errors = [_('Build or output not specified')]
|
||||
|
||||
data = {
|
||||
'form_valid': valid,
|
||||
}
|
||||
|
||||
return self.renderJsonResponse(request, form, data)
|
||||
|
||||
class BuildUnallocate(AjaxUpdateView):
|
||||
""" View to un-allocate all parts from a build.
|
||||
|
||||
@ -151,7 +201,7 @@ class BuildUnallocate(AjaxUpdateView):
|
||||
form_class = forms.UnallocateBuildForm
|
||||
ajax_form_title = _("Unallocate Stock")
|
||||
ajax_template_name = "build/unallocate.html"
|
||||
form_required = 'build.change'
|
||||
role_required = 'build.change'
|
||||
|
||||
def get_initial(self):
|
||||
|
||||
@ -161,13 +211,7 @@ class BuildUnallocate(AjaxUpdateView):
|
||||
output = self.get_param('output')
|
||||
|
||||
if output:
|
||||
try:
|
||||
output = StockItem.objects.get(pk=output)
|
||||
except (ValueError, StockItem.DoesNotExist):
|
||||
output = None
|
||||
|
||||
if output:
|
||||
initials['output_id'] = output.pk
|
||||
initials['output_id'] = output
|
||||
|
||||
return initials
|
||||
|
||||
|
Reference in New Issue
Block a user