Skip to content

Commit

Permalink
Fix filter() call in get_content_snippet not working on Python 2.7+
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed May 20, 2014
1 parent e60cae5 commit 44dcfdd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
10 changes: 8 additions & 2 deletions wiki/templates/wiki/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ <h1 class="page-header">{% trans "Search results for:" %} {{ search_query }}</h1
<form class="form-search directory-toolbar">
<p class="lead">
<div class="pull-right">
{{ search_form.q }}
<button class="btn btn-default"><span class="icon-search"></span></button>
<div class="input-group">
<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>
<p>{% blocktrans with paginator.object_list.count as cnt %}Your search returned <strong>{{ cnt }}</strong> results.{% endblocktrans %}</p>
<div class="clearfix"></div>
Expand Down
48 changes: 36 additions & 12 deletions wiki/templatetags/wiki_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@
# called more than once per page in multiple template blocks.
_cache = {}


@register.assignment_tag(takes_context=True)
def article_for_object(context, obj):
if not isinstance(obj, Model):
raise TypeError(
"A Wiki article can only be associated to a Django Model "
"instance, not %s" % type(obj)
)

content_type = ContentType.objects.get_for_model(obj)

# TODO: This is disabled for now, as it should only fire once per request
# Maybe store cache in the request object?
if True or not obj in _cache.keys():
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:
article = None
_cache[obj] = article
Expand All @@ -44,7 +47,7 @@ def article_for_object(context, obj):

@register.inclusion_tag('wiki/includes/render.html', takes_context=True)
def wiki_render(context, article, preview_content=None):

if preview_content:
content = article.render(preview_content=preview_content)
else:
Expand All @@ -63,29 +66,50 @@ def wiki_render(context, article, preview_content=None):
@register.inclusion_tag('wiki/includes/form.html', takes_context=True)
def wiki_form(context, form_obj):
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})
return context


@register.filter
def get_content_snippet(content, keyword, max_words=30):
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)
html = ""
if m:
words = filter(lambda x: x!="", striptags(m.group("before")).replace("\n", " ").split(" "))
before_words = words[-max_words/2:]
words = filter(lambda x: x!="", striptags(m.group("after")).replace("\n", " ").split(" "))
words = list(filter(
lambda x: x != "",
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)])
before = " ".join(before_words)
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 = mark_safe(html)
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


Expand Down Expand Up @@ -127,4 +151,4 @@ def login_url(context):
qs = urlquote('?' + qs)
else:
qs = ''
return settings.LOGIN_URL+"?next="+request.path + qs
return settings.LOGIN_URL + "?next=" + request.path + qs

0 comments on commit 44dcfdd

Please sign in to comment.