- # Prefix must have at least 2 characters
- if len(prefix) < 2:
- return HttpResponseRedirect(reverse('catalogue.views.search_no_hits',
- kwargs={'tags': '/'.join(tag.slug for tag in tag_list)}
- ))
-
- result = _tags_starting_with(prefix, request.user)
- if len(result) > 0:
- tag = result[0]
- if isinstance(tag, models.Book) or isinstance(tag, models.BookStub):
- return HttpResponseRedirect(tag.get_absolute_url())
- else:
- tag_list.append(tag)
-
- return HttpResponseRedirect(reverse('catalogue.views.tagged_object_list',
- kwargs={'tags': '/'.join(tag.slug for tag in tag_list)}
- ))
+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 Book, Tag, 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