Permalink
Browse files

Moved exceptions into own module and created common superclass.

This makes it easy to catch any "adaptor" error when running an import.
  • Loading branch information...
1 parent 95004af commit e1b24376bae0b32385adfbf2412f26e46bcb8d9b @codeinthehole codeinthehole committed Jun 28, 2012
Showing with 44 additions and 37 deletions.
  1. +25 −0 adaptor/exceptions.py
  2. +15 −35 adaptor/fields.py
  3. +2 −1 adaptor/model.py
  4. +2 −1 tests/xml_tests.py
View
@@ -0,0 +1,25 @@
+class AdaptorError(Exception):
+ pass
+
+
+class FieldError(AdaptorError, ValueError):
+ pass
+
+
+class ForeignKeyFieldError(FieldError):
+ def __init__(self, msg, model, value):
+ self.model = model
+ self.value = value
+ self.msg = msg
+ super(ForeignKeyFieldError, self).__init__(self.msg)
+
+
+class FieldValueMissing(FieldError):
+ def __init__(self, field_name):
+ super(FieldValueMissing, self).__init__("No value found for field %s" % field_name)
+
+
+class ChoiceError(AdaptorError, ValueError):
+ pass
+
+
View
@@ -3,27 +3,7 @@
from django.db.models import Model as djangoModel
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
-
-
-class FieldError(ValueError):
- pass
-
-
-class ForeignKeyFieldError(FieldError):
- def __init__(self, msg, model, value):
- self.model = model
- self.value = value
- self.msg = msg
- super(ForeignKeyFieldError, self).__init__(self.msg)
-
-
-class FieldValueMissing(FieldError):
- def __init__(self, field_name):
- super(FieldValueMissing, self).__init__("No value found for field %s" % field_name)
-
-
-class ChoiceError(ValueError):
- pass
+from adaptor import exceptions
class AllChoices(object):
@@ -43,7 +23,7 @@ def __init__(self, **kwargs):
self.null = kwargs.pop("null", False)
self.default = kwargs.pop("default", None)
if self.default and not self.null:
- raise FieldError("You cannot provide a default without setting the field as nullable")
+ raise exceptions.FieldError("You cannot provide a default without setting the field as nullable")
if 'row_num' in kwargs:
self.position = kwargs.pop('row_num')
else:
@@ -71,20 +51,20 @@ def get_prep_value(self, value, instance=None):
value = self.to_python(value)
if value not in self.choices:
if not self.null:
- raise ChoiceError("Value \'%s\' does not belong to %s" % (value, self.choices))
+ raise exceptions.ChoiceError("Value \'%s\' does not belong to %s" % (value, self.choices))
value = None
transform_method = "transform_" + getattr(self, "fieldname", self.field_name)
# Look for transform_<field_name>, else look for the transform parameter, else identity method
transform = getattr(instance, transform_method, self.transform)
value = transform(value)
if not self.validator().validate(value):
- raise FieldError(self.validator.validation_message)
+ raise exceptions.FieldError(self.validator.validation_message)
return value
- except ChoiceError, e:
+ except exceptions.ChoiceError:
raise
- except FieldError, e:
+ except exceptions.FieldError:
raise
- except ValueError, e:
+ except ValueError:
raise ValueError("Value \'%s\' in columns %d does not match the expected type %s" %
(value, self.position + 1, self.__class__.field_name))
@@ -157,25 +137,25 @@ def __init__(self, *args, **kwargs):
try:
if not issubclass(self.model, djangoModel):
raise TypeError("The first argument should be a django model class.")
- except TypeError, e:
+ except TypeError:
raise TypeError("The first argument should be a django model class.")
super(DjangoModelField, self).__init__(**kwargs)
def to_python(self, value):
try:
return self.model.objects.get(**{self.pk: value})
- except ObjectDoesNotExist, e:
- raise ForeignKeyFieldError("No match found for %s" % self.model.__name__, self.model.__name__, value)
- except MultipleObjectsReturned, e:
- raise ForeignKeyFieldError("Multiple match found for %s" % self.model.__name__, self.model.__name__, value)
+ except ObjectDoesNotExist:
+ raise exceptions.ForeignKeyFieldError("No match found for %s" % self.model.__name__, self.model.__name__, value)
+ except MultipleObjectsReturned:
+ raise exceptions.ForeignKeyFieldError("Multiple match found for %s" % self.model.__name__, self.model.__name__, value)
class ComposedKeyField(DjangoModelField):
def to_python(self, value):
try:
return self.model.objects.get(**value)
- except ObjectDoesNotExist, e:
- raise ForeignKeyFieldError("No match found for %s" % self.model.__name__, self.model.__name__, value)
+ except ObjectDoesNotExist:
+ raise exceptions.ForeignKeyFieldError("No match found for %s" % self.model.__name__, self.model.__name__, value)
class XMLField(Field):
@@ -264,7 +244,7 @@ def __init__(self, *args, **kwargs):
def get_prep_value(self, value, instance=None):
try:
return super(XMLDjangoModelField, self).get_prep_value(value, instance=instance)
- except ForeignKeyFieldError, e:
+ except exceptions.ForeignKeyFieldError, e:
if self.nomatch:
return None
else:
View
@@ -5,7 +5,8 @@
import csv
from django.db.models.base import Model
-from fields import Field, ForeignKeyFieldError, IgnoredField, ComposedKeyField, XMLRootField, FieldValueMissing
+from adaptor.fields import Field, IgnoredField, ComposedKeyField, XMLRootField
+from adaptor.exceptions import ForeignKeyFieldError, FieldValueMissing
class ImproperlyConfigured(Exception):
View
@@ -1,6 +1,7 @@
from django.test import TestCase
from adaptor.fields import *
from adaptor.model import XMLModel
+from adaptor import exceptions
from tests.test_app.models import *
@@ -74,7 +75,7 @@ class TestXMLModel(XMLModel):
<name>Gigi</name>
</person>
</data>"""
- self.assertRaises(FieldValueMissing, TestXMLModel.import_data, xmldata)
+ self.assertRaises(exceptions.FieldValueMissing, TestXMLModel.import_data, xmldata)
def test_null_value(self):

0 comments on commit e1b2437

Please sign in to comment.