mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Remove references to get_object_or_404
- Properly handle DoesNotExist errors
This commit is contained in:
parent
624c5094c5
commit
a9fbbc3a37
@ -198,7 +198,6 @@ class AjaxUpdateView(AjaxMixin, UpdateView):
|
|||||||
super(UpdateView, self).get(request, *args, **kwargs)
|
super(UpdateView, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
form = self.get_form()
|
form = self.get_form()
|
||||||
# form = self.form_class(instance=self.get_object())
|
|
||||||
|
|
||||||
return self.renderJsonResponse(request, form)
|
return self.renderJsonResponse(request, form)
|
||||||
|
|
||||||
|
@ -101,7 +101,10 @@ class BuildCreate(AjaxCreateView):
|
|||||||
part_id = self.request.GET.get('part', None)
|
part_id = self.request.GET.get('part', None)
|
||||||
|
|
||||||
if part_id:
|
if part_id:
|
||||||
initials['part'] = get_object_or_404(Part, pk=part_id)
|
try:
|
||||||
|
initials['part'] = Part.objects.get(pk=part_id)
|
||||||
|
except Part.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return initials
|
return initials
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ from rest_framework import generics, permissions
|
|||||||
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
from django.shortcuts import get_object_or_404
|
|
||||||
|
|
||||||
from .models import Part, PartCategory, BomItem
|
from .models import Part, PartCategory, BomItem
|
||||||
from .models import SupplierPart, SupplierPriceBreak
|
from .models import SupplierPart, SupplierPriceBreak
|
||||||
@ -99,7 +98,8 @@ class PartList(generics.ListCreateAPIView):
|
|||||||
parts_list = Part.objects.all()
|
parts_list = Part.objects.all()
|
||||||
|
|
||||||
if cat_id:
|
if cat_id:
|
||||||
category = get_object_or_404(PartCategory, pk=cat_id)
|
try:
|
||||||
|
category = PartCategory.objects.get(pk=cat_id)
|
||||||
|
|
||||||
# Filter by the supplied category
|
# Filter by the supplied category
|
||||||
flt = Q(category=cat_id)
|
flt = Q(category=cat_id)
|
||||||
@ -114,6 +114,9 @@ class PartList(generics.ListCreateAPIView):
|
|||||||
|
|
||||||
parts_list = parts_list.filter(flt)
|
parts_list = parts_list.filter(flt)
|
||||||
|
|
||||||
|
except PartCategory.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return parts_list
|
return parts_list
|
||||||
|
|
||||||
permission_classes = [
|
permission_classes = [
|
||||||
|
@ -84,7 +84,10 @@ class PartCreate(AjaxCreateView):
|
|||||||
cat_id = self.get_category_id()
|
cat_id = self.get_category_id()
|
||||||
|
|
||||||
if cat_id:
|
if cat_id:
|
||||||
context['category'] = get_object_or_404(PartCategory, pk=cat_id)
|
try:
|
||||||
|
context['category'] = PartCategory.objects.get(pk=cat_id)
|
||||||
|
except PartCategory.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
@ -111,7 +114,10 @@ class PartCreate(AjaxCreateView):
|
|||||||
initials = super(PartCreate, self).get_initial()
|
initials = super(PartCreate, self).get_initial()
|
||||||
|
|
||||||
if self.get_category_id():
|
if self.get_category_id():
|
||||||
initials['category'] = get_object_or_404(PartCategory, pk=self.get_category_id())
|
try:
|
||||||
|
initials['category'] = PartCategory.objects.get(pk=self.get_category_id())
|
||||||
|
except PartCategory.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return initials
|
return initials
|
||||||
|
|
||||||
@ -275,7 +281,10 @@ class CategoryEdit(AjaxUpdateView):
|
|||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(CategoryEdit, self).get_context_data(**kwargs).copy()
|
context = super(CategoryEdit, self).get_context_data(**kwargs).copy()
|
||||||
|
|
||||||
context['category'] = get_object_or_404(PartCategory, pk=self.kwargs['pk'])
|
try:
|
||||||
|
context['category'] = PartCategory.objects.get(pk=self.kwargs['pk'])
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
@ -311,7 +320,10 @@ class CategoryCreate(AjaxCreateView):
|
|||||||
parent_id = self.request.GET.get('category', None)
|
parent_id = self.request.GET.get('category', None)
|
||||||
|
|
||||||
if parent_id:
|
if parent_id:
|
||||||
context['category'] = get_object_or_404(PartCategory, pk=parent_id)
|
try:
|
||||||
|
context['category'] = PartCategory.objects.get(pk=parent_id)
|
||||||
|
except PartCategory.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
@ -325,7 +337,10 @@ class CategoryCreate(AjaxCreateView):
|
|||||||
parent_id = self.request.GET.get('category', None)
|
parent_id = self.request.GET.get('category', None)
|
||||||
|
|
||||||
if parent_id:
|
if parent_id:
|
||||||
initials['parent'] = get_object_or_404(PartCategory, pk=parent_id)
|
try:
|
||||||
|
initials['parent'] = PartCategory.objects.get(pk=parent_id)
|
||||||
|
except PartCategory.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return initials
|
return initials
|
||||||
|
|
||||||
@ -357,7 +372,10 @@ class BomItemCreate(AjaxCreateView):
|
|||||||
parent_id = self.request.GET.get('parent', None)
|
parent_id = self.request.GET.get('parent', None)
|
||||||
|
|
||||||
if parent_id:
|
if parent_id:
|
||||||
initials['part'] = get_object_or_404(Part, pk=parent_id)
|
try:
|
||||||
|
initials['part'] = Part.objects.get(pk=parent_id)
|
||||||
|
except Part.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return initials
|
return initials
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ from django_filters import NumberFilter
|
|||||||
|
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.shortcuts import get_object_or_404
|
|
||||||
|
|
||||||
from .models import StockLocation, StockItem
|
from .models import StockLocation, StockItem
|
||||||
from .models import StockItemTracking
|
from .models import StockItemTracking
|
||||||
@ -238,7 +237,8 @@ class StockList(generics.ListCreateAPIView):
|
|||||||
stock_list = StockItem.objects.all()
|
stock_list = StockItem.objects.all()
|
||||||
|
|
||||||
if loc_id:
|
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
|
# Filter by the supplied category
|
||||||
flt = Q(location=loc_id)
|
flt = Q(location=loc_id)
|
||||||
@ -253,6 +253,9 @@ class StockList(generics.ListCreateAPIView):
|
|||||||
|
|
||||||
stock_list = stock_list.filter(flt)
|
stock_list = stock_list.filter(flt)
|
||||||
|
|
||||||
|
except StockLocation.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
return stock_list
|
return stock_list
|
||||||
|
|
||||||
serializer_class = StockItemSerializer
|
serializer_class = StockItemSerializer
|
||||||
|
@ -5,8 +5,6 @@ Django views for interacting with Stock app
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404
|
|
||||||
|
|
||||||
from django.views.generic import DetailView, ListView
|
from django.views.generic import DetailView, ListView
|
||||||
from django.forms.models import model_to_dict
|
from django.forms.models import model_to_dict
|
||||||
|
|
||||||
@ -106,7 +104,10 @@ class StockLocationCreate(AjaxCreateView):
|
|||||||
loc_id = self.request.GET.get('location', None)
|
loc_id = self.request.GET.get('location', None)
|
||||||
|
|
||||||
if loc_id:
|
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
|
return initials
|
||||||
|
|
||||||
@ -126,6 +127,8 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
ajax_form_title = 'Create new Stock Item'
|
ajax_form_title = 'Create new Stock Item'
|
||||||
|
|
||||||
def get_initial(self):
|
def get_initial(self):
|
||||||
|
""" Provide initial data to create a new StockItem object
|
||||||
|
"""
|
||||||
|
|
||||||
# Is the client attempting to copy an existing stock item?
|
# Is the client attempting to copy an existing stock item?
|
||||||
item_to_copy = self.request.GET.get('copy', None)
|
item_to_copy = self.request.GET.get('copy', None)
|
||||||
@ -144,15 +147,22 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
part_id = self.request.GET.get('part', None)
|
part_id = self.request.GET.get('part', None)
|
||||||
loc_id = self.request.GET.get('location', None)
|
loc_id = self.request.GET.get('location', None)
|
||||||
|
|
||||||
|
# Part field has been specified
|
||||||
if part_id:
|
if part_id:
|
||||||
part = get_object_or_404(Part, pk=part_id)
|
try:
|
||||||
if part:
|
part = Part.objects.get(pk=part_id)
|
||||||
initials['part'] = get_object_or_404(Part, pk=part_id)
|
initials['part'] = part
|
||||||
initials['location'] = part.default_location
|
initials['location'] = part.default_location
|
||||||
initials['supplier_part'] = part.default_supplier
|
initials['supplier_part'] = part.default_supplier
|
||||||
|
except Part.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Location has been specified
|
||||||
if loc_id:
|
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
|
return initials
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user