Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added RequestContext instance to render_plugin call. Many plugins exp…

…ect to have access to the request
  • Loading branch information...
commit 4d8a443eb8a96ffa1ea1e80771db7109fd042153 1 parent b892977
@piquadrat piquadrat authored
Showing with 51 additions and 1 deletion.
  1. +11 −1 cms_search/search_indexes.py
  2. +40 −0 cms_search/utils.py
View
12 cms_search/search_indexes.py
@@ -1,3 +1,4 @@
+from django.template import RequestContext
import re
from django.conf import settings
@@ -5,6 +6,11 @@
from django.utils.encoding import force_unicode
from django.utils.translation import get_language, activate
+try:
+ from django.test.client import RequestFactoryII
+except ImportError:
+ from cms_search.utils import RequestFactory
+
def _strip_tags(value):
"""
Returns the given HTML with all tags stripped.
@@ -38,6 +44,8 @@ def _get_index_base():
raise ImproperlyConfigured('CMS_SEARCH_INDEX_BASE_CLASS: %s is not a subclass of haystack.indexes.SearchIndex' % search_settings.INDEX_BASE_CLASS)
return base_class
+rf = RequestFactory()
+
def page_index_factory(language_code, proxy_model):
class _PageIndex(_get_index_base()):
@@ -54,6 +62,8 @@ def prepare(self, obj):
current_languge = get_language()
try:
activate(self.language)
+ request = rf.get("/")
+ request.session = {}
self.prepared_data = super(_PageIndex, self).prepare(obj)
plugins = CMSPlugin.objects.filter(language=language_code, placeholder__in=obj.placeholders.all())
text = ''
@@ -62,7 +72,7 @@ def prepare(self, obj):
if hasattr(instance, 'search_fields'):
text += u''.join(force_unicode(_strip_tags(getattr(instance, field, ''))) for field in instance.search_fields)
if getattr(instance, 'search_fulltext', False):
- text += _strip_tags(instance.render_plugin())
+ text += _strip_tags(instance.render_plugin(context=RequestContext(request)))
self.prepared_data['text'] = text
return self.prepared_data
finally:
View
40 cms_search/utils.py
@@ -0,0 +1,40 @@
+from django.test import Client
+from django.core.handlers.wsgi import WSGIRequest
+
+class RequestFactory(Client):
+ """
+ From: http://djangosnippets.org/snippets/963/
+
+ Class that lets you create mock Request objects for use in testing.
+
+ Usage:
+
+ rf = RequestFactory()
+ get_request = rf.get('/hello/')
+ post_request = rf.post('/submit/', {'foo': 'bar'})
+
+ This class re-uses the django.test.client.Client interface, docs here:
+ http://www.djangoproject.com/documentation/testing/#the-test-client
+
+ Once you have a request object you can pass it to any view function,
+ just as if that view had been hooked up using a URLconf.
+
+ """
+ def request(self, **request):
+ """
+ Similar to parent class, but returns the request object as soon as it
+ has created it.
+ """
+ environ = {
+ 'HTTP_COOKIE': self.cookies,
+ 'PATH_INFO': '/',
+ 'QUERY_STRING': '',
+ 'REQUEST_METHOD': 'GET',
+ 'SCRIPT_NAME': '',
+ 'SERVER_NAME': 'testserver',
+ 'SERVER_PORT': 80,
+ 'SERVER_PROTOCOL': 'HTTP/1.1',
+ }
+ environ.update(self.defaults)
+ environ.update(request)
+ return WSGIRequest(environ)
Please sign in to comment.
Something went wrong with that request. Please try again.