From: Radek Czajka Date: Wed, 29 Dec 2010 21:01:39 +0000 (+0100) Subject: remove duplicates X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/1d641adedff354dc932c81ae7b1d9f8c41d0b459?ds=sidebyside;hp=--cc remove duplicates --- 1d641adedff354dc932c81ae7b1d9f8c41d0b459 diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 7cc77156d..1d0b83b65 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -648,6 +648,24 @@ class Book(models.Model): return self.refresh_theme_counter() return dict((int(k), v) for k, v in self.get__theme_counter_value().iteritems()) + def pretty_title(self, html_links=False): + book = self + names = list(book.tags.filter(category='author')) + + books = [] + while book: + books.append(book) + book = book.parent + names.extend(reversed(books)) + + if html_links: + names = ['%s' % (tag.get_absolute_url(), tag.name) for tag in names] + else: + names = [tag.name for tag in names] + + return ', '.join(names) + + class Fragment(models.Model): text = models.TextField() short_text = models.TextField(editable=False) diff --git a/apps/catalogue/templatetags/catalogue_tags.py b/apps/catalogue/templatetags/catalogue_tags.py index 6ad83a045..6560730f2 100644 --- a/apps/catalogue/templatetags/catalogue_tags.py +++ b/apps/catalogue/templatetags/catalogue_tags.py @@ -53,29 +53,9 @@ def simple_title(tags): return capfirst(', '.join(title)) -def book_stub_title(book): - return ', '.join((book.author, book.title)) - - @register.simple_tag def book_title(book, html_links=False): - try: - names = list(book.tags.filter(category='author')) - except AttributeError: - return book_stub_title(book) - - books = [] - while book: - books.append(book) - book = book.parent - names.extend(reversed(books)) - - if html_links: - names = ['%s' % (tag.get_absolute_url(), tag.name) for tag in names] - else: - names = [tag.name for tag in names] - - return ', '.join(names) + return book.pretty_title(html_links) @register.simple_tag diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 26821a3af..82ca9fde0 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -428,11 +428,21 @@ def find_best_matches(query, user=None): 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 = (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 result[:1] + return tuple(result)[:1] def search(request): diff --git a/apps/pdcounter/models.py b/apps/pdcounter/models.py index f62781555..f4832931f 100644 --- a/apps/pdcounter/models.py +++ b/apps/pdcounter/models.py @@ -83,3 +83,5 @@ class BookStub(models.Model): def name(self): return self.title + def pretty_title(self, html_links=False): + return ', '.join((self.author, self.title))