diff --git a/sqlalchemy_utils/i18n.py b/sqlalchemy_utils/i18n.py index 911326df..cad93a04 100644 --- a/sqlalchemy_utils/i18n.py +++ b/sqlalchemy_utils/i18n.py @@ -14,13 +14,15 @@ except ImportError: babel = None + try: - from flask_babel import get_locale -except ImportError: + def get_locale(): return babel.Locale('en') +except AttributeError: + # As babel is optional, we may raise an AttributeError accessing it def get_locale(): raise ImproperlyConfigured( - 'Could not load get_locale function from Flask-Babel. Either ' - 'install Flask-Babel or make a similar function and override it ' + 'Could not load get_locale function using Babel. Either ' + 'install Babel or make a similar function and override it ' 'in this module.' ) diff --git a/sqlalchemy_utils/primitives/country.py b/sqlalchemy_utils/primitives/country.py index 18f5ede8..72265425 100644 --- a/sqlalchemy_utils/primitives/country.py +++ b/sqlalchemy_utils/primitives/country.py @@ -25,7 +25,8 @@ class Country(object): Country(Country('FI')).code # 'FI' - Country always validates the given code. + Country always validates the given code if you use at least the optional + dependency list 'babel', otherwise no validation are performed. :: @@ -76,6 +77,9 @@ def validate(self, code): raise ValueError( 'Could not convert string to country code: {0}'.format(code) ) + except AttributeError: + # As babel is optional, we may raise an AttributeError accessing it + pass def __eq__(self, other): if isinstance(other, Country): diff --git a/sqlalchemy_utils/primitives/currency.py b/sqlalchemy_utils/primitives/currency.py index 37bd992b..e16d71a3 100644 --- a/sqlalchemy_utils/primitives/currency.py +++ b/sqlalchemy_utils/primitives/currency.py @@ -26,7 +26,8 @@ class Currency(object): Currency(Currency('USD')).code # 'USD' - Currency always validates the given code. + Currency always validates the given code if you use at least the optional + dependency list 'babel', otherwise no validation are performed. :: @@ -75,6 +76,9 @@ def validate(self, code): i18n.babel.Locale('en').currencies[code] except KeyError: raise ValueError("'{0}' is not valid currency code.".format(code)) + except AttributeError: + # As babel is optional, we may raise an AttributeError accessing it + pass @property def symbol(self):