Start of exception handler

This commit is contained in:
Oliver Walters 2017-04-16 00:58:40 +10:00
parent a139a00843
commit 2e133b7744
4 changed files with 41 additions and 2 deletions

View File

@ -79,6 +79,10 @@ TEMPLATES = [
},
]
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler'
}
WSGI_APPLICATION = 'InvenTree.wsgi.application'

View 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

View File

@ -29,7 +29,9 @@ class PartSerializer(serializers.HyperlinkedModelSerializer):
'IPN',
'description',
'category',
'stock')
'stock',
'units',
'trackable')
class PartCategorySerializer(serializers.HyperlinkedModelSerializer):

View File

@ -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')