Skip to content

Commit

Permalink
added a new argument to MultilangTemplateField, to instruct it to add…
Browse files Browse the repository at this point in the history
… a fake request to the template context

This is needed for rendering CMS placeholders
  • Loading branch information
beniwohli committed May 11, 2012
1 parent 2074cc9 commit 5979ff7
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions 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):
Expand All @@ -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))

0 comments on commit 5979ff7

Please sign in to comment.