mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Start of exception handler
This commit is contained in:
parent
a139a00843
commit
2e133b7744
@ -79,6 +79,10 @@ TEMPLATES = [
|
||||
},
|
||||
]
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler'
|
||||
}
|
||||
|
||||
WSGI_APPLICATION = 'InvenTree.wsgi.application'
|
||||
|
||||
|
||||
|
17
InvenTree/InvenTree/utils.py
Normal file
17
InvenTree/InvenTree/utils.py
Normal file
@ -0,0 +1,17 @@
|
||||
from rest_framework.views import exception_handler
|
||||
|
||||
|
||||
#class APIValidationError()
|
||||
|
||||
def api_exception_handler(exc, context):
|
||||
response = exception_handler(exc, context)
|
||||
|
||||
#print(response)
|
||||
|
||||
# Now add the HTTP status code to the response.
|
||||
if response is not None:
|
||||
|
||||
data = {'error': response.data}
|
||||
response.data = data
|
||||
|
||||
return response
|
@ -29,7 +29,9 @@ class PartSerializer(serializers.HyperlinkedModelSerializer):
|
||||
'IPN',
|
||||
'description',
|
||||
'category',
|
||||
'stock')
|
||||
'stock',
|
||||
'units',
|
||||
'trackable')
|
||||
|
||||
|
||||
class PartCategorySerializer(serializers.HyperlinkedModelSerializer):
|
||||
|
@ -1,5 +1,5 @@
|
||||
from __future__ import unicode_literals
|
||||
from django.core.exceptions import ValidationError
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.db import models
|
||||
# from django.contrib.auth.models import User
|
||||
@ -8,12 +8,28 @@ from supplier.models import Customer
|
||||
from part.models import Part, PartRevision
|
||||
|
||||
|
||||
class UniquePartManager(models.Manager):
|
||||
|
||||
def create(self, *args, **kwargs):
|
||||
|
||||
print(kwargs)
|
||||
|
||||
part = kwargs.get('part', None)
|
||||
|
||||
if not part.trackable:
|
||||
raise ValidationError("Unique part cannot be created for a non-trackable part")
|
||||
|
||||
return super(UniquePartManager, self).create(*args, **kwargs)
|
||||
|
||||
|
||||
class UniquePart(models.Model):
|
||||
""" A unique instance of a Part object.
|
||||
Used for tracking parts based on serial numbers,
|
||||
and tracking all events in the life of a part
|
||||
"""
|
||||
|
||||
objects = UniquePartManager()
|
||||
|
||||
class Meta:
|
||||
# Cannot have multiple parts with same serial number
|
||||
unique_together = ('part', 'serial')
|
||||
|
Loading…
Reference in New Issue
Block a user