2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 03:00:54 +00:00

More refactoring

This commit is contained in:
Oliver Walters
2020-10-30 16:54:05 +11:00
parent c533f59405
commit e049ca1a85
8 changed files with 188 additions and 184 deletions

View File

@ -1126,6 +1126,20 @@ class StockItem(MPTTModel):
return s
@transaction.atomic
def clear_test_results(self, **kwargs):
"""
Remove all test results
"""
# All test results
results = self.test_results.all()
# TODO - Perhaps some filtering options supplied by kwargs?
results.delete()
def getTestResults(self, test=None, result=None, user=None):
"""
Return all test results associated with this StockItem.

View File

@ -245,32 +245,28 @@ class StockItemAssignToCustomer(AjaxUpdateView):
form_class = StockForms.AssignStockItemToCustomerForm
role_required = 'stock.change'
def post(self, request, *args, **kwargs):
def validate(self, item, form, **kwargs):
customer = request.POST.get('customer', None)
customer = form.cleaned_data.get('customer', None)
if not customer:
form.add_error('customer', _('Customer must be specified'))
def post_save(self, item, form, **kwargs):
"""
Assign the stock item to the customer.
"""
customer = form.cleaned_data.get('customer', None)
if customer:
try:
customer = Company.objects.get(pk=customer)
except (ValueError, Company.DoesNotExist):
customer = None
if customer is not None:
stock_item = self.get_object()
item = stock_item.allocateToCustomer(
item = item.allocateToCustomer(
customer,
user=request.user
user=self.request.user
)
item.clearAllocations()
data = {
'form_valid': True,
}
return self.renderJsonResponse(request, self.get_form(), data)
class StockItemReturnToStock(AjaxUpdateView):
"""
@ -283,30 +279,25 @@ class StockItemReturnToStock(AjaxUpdateView):
form_class = StockForms.ReturnStockItemForm
role_required = 'stock.change'
def post(self, request, *args, **kwargs):
def validate(self, item, form, **kwargs):
location = request.POST.get('location', None)
location = form.cleaned_data.get('location', None)
if not location:
form.add_error('location', _('Specify a valid location'))
def post_save(self, item, form, **kwargs):
location = form.cleaned_data.get('location', None)
if location:
try:
location = StockLocation.objects.get(pk=location)
except (ValueError, StockLocation.DoesNotExist):
location = None
item.returnFromCustomer(location, self.request.user)
if location:
stock_item = self.get_object()
stock_item.returnFromCustomer(location, request.user)
else:
raise ValidationError({'location': _("Specify a valid location")})
data = {
'form_valid': True,
'success': _("Stock item returned from customer")
def get_data(self):
return {
'success': _('Stock item returned from customer')
}
return self.renderJsonResponse(request, self.get_form(), data)
class StockItemSelectLabels(AjaxView):
"""