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/0739e3055a53ad77c1c7de4352750f60453e20c1?ds=inline;hp=--cc remove duplicates --- 0739e3055a53ad77c1c7de4352750f60453e20c1 diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 54a915227..f201dc237 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -548,6 +548,22 @@ 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): 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 6b60dc0fd..7bb82fd4a 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -378,11 +378,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))