Skip to content

Commit

Permalink
Don't crash if Colander is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
amarandon committed Jan 16, 2016
1 parent acd4c33 commit a5f3234
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
34 changes: 25 additions & 9 deletions cornice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
register_resource_views,
)
from cornice.util import ContentTypePredicate
from pyramid.events import BeforeRender, NewRequest
from pyramid.i18n import get_localizer
from pyramid.httpexceptions import HTTPNotFound, HTTPForbidden
from pyramid.security import NO_PERMISSION_REQUIRED
Expand Down Expand Up @@ -55,11 +56,32 @@ def set_localizer_for_languages(event, available_languages,
request.localizer = localizer


def setup_localization(config):
"""
Setup localization based on the available_languages and
pyramid.default_locale_name settings.
These settings are named after suggestions from the "Internationalization
and Localization" section of the Pyramid documentation.
"""
try:
config.add_translation_dirs('colander:locale/')
settings = config.get_settings()
available_languages = settings['available_languages'].split()
default_locale_name = settings.get('pyramid.default_locale_name', 'en')
set_localizer = partial(set_localizer_for_languages,
available_languages=available_languages,
default_locale_name=default_locale_name)
config.add_subscriber(set_localizer, NewRequest)
except ImportError:
# add_translation_dirs raises an ImportError if colander is not
# installed
pass


def includeme(config):
"""Include the Cornice definitions
"""
from pyramid.events import BeforeRender, NewRequest

# attributes required to maintain services
config.registry.cornice_services = {}

Expand All @@ -85,10 +107,4 @@ def includeme(config):
permission=NO_PERMISSION_REQUIRED)

if settings.get('available_languages'):
available_languages = settings['available_languages'].split()
default_locale_name = settings.get('pyramid.default_locale_name', 'en')
set_localizer = partial(set_localizer_for_languages,
available_languages=available_languages,
default_locale_name=default_locale_name)
config.add_subscriber(set_localizer, NewRequest)
config.add_translation_dirs('colander:locale/')
setup_localization(config)
4 changes: 4 additions & 0 deletions docs/source/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ before passing the result to Colander.
View-specific deserializers have priority over global content-type
deserializers.

To enable localization of Colander error messages, you must set
`available_languages <http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/i18n.html#detecting-available-languages>`_ in your settings.
You may also set `pyramid.default_locale_name <http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/environment.html#default-locale-name-setting>`_.


Using formencode
~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit a5f3234

Please sign in to comment.