mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-19 05:25:42 +00:00
Remove references to get_object_or_404
- Properly handle DoesNotExist errors
This commit is contained in:
@ -11,7 +11,6 @@ from rest_framework import generics, permissions
|
||||
|
||||
from django.db.models import Q
|
||||
from django.conf.urls import url, include
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from .models import Part, PartCategory, BomItem
|
||||
from .models import SupplierPart, SupplierPriceBreak
|
||||
@ -99,20 +98,24 @@ class PartList(generics.ListCreateAPIView):
|
||||
parts_list = Part.objects.all()
|
||||
|
||||
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
|
||||
flt = Q(category=cat_id)
|
||||
|
||||
# Filter by the supplied category
|
||||
flt = Q(category=cat_id)
|
||||
if self.request.query_params.get('include_child_categories', None):
|
||||
childs = category.getUniqueChildren()
|
||||
for child in childs:
|
||||
# Ignore the top-level category (already filtered)
|
||||
if str(child) == str(cat_id):
|
||||
continue
|
||||
flt |= Q(category=child)
|
||||
|
||||
if self.request.query_params.get('include_child_categories', None):
|
||||
childs = category.getUniqueChildren()
|
||||
for child in childs:
|
||||
# Ignore the top-level category (already filtered)
|
||||
if str(child) == str(cat_id):
|
||||
continue
|
||||
flt |= Q(category=child)
|
||||
parts_list = parts_list.filter(flt)
|
||||
|
||||
parts_list = parts_list.filter(flt)
|
||||
except PartCategory.DoesNotExist:
|
||||
pass
|
||||
|
||||
return parts_list
|
||||
|
||||
|
@ -84,7 +84,10 @@ class PartCreate(AjaxCreateView):
|
||||
cat_id = self.get_category_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
|
||||
|
||||
@ -111,7 +114,10 @@ class PartCreate(AjaxCreateView):
|
||||
initials = super(PartCreate, self).get_initial()
|
||||
|
||||
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
|
||||
|
||||
@ -275,7 +281,10 @@ class CategoryEdit(AjaxUpdateView):
|
||||
def get_context_data(self, **kwargs):
|
||||
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
|
||||
|
||||
@ -311,7 +320,10 @@ class CategoryCreate(AjaxCreateView):
|
||||
parent_id = self.request.GET.get('category', None)
|
||||
|
||||
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
|
||||
|
||||
@ -325,7 +337,10 @@ class CategoryCreate(AjaxCreateView):
|
||||
parent_id = self.request.GET.get('category', None)
|
||||
|
||||
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
|
||||
|
||||
@ -357,7 +372,10 @@ class BomItemCreate(AjaxCreateView):
|
||||
parent_id = self.request.GET.get('parent', None)
|
||||
|
||||
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
|
||||
|
||||
|
Reference in New Issue
Block a user