+ prefix_regexp = re.compile(_word_starts_with_regexp(prefix))
+ return list(books) + list(tags) + [app for app in _apps if prefix_regexp.search(app.lower)] + list(book_stubs) + list(authors)
+
+
+def _get_result_link(match, tag_list):
+ if isinstance(match, models.Tag):
+ return reverse('catalogue.views.tagged_object_list',
+ kwargs={'tags': '/'.join(tag.url_chunk for tag in tag_list + [match])}
+ )
+ elif isinstance(match, App):
+ return match.view()
+ else:
+ return match.get_absolute_url()
+
+
+def _get_result_type(match):
+ if isinstance(match, models.Book) or isinstance(match, pdcounter_models.BookStub):
+ type = 'book'
+ else:
+ type = match.category
+ return type
+
+
+def books_starting_with(prefix):
+ prefix = prefix.lower()
+ return models.Book.objects.filter(_word_starts_with('title', prefix))
+
+
+def find_best_matches(query, user=None):
+ """ Finds a models.Book, Tag, models.BookStub or Author best matching a query.
+
+ Returns a with:
+ - zero elements when nothing is found,
+ - one element when a best result is found,
+ - more then one element on multiple exact matches
+
+ Raises a ValueError on too short a query.
+ """
+
+ query = query.lower()
+ if len(query) < 2:
+ raise ValueError("query must have at least two characters")
+
+ result = tuple(_tags_starting_with(query, user))
+ # remove pdcounter stuff
+ book_titles = set(match.pretty_title().lower() for match in result
+ if isinstance(match, models.Book))
+ authors = set(match.name.lower() for match in result
+ if isinstance(match, models.Tag) and match.category=='author')
+ result = tuple(res for res in result if not (
+ (isinstance(res, pdcounter_models.BookStub) and res.pretty_title().lower() in book_titles)
+ or (isinstance(res, pdcounter_models.Author) and res.name.lower() in authors)
+ ))
+
+ exact_matches = tuple(res for res in result if res.name.lower() == query)
+ if exact_matches:
+ return exact_matches
+ else:
+ return tuple(result)[:1]
+