Commit 44dcfdd5 by benjaoming

Fix filter() call in get_content_snippet not working on Python 2.7+

parent e60cae57
...@@ -11,8 +11,14 @@ ...@@ -11,8 +11,14 @@
<form class="form-search directory-toolbar"> <form class="form-search directory-toolbar">
<p class="lead"> <p class="lead">
<div class="pull-right"> <div class="pull-right">
{{ search_form.q }} <div class="input-group">
<button class="btn btn-default"><span class="icon-search"></span></button> <input type="search" class="form-control search-query" name="q" value="{{ search_query }}" />
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<span class="icon-search"></span>
</button>
</span>
</div>
</div> </div>
<p>{% blocktrans with paginator.object_list.count as cnt %}Your search returned <strong>{{ cnt }}</strong> results.{% endblocktrans %}</p> <p>{% blocktrans with paginator.object_list.count as cnt %}Your search returned <strong>{{ cnt }}</strong> results.{% endblocktrans %}</p>
<div class="clearfix"></div> <div class="clearfix"></div>
......
...@@ -21,6 +21,7 @@ from wiki.core.plugins import registry as plugin_registry ...@@ -21,6 +21,7 @@ from wiki.core.plugins import registry as plugin_registry
# called more than once per page in multiple template blocks. # called more than once per page in multiple template blocks.
_cache = {} _cache = {}
@register.assignment_tag(takes_context=True) @register.assignment_tag(takes_context=True)
def article_for_object(context, obj): def article_for_object(context, obj):
if not isinstance(obj, Model): if not isinstance(obj, Model):
...@@ -28,14 +29,16 @@ def article_for_object(context, obj): ...@@ -28,14 +29,16 @@ def article_for_object(context, obj):
"A Wiki article can only be associated to a Django Model " "A Wiki article can only be associated to a Django Model "
"instance, not %s" % type(obj) "instance, not %s" % type(obj)
) )
content_type = ContentType.objects.get_for_model(obj) content_type = ContentType.objects.get_for_model(obj)
# TODO: This is disabled for now, as it should only fire once per request # TODO: This is disabled for now, as it should only fire once per request
# Maybe store cache in the request object? # Maybe store cache in the request object?
if True or not obj in _cache.keys(): if True or not obj in _cache.keys():
try: try:
article = models.ArticleForObject.objects.get(content_type=content_type, object_id=obj.pk).article article = models.ArticleForObject.objects.get(
content_type=content_type,
object_id=obj.pk).article
except models.ArticleForObject.DoesNotExist: except models.ArticleForObject.DoesNotExist:
article = None article = None
_cache[obj] = article _cache[obj] = article
...@@ -44,7 +47,7 @@ def article_for_object(context, obj): ...@@ -44,7 +47,7 @@ def article_for_object(context, obj):
@register.inclusion_tag('wiki/includes/render.html', takes_context=True) @register.inclusion_tag('wiki/includes/render.html', takes_context=True)
def wiki_render(context, article, preview_content=None): def wiki_render(context, article, preview_content=None):
if preview_content: if preview_content:
content = article.render(preview_content=preview_content) content = article.render(preview_content=preview_content)
else: else:
...@@ -63,7 +66,9 @@ def wiki_render(context, article, preview_content=None): ...@@ -63,7 +66,9 @@ def wiki_render(context, article, preview_content=None):
@register.inclusion_tag('wiki/includes/form.html', takes_context=True) @register.inclusion_tag('wiki/includes/form.html', takes_context=True)
def wiki_form(context, form_obj): def wiki_form(context, form_obj):
if not isinstance(form_obj, BaseForm): if not isinstance(form_obj, BaseForm):
raise TypeError("Error including form, it's not a form, it's a %s" % type(form_obj)) raise TypeError(
"Error including form, it's not a form, it's a %s" %
type(form_obj))
context.update({'form': form_obj}) context.update({'form': form_obj})
return context return context
...@@ -71,21 +76,40 @@ def wiki_form(context, form_obj): ...@@ -71,21 +76,40 @@ def wiki_form(context, form_obj):
@register.filter @register.filter
def get_content_snippet(content, keyword, max_words=30): def get_content_snippet(content, keyword, max_words=30):
max_words = int(max_words) max_words = int(max_words)
p = re.compile(r'(?P<before>.*)%s(?P<after>.*)' % re.escape(keyword), re.MULTILINE | re.IGNORECASE | re.DOTALL) p = re.compile(
r'(?P<before>.*)%s(?P<after>.*)' %
re.escape(keyword),
re.MULTILINE | re.IGNORECASE | re.DOTALL)
m = p.search(content) m = p.search(content)
html = "" html = ""
if m: if m:
words = filter(lambda x: x!="", striptags(m.group("before")).replace("\n", " ").split(" ")) words = list(filter(
before_words = words[-max_words/2:] lambda x: x != "",
words = filter(lambda x: x!="", striptags(m.group("after")).replace("\n", " ").split(" ")) striptags(
m.group("before")).replace(
"\n",
" ").split(" ")))
before_words = words[-max_words / 2:]
words = list(filter(
lambda x: x != "",
striptags(
m.group("after")).replace(
"\n",
" ").split(" ")))
after = " ".join(words[:max_words - len(before_words)]) after = " ".join(words[:max_words - len(before_words)])
before = " ".join(before_words) before = " ".join(before_words)
html = "%s %s %s" % (before, striptags(keyword), after) html = "%s %s %s" % (before, striptags(keyword), after)
kw_p = re.compile(r'(%s)'%keyword, re.IGNORECASE) kw_p = re.compile(r'(%s)' % keyword, re.IGNORECASE)
html = kw_p.sub(r"<strong>\1</strong>", html) html = kw_p.sub(r"<strong>\1</strong>", html)
html = mark_safe(html) html = mark_safe(html)
else: else:
html = " ".join(filter(lambda x: x!="", striptags(content).replace("\n", " ").split(" "))[:max_words]) html = " ".join(
filter(
lambda x: x != "",
striptags(content).replace(
"\n",
" ").split(" "))[
:max_words])
return html return html
...@@ -127,4 +151,4 @@ def login_url(context): ...@@ -127,4 +151,4 @@ def login_url(context):
qs = urlquote('?' + qs) qs = urlquote('?' + qs)
else: else:
qs = '' qs = ''
return settings.LOGIN_URL+"?next="+request.path + qs return settings.LOGIN_URL + "?next=" + request.path + qs
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment