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:17 +0000 (22:20 +0100)
apps/catalogue/models.py
apps/catalogue/templatetags/catalogue_tags.py
apps/catalogue/views.py
apps/pdcounter/models.py

index 7cc7715..1d0b83b 100644 (file)
@@ -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 = ['<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):
     text = models.TextField()
     short_text = models.TextField(editable=False)
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 26821a3..82ca9fd 100644 (file)
@@ -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):
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))