From 2e133b774472aebf43a12b1c9db1809f5c44cbca Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 16 Apr 2017 00:58:40 +1000 Subject: [PATCH] Start of exception handler --- InvenTree/InvenTree/settings.py | 4 ++++ InvenTree/InvenTree/utils.py | 17 +++++++++++++++++ InvenTree/part/serializers.py | 4 +++- InvenTree/track/models.py | 18 +++++++++++++++++- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 InvenTree/InvenTree/utils.py diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 975af8c20b..5cfdbcf924 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -79,6 +79,10 @@ TEMPLATES = [ }, ] +REST_FRAMEWORK = { + 'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler' +} + WSGI_APPLICATION = 'InvenTree.wsgi.application' diff --git a/InvenTree/InvenTree/utils.py b/InvenTree/InvenTree/utils.py new file mode 100644 index 0000000000..3edf222306 --- /dev/null +++ b/InvenTree/InvenTree/utils.py @@ -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 diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index bc38ca309d..48719e9cd5 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -29,7 +29,9 @@ class PartSerializer(serializers.HyperlinkedModelSerializer): 'IPN', 'description', 'category', - 'stock') + 'stock', + 'units', + 'trackable') class PartCategorySerializer(serializers.HyperlinkedModelSerializer): diff --git a/InvenTree/track/models.py b/InvenTree/track/models.py index da2a912df7..ac22092731 100644 --- a/InvenTree/track/models.py +++ b/InvenTree/track/models.py @@ -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')