2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-14 19:15:41 +00:00

Merge pull request #181 from SchrodingersGat/supplier-parts

Supplier parts
This commit is contained in:
Oliver
2019-04-28 11:28:32 +10:00
committed by GitHub
7 changed files with 219 additions and 127 deletions

View File

@ -7,7 +7,6 @@ from django_filters import NumberFilter
from django.conf.urls import url, include
from django.db.models import Q
from django.shortcuts import get_object_or_404
from .models import StockLocation, StockItem
from .models import StockItemTracking
@ -238,20 +237,24 @@ class StockList(generics.ListCreateAPIView):
stock_list = StockItem.objects.all()
if loc_id:
location = get_object_or_404(StockLocation, pk=loc_id)
try:
location = StockLocation.objects.get(pk=loc_id)
# Filter by the supplied category
flt = Q(location=loc_id)
# Filter by the supplied category
flt = Q(location=loc_id)
if self.request.query_params.get('include_child_locations', None):
childs = location.getUniqueChildren()
for child in childs:
# Ignore the top-level category (already filtered!)
if str(child) == str(loc_id):
continue
flt |= Q(location=child)
if self.request.query_params.get('include_child_locations', None):
childs = location.getUniqueChildren()
for child in childs:
# Ignore the top-level category (already filtered!)
if str(child) == str(loc_id):
continue
flt |= Q(location=child)
stock_list = stock_list.filter(flt)
stock_list = stock_list.filter(flt)
except StockLocation.DoesNotExist:
pass
return stock_list

View File

@ -5,8 +5,6 @@ Django views for interacting with Stock app
# -*- 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.models import model_to_dict
@ -106,7 +104,10 @@ class StockLocationCreate(AjaxCreateView):
loc_id = self.request.GET.get('location', None)
if loc_id:
initials['parent'] = get_object_or_404(StockLocation, pk=loc_id)
try:
initials['parent'] = StockLocation.objects.get(pk=loc_id)
except StockLocation.DoesNotExist:
pass
return initials
@ -125,7 +126,30 @@ class StockItemCreate(AjaxCreateView):
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Create new Stock Item'
def get_form(self):
""" Get form for StockItem creation.
Overrides the default get_form() method to intelligently limit
ForeignKey choices based on other selections
"""
form = super(AjaxCreateView, self).get_form()
# If the user has selected a Part, limit choices for SupplierPart
if form['part'].value() is not None:
part = form['part'].value()
parts = form.fields['supplier_part'].queryset
parts = parts.filter(part=part)
form.fields['supplier_part'].queryset = parts
# Otherwise if the user has selected a SupplierPart, we know what Part they meant!
elif form['supplier_part'].value() is not None:
pass
return form
def get_initial(self):
""" Provide initial data to create a new StockItem object
"""
# Is the client attempting to copy an existing stock item?
item_to_copy = self.request.GET.get('copy', None)
@ -144,15 +168,22 @@ class StockItemCreate(AjaxCreateView):
part_id = self.request.GET.get('part', None)
loc_id = self.request.GET.get('location', None)
# Part field has been specified
if part_id:
part = get_object_or_404(Part, pk=part_id)
if part:
initials['part'] = get_object_or_404(Part, pk=part_id)
try:
part = Part.objects.get(pk=part_id)
initials['part'] = part
initials['location'] = part.default_location
initials['supplier_part'] = part.default_supplier
except Part.DoesNotExist:
pass
# Location has been specified
if loc_id:
initials['location'] = get_object_or_404(StockLocation, pk=loc_id)
try:
initials['location'] = StockLocation.objects.get(pk=loc_id)
except StockLocation.DoesNotExist:
pass
return initials
@ -178,7 +209,7 @@ class StockItemDelete(AjaxDeleteView):
model = StockItem
success_url = '/stock/'
template_name = 'stock/item_delete.html'
ajax_template_name = 'stock/item_delete.html'
context_object_name = 'item'
ajax_form_title = 'Delete Stock Item'
@ -190,7 +221,7 @@ class StockItemMove(AjaxUpdateView):
"""
model = StockItem
template_name = 'modal_form.html'
ajax_template_name = 'modal_form.html'
context_object_name = 'item'
ajax_form_title = 'Move Stock Item'
form_class = MoveStockItemForm