From fa809b4225906f8d1433f4041795e6bfc0869878 Mon Sep 17 00:00:00 2001 From: Tanshu Date: Mon, 17 Dec 2012 00:09:45 +0530 Subject: [PATCH] Updated voucher save/update to catch more exceptions. Updated ValidatinException to return message on str(). --- brewman/brewman/models/validation_exception.py | 4 +++- brewman/brewman/views/services/voucher/purchase.py | 2 ++ brewman/brewman/views/services/voucher/save_voucher.py | 4 ++-- brewman/brewman/views/services/voucher/update_voucher.py | 4 ++-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/brewman/brewman/models/validation_exception.py b/brewman/brewman/models/validation_exception.py index 468e2828..c7c58e5c 100644 --- a/brewman/brewman/models/validation_exception.py +++ b/brewman/brewman/models/validation_exception.py @@ -5,4 +5,6 @@ class ValidationError(Exception): Exception.__init__(self, message) # Now for your custom code... - self.Errors = Errors \ No newline at end of file + self.Errors = Errors + def __str__(self): + return self.message \ No newline at end of file diff --git a/brewman/brewman/views/services/voucher/purchase.py b/brewman/brewman/views/services/voucher/purchase.py index 0e521191..c77670b1 100644 --- a/brewman/brewman/views/services/voucher/purchase.py +++ b/brewman/brewman/views/services/voucher/purchase.py @@ -28,6 +28,8 @@ def purchase_create_inventory(voucher, item, date): if 'Product' not in item or 'ProductID' not in item['Product']: raise ValidationError('No Product in item') product = Product.by_id(uuid.UUID(item['Product']['ProductID'])) + if product is None: + raise ValidationError('No Product in item') inventory_id = uuid.UUID(item['InventoryID']) if 'InventoryID' in item else None quantity = round(Decimal(item['Quantity']), 2) rate = round(Decimal(item['Rate']), 2) diff --git a/brewman/brewman/views/services/voucher/save_voucher.py b/brewman/brewman/views/services/voucher/save_voucher.py index d65d613f..18c3d6cf 100644 --- a/brewman/brewman/views/services/voucher/save_voucher.py +++ b/brewman/brewman/views/services/voucher/save_voucher.py @@ -69,8 +69,8 @@ class save_voucher(object): transaction.commit() session_current_date_set(self.request,self.json['Date']) return voucher_info(Voucher.by_id(voucher.id)) - except ValidationError as ex: + except (ValidationError, ValueError, KeyError) as ex: transaction.abort() - response = Response("Failed validation: {0}".format(ex.message)) + response = Response("Failed validation: {0}".format(str(ex))) response.status_int = 500 return response diff --git a/brewman/brewman/views/services/voucher/update_voucher.py b/brewman/brewman/views/services/voucher/update_voucher.py index fabe940f..a949bc94 100644 --- a/brewman/brewman/views/services/voucher/update_voucher.py +++ b/brewman/brewman/views/services/voucher/update_voucher.py @@ -86,8 +86,8 @@ class update_voucher(object): transaction.commit() session_current_date_set(self.request,self.json['Date']) return voucher_info(Voucher.by_id(voucher.id)) - except ValidationError as ex: + except (ValidationError, ValueError, KeyError) as ex: transaction.abort() - response = Response("Failed validation: {0}".format(ex.message)) + response = Response("Failed validation: {0}".format(str(ex))) response.status_int = 500 return response