book search - new design - almost ready
[wolnelektury.git] / apps / search / templatetags / search_tags.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 # import feedparser
6 # import datetime
7
8 from django import template
9 from django.template import Node, Variable
10 from django.utils.encoding import smart_str
11 from django.core.urlresolvers import reverse
12 # from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
13 # from django.db.models import Q
14 from django.conf import settings
15 # from django.utils.translation import ugettext as _
16 from catalogue.templatetags.catalogue_tags import book_wide
17 from catalogue.models import Book
18 # from catalogue.forms import SearchForm
19 # from catalogue.utils import split_tags
20
21
22 register = template.Library()
23
24
25 @register.inclusion_tag('catalogue/book_searched.html')
26 def book_searched(result):
27     book = Book.objects.get(pk=result.book_id)
28     vals = book_wide(book)
29
30     # snippets = []
31     # for hit in result.hits:
32     #     if hit['snippets']:
33     #         snippets.append(hit['snippets'])
34     #     elif hit['fragment']:
35     #         snippets.append(hit['fragment'].short_text)
36
37     # We don't need hits which lead to sections but do not have
38     # snippets.
39     vals['hits'] = filter(lambda h: 'fragment' in h or
40                           h['snippets'], result.hits)
41
42     for hit in vals['hits']:
43         hit['snippets'] = map(lambda s: s.replace("\n", "<br />").replace('---', '&mdash;'), hit['snippets'])
44
45     return vals