From 9a12e9415ab49c8cd80675c43de840076eca6d56 Mon Sep 17 00:00:00 2001 From: Antoine Veuiller Date: Mon, 16 Jul 2018 11:58:35 +0200 Subject: [PATCH 1/2] fix(get_locale): set default locale to 'en' without Flask-Babel An exception is still raised if babel is not installed at all. --- sqlalchemy_utils/i18n.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sqlalchemy_utils/i18n.py b/sqlalchemy_utils/i18n.py index 71cafba6..846c5ba7 100644 --- a/sqlalchemy_utils/i18n.py +++ b/sqlalchemy_utils/i18n.py @@ -12,13 +12,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.' ) From c863854287764b218e040578e8c83e47691f65ad Mon Sep 17 00:00:00 2001 From: Antoine Veuiller Date: Mon, 16 Jul 2018 11:58:43 +0200 Subject: [PATCH 2/2] fix(babel): pass validation if babel is not installed --- sqlalchemy_utils/primitives/country.py | 6 +++++- sqlalchemy_utils/primitives/currency.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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):