remove duplicates
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 29 Dec 2010 21:01:39 +0000 (22:01 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 29 Dec 2010 21:20:45 +0000 (22:20 +0100)
apps/catalogue/models.py
apps/catalogue/templatetags/catalogue_tags.py
apps/catalogue/views.py
apps/pdcounter/models.py

index 54a9152..f201dc2 100644 (file)
@@ -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 = ['<a href="%s">%s</a>' % (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):
index 6ad83a0..6560730 100644 (file)
@@ -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 = ['<a href="%s">%s</a>' % (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
index 6b60dc0..7bb82fd 100644 (file)
@@ -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):
index f627815..f483293 100644 (file)
@@ -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))