+
+def _tags_starting_with(prefix, user):
+ book_stubs = models.BookStub.objects.filter(_word_starts_with('title', prefix))
+ books = models.Book.objects.filter(_word_starts_with('title', prefix))
+ book_stubs = filter(lambda x: x not in books, book_stubs)
+ tags = models.Tag.objects.filter(_word_starts_with('name', prefix))
+ if user.is_authenticated():
+ tags = tags.filter(~Q(category='book') & (~Q(category='set') | Q(user=user)))
+ else:
+ tags = tags.filter(~Q(category='book') & ~Q(category='set'))
+
+ return list(books) + list(tags) + list(book_stubs)
+
+
+
+def _get_result_link(match, tag_list):
+ if isinstance(match, models.Book) or isinstance(match, models.BookStub):
+ return match.get_absolute_url()
+ else:
+ return reverse('catalogue.views.tagged_object_list',
+ kwargs={'tags': '/'.join(tag.slug for tag in tag_list + [match])}
+ )
+
+def _get_result_type(match):
+ if isinstance(match, models.Book) or isinstance(match, models.BookStub):
+ type = 'book'
+ else:
+ type = match.category
+ return dict(models.TAG_CATEGORIES)[type]
+
+
+
+def search(request):
+ tags = request.GET.get('tags', '')
+ prefix = request.GET.get('q', '')
+