Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'release/0.5.2'

  • Loading branch information...
commit 4530842d81e3cfebe560196426973616f8c0b717 2 parents c7ba99d + 2d783e1
@piquadrat authored
View
2  cms_search/__init__.py
@@ -1 +1 @@
-__version__ = '0.5.1'
+__version__ = '0.5.2'
View
39 cms_search/search_helpers/fields.py
@@ -1,11 +1,20 @@
from django.conf import settings
from haystack import indexes
from django.utils.translation import get_language, activate
+from django.template import loader, Context
+
+try:
+ from django.test.client import RequestFactory
+except ImportError:
+ from cms_search.utils import RequestFactory
+
+rf = RequestFactory()
class MultiLangTemplateField(indexes.CharField):
- def __init__(self, **kwargs):
+ def __init__(self, needs_request=False, **kwargs):
kwargs['use_template'] = True
+ self.needs_request = needs_request
super(MultiLangTemplateField, self).__init__(**kwargs)
def prepare_template(self, obj):
@@ -14,9 +23,31 @@ def prepare_template(self, obj):
try:
for lang, lang_name in settings.LANGUAGES:
activate(lang)
- content.append(super(MultiLangTemplateField, self).prepare_template(obj))
+ content.append(self._prepare_template(obj, needs_request=self.needs_request))
finally:
activate(current_lang)
-
return '\n'.join(content)
-
+
+ def _prepare_template(self, obj, needs_request=False):
+ """
+ This is a copy of CharField.prepare_template, except that it adds a fake
+ request to the context, which is mainly needed to render CMS placeholders
+ """
+ if self.instance_name is None and self.template_name is None:
+ raise SearchFieldError("This field requires either its instance_name variable to be populated or an explicit template_name in order to load the correct template.")
+
+ if self.template_name is not None:
+ template_names = self.template_name
+
+ if not isinstance(template_names, (list, tuple)):
+ template_names = [template_names]
+ else:
+ template_names = ['search/indexes/%s/%s_%s.txt' % (obj._meta.app_label, obj._meta.module_name, self.instance_name)]
+
+ t = loader.select_template(template_names)
+ ctx = {'object': obj}
+ if needs_request:
+ request = rf.get("/")
+ request.session = {}
+ ctx['request'] = request
+ return t.render(Context(ctx))
View
4 docs/conf.py
@@ -46,9 +46,9 @@
# built documents.
#
# The short X.Y version.
-version = '0.5.1'
+version = '0.5.2'
# The full version, including alpha/beta/rc tags.
-release = '0.5.1'
+release = '0.5.2'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
View
8 docs/index.rst
@@ -108,12 +108,18 @@ content.
haystack.autodiscover()
-.. :class:: cms_search.search_helpers.fields.MultiLangTemplateField
+.. py:class:: cms_search.search_helpers.fields.MultiLangTemplateField
A :class:`haystack.indexes.CharField` subclass that renders the search
template in all languages defined in :setting:`django:LANGUAGES` and
concatenates the result.
+ .. note::
+
+ If you plan to render django CMS placeholders in the template,
+ make sure to pass the ``needs_request`` argument to
+ :meth:`cms_search.search_helpers.fields.MultiLangTemplateField`.
+
.. templatetag:: get_translated_value
``{% get_translated_value %}`` template tag
Please sign in to comment.
Something went wrong with that request. Please try again.