Permalink
Browse files

Added SearchURLValue.

  • Loading branch information...
1 parent 7ddaf15 commit f087cea84d964332e846395afed1b8a2f750934b @jezdez jezdez committed Jan 16, 2014
Showing with 73 additions and 28 deletions.
  1. +24 −27 configurations/values.py
  2. +6 −0 docs/changes.rst
  3. +30 −0 docs/values.rst
  4. +12 −1 tests/test_values.py
  5. +1 −0 tox.ini
View
51 configurations/values.py
@@ -296,26 +296,6 @@ def setup(self, name):
return value
-class DatabaseURLValue(CastingMixin, Value):
- caster = 'dj_database_url.parse'
- message = 'Cannot interpret database URL value {0!r}'
-
- def __init__(self, *args, **kwargs):
- self.alias = kwargs.pop('alias', 'default')
- kwargs.setdefault('environ', True)
- kwargs.setdefault('environ_prefix', None)
- kwargs.setdefault('environ_name', 'DATABASE_URL')
- super(DatabaseURLValue, self).__init__(*args, **kwargs)
- if self.default is None:
- self.default = {}
- else:
- self.default = self.to_python(self.default)
-
- def to_python(self, value):
- value = super(DatabaseURLValue, self).to_python(value)
- return {self.alias: value}
-
-
class EmailURLValue(CastingMixin, MultipleMixin, Value):
caster = 'dj_email_url.parse'
message = 'Cannot interpret email URL value {0!r}'
@@ -331,21 +311,38 @@ def __init__(self, *args, **kwargs):
self.default = self.to_python(self.default)
-class CacheURLValue(CastingMixin, Value):
- caster = 'django_cache_url.parse'
- message = 'Cannot interpret cache URL value {0!r}'
+class DictBackendMixin(Value):
+ default_alias = 'default'
def __init__(self, *args, **kwargs):
- self.alias = kwargs.pop('alias', 'default')
+ self.alias = kwargs.pop('alias', self.default_alias)
kwargs.setdefault('environ', True)
kwargs.setdefault('environ_prefix', None)
- kwargs.setdefault('environ_name', 'CACHE_URL')
- super(CacheURLValue, self).__init__(*args, **kwargs)
+ kwargs.setdefault('environ_name', self.environ_name)
+ super(DictBackendMixin, self).__init__(*args, **kwargs)
if self.default is None:
self.default = {}
else:
self.default = self.to_python(self.default)
def to_python(self, value):
- value = super(CacheURLValue, self).to_python(value)
+ value = super(DictBackendMixin, self).to_python(value)
return {self.alias: value}
+
+
+class DatabaseURLValue(DictBackendMixin, CastingMixin, Value):
+ caster = 'dj_database_url.parse'
+ message = 'Cannot interpret database URL value {0!r}'
+ environ_name = 'DATABASE_URL'
+
+
+class CacheURLValue(DictBackendMixin, CastingMixin, Value):
+ caster = 'django_cache_url.parse'
+ message = 'Cannot interpret cache URL value {0!r}'
+ environ_name = 'CACHE_URL'
+
+
+class SearchURLValue(DictBackendMixin, CastingMixin, Value):
+ caster = 'dj_search_url.parse'
+ message = 'Cannot interpret Search URL value {0!r}'
+ environ_name = 'SEARCH_URL'
View
6 docs/changes.rst
@@ -3,6 +3,12 @@
Changelog
---------
+v0.8 (2014-01-16)
+^^^^^^^^^^^^^^^^^
+
+- Added ``SearchURLValue`` to configure Haystack ``HAYSTACK_CONNECTIONS``
+ settings.
+
v0.7 (2013-11-26)
^^^^^^^^^^^^^^^^^
View
30 docs/values.rst
@@ -446,6 +446,36 @@ URL-based values
.. _`dj-email-url`: https://pypi.python.org/pypi/dj-email-url/
+.. class:: SearchURLValue(default, [environ=True, environ_name='EMAIL_URL', environ_prefix=None])
+
+ .. versionadded:: 0.8
+
+ A :class:`~Value` subclass that uses the `dj-search-url`_ app to
+ convert a search configuration value stored in the ``SEARCH_URL``
+ environment variable into the appropriate settings for use with Haystack_.
+ It's inspired by the `Twelve-Factor methodology`_.
+
+ By default this :class:`~Value` subclass looks for the ``SEARCH_URL``
+ environment variable.
+
+ Takes an optional ``alias`` parameter to define which search backend alias
+ to use for the ``HAYSTACK_CONNECTIONS`` setting.
+
+ :param alias: which cache alias to use
+
+ The other parameters have the following default values:
+
+ :param environ: ``True``
+ :param environ_name: ``SEARCH_URL``
+ :param environ_prefix: ``None``
+
+ ::
+
+ HAYSTACK_CONNECTIONS = values.SearchURLValue('elasticsearch://127.0.0.1:9200/my-index')
+
+ .. _`dj-search-url`: https://pypi.python.org/pypi/dj-search-url/
+ .. _Haystack: http://haystacksearch.org/
+
Other values
^^^^^^^^^^^^
View
13 tests/test_values.py
@@ -14,7 +14,7 @@
RegexValue, PathValue, SecretValue,
DatabaseURLValue, EmailURLValue,
CacheURLValue, BackendsValue,
- CastingMixin)
+ CastingMixin, SearchURLValue)
@contextmanager
@@ -295,6 +295,17 @@ def test_cache_url_value(self):
with env(CACHE_URL='wrong://user@host:port/1'):
self.assertRaises(KeyError, value.setup, 'TEST')
+ def test_search_url_value(self):
+ value = SearchURLValue()
+ self.assertEqual(value.default, {})
+ with env(SEARCH_URL='elasticsearch://127.0.0.1:9200/index'):
+ self.assertEqual(value.setup('SEARCH_URL'), {
+ 'default': {
+ 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
+ 'URL': 'http://127.0.0.1:9200',
+ 'INDEX_NAME': 'index',
+ }})
+
def test_backend_list_value(self):
backends = ['django.middleware.common.CommonMiddleware']
value = BackendsValue(backends)
View
1 tox.ini
@@ -8,6 +8,7 @@ deps =
mock
dj-database-url
dj-email-url
+ dj-search-url
django-cache-url>=0.6.0
six
deps14 =

0 comments on commit f087cea

Please sign in to comment.