db optimizations
authorJan Szejko <jan.szejko@gmail.com>
Wed, 6 Apr 2016 10:56:10 +0000 (12:56 +0200)
committerJan Szejko <jan.szejko@gmail.com>
Wed, 6 Apr 2016 10:56:10 +0000 (12:56 +0200)
12 files changed:
src/api/management/commands/mobileinit.py
src/catalogue/feeds.py
src/catalogue/models/book.py
src/catalogue/templates/catalogue/book_mini_box.html
src/catalogue/templatetags/catalogue_tags.py
src/catalogue/tests/tags.py
src/catalogue/views.py
src/opds/views.py
src/picture/models.py
src/picture/templates/picture/picture_mini_box.html
src/picture/views.py
src/wolnelektury/views.py

index b89fede..ccbff3e 100755 (executable)
@@ -147,7 +147,7 @@ def add_book(db, book):
     parent_number = book.parent_number
     sort_key = book.sort_key
     size_str = pretty_size(html_file_size)
-    authors = ", ".join(t.name for t in book.tags.filter(category='author'))
+    authors = book.author_unicode()
     db.execute(book_sql, locals())
 
 
index 2a65c59..0b7e4d4 100644 (file)
@@ -52,8 +52,7 @@ class AudiobookFeed(Feed):
         return item.name
 
     def item_categories(self, item):
-        return sorted(set(author.name for author in
-                      item.book.tags.filter(category='author').iterator()))
+        return sorted(item.book.authors().values_list('name', flat=True))
 
     def item_description(self, item):
         lines = []
index a4017fb..f1501d0 100644 (file)
@@ -106,8 +106,11 @@ class Book(models.Model):
         except AttributeError:
             return ''
 
-    def author_str(self):
-        return ", ".join(str(t) for t in self.tags.filter(category='author'))
+    def authors(self):
+        return self.tags.filter(category='author')
+
+    def author_unicode(self):
+        return ", ".join(self.authors().values_list('name', flat=True))
 
     def save(self, force_insert=False, force_update=False, **kwargs):
         from sortify import sortify
@@ -116,8 +119,8 @@ class Book(models.Model):
         self.title = unicode(self.title)  # ???
 
         try:
-            author = self.tags.filter(category='author')[0].sort_key
-        except IndexError:
+            author = self.authors().first().sort_key
+        except AttributeError:
             author = u''
         self.sort_key_author = author
 
@@ -474,7 +477,7 @@ class Book(models.Model):
         return books
 
     def pretty_title(self, html_links=False):
-        names = [(tag.name, tag.get_absolute_url()) for tag in self.tags.filter(category='author')]
+        names = [(tag.name, tag.get_absolute_url()) for tag in self.authors().only('name', 'category', 'slug')]
         books = self.parents() + [self]
         names.extend([(b.title, b.get_absolute_url()) for b in books])
 
@@ -504,8 +507,7 @@ class Book(models.Model):
         """
 
         books_by_parent = {}
-        books = cls.objects.all().order_by('parent_number', 'sort_key').only(
-                'title', 'parent', 'slug')
+        books = cls.objects.order_by('parent_number', 'sort_key').only('title', 'parent', 'slug')
         if book_filter:
             books = books.filter(book_filter).distinct()
 
@@ -525,7 +527,7 @@ class Book(models.Model):
             books_by_author[tag] = []
 
         for book in books_by_parent.get(None, ()):
-            authors = list(book.tags.filter(category='author'))
+            authors = list(book.authors().only('pk'))
             if authors:
                 for author in authors:
                     books_by_author[author].append(book)
index 88ec16d..d77f910 100755 (executable)
@@ -5,13 +5,13 @@
     <a href="{{ book.get_absolute_url }}">
     {% endif %}
         {% if book.cover_thumb %}
-            <img src="{{ book.cover_thumb.url }}" alt="{{ author_str }} – {{ book.title }}" class="cover" />
+            <img src="{{ book.cover_thumb.url }}" alt="{{ author }} – {{ book.title }}" class="cover" />
         {% endif %}
         {% if show_lang %}
             <span class="language" title="{{ book.language_name }}">{{ book.language_code }}</span>
         {% endif %}
         <div class="desc">
-            <span class="mono author">{{ author_str }}</span>
+            <span class="mono author">{{ author }}</span>
             <span class="title">{{ book.title }}</span>
         </div>
     {% if with_link %}
index 23eeeda..5064cb6 100644 (file)
@@ -370,7 +370,7 @@ def plain_list(context, object_list, with_initials=True, by_author=False, choice
                 initial = obj.get_initial().upper()
             if initial != last_initial:
                 last_initial = initial
-                names.append((obj.author_str() if by_author else initial, []))
+                names.append((obj.author_unicode() if by_author else initial, []))
         names[-1][1].append(obj)
     return locals()
 
index 2ba617e..8d651a1 100644 (file)
@@ -276,7 +276,7 @@ class BookTagsTests(WLTestCase):
         book = models.Book.objects.get(slug='parent')
         related_themes = book.related_themes()
 
-        self.assertEqual([t.slug for t in book.tags.filter(category='author')],
+        self.assertEqual([t.slug for t in book.authors()],
                          ['common-man'])
         self.assertEqual([t.slug for t in book.tags.filter(category='kind')],
                          ['kind'])
index 50dde9f..c263608 100644 (file)
@@ -609,11 +609,14 @@ class CustomPDFFormView(AjaxableFormView):
 
 @ssi_included
 def book_mini(request, pk, with_link=True):
-    book = get_object_or_404(models.Book, pk=pk)
-    author_str = ", ".join(tag.name for tag in book.tags.filter(category='author'))
+    # book = get_object_or_404(models.Book, pk=pk)
+    try:
+        book = models.Book.objects.only('cover_thumb', 'title', 'language', 'slug').get(pk=pk)
+    except models.Book.DoesNotExist:
+        raise Http404
     return render(request, 'catalogue/book_mini_box.html', {
         'book': book,
-        'author_str': author_str,
+        'author': book.author_unicode(),
         'with_link': with_link,
         'show_lang': book.language_code() != settings.LANGUAGE_CODE,
     })
index 001b69d..189ff0f 100644 (file)
@@ -190,14 +190,14 @@ class AcquisitionFeed(Feed):
 
     def item_author_name(self, book):
         try:
-            return book.tags.filter(category='author')[0].name
-        except KeyError:
+            return book.authors().first().name
+        except AttributeError:
             return u''
 
     def item_author_link(self, book):
         try:
-            return book.tags.filter(category='author')[0].get_absolute_url()
-        except KeyError:
+            return book.authors().first().get_absolute_url()
+        except AttributeError:
             return u''
 
     def item_enclosure_url(self, book):
index 8ab5d21..b39661f 100644 (file)
@@ -112,8 +112,8 @@ class Picture(models.Model):
         self.sort_key = sortify(self.title)[:120]
 
         try:
-            author = self.tags.filter(category='author')[0].sort_key
-        except IndexError:
+            author = self.authors().first().sort_key
+        except AttributeError:
             author = u''
         self.sort_key_author = author
 
@@ -124,8 +124,11 @@ class Picture(models.Model):
     def __unicode__(self):
         return self.title
 
-    def author_str(self):
-        return ", ".join(str(t) for t in self.tags.filter(category='author'))
+    def authors(self):
+        return self.tags.filter(category='author')
+
+    def author_unicode(self):
+        return ", ".join(self.authors().values_list('name', flat=True))
 
     @permalink
     def get_absolute_url(self):
@@ -313,7 +316,7 @@ class Picture(models.Model):
             pics_by_author[tag] = []
 
         for pic in pics.iterator():
-            authors = list(pic.tags.filter(category='author'))
+            authors = list(pic.authors().only('pk'))
             if authors:
                 for author in authors:
                     pics_by_author[author].append(pic)
@@ -332,9 +335,7 @@ class Picture(models.Model):
         return self._info
 
     def pretty_title(self, html_links=False):
-        picture = self
-        names = [(tag.name, tag.get_absolute_url())
-                 for tag in self.tags.filter(category='author')]
+        names = [(tag.name, tag.get_absolute_url()) for tag in self.authors().only('name', 'category', 'slug')]
         names.append((self.title, self.get_absolute_url()))
 
         if html_links:
index d8ebbf7..1ad2acc 100644 (file)
@@ -7,10 +7,10 @@
     <a href="{{ picture.get_absolute_url }}">
     {% endif %}
         {% if picture.image_file %}
-            <img src="{% thumbnail picture.image_file "139x193" crop="center" as thumb %}{{ thumb.url }}{% empty %}{{ picture.image_file.url }}{% endthumbnail %}" alt="{{ author_str }} – {{ picture.title }}" class="cover" />
+            <img src="{% thumbnail picture.image_file "139x193" crop="center" as thumb %}{{ thumb.url }}{% empty %}{{ picture.image_file.url }}{% endthumbnail %}" alt="{{ author }} – {{ picture.title }}" class="cover" />
         {% endif %}
         <div class="desc">
-            <span class="mono author">{{ author_str }}</span>
+            <span class="mono author">{{ author }}</span>
             <span class="title">{{ picture.title }}</span>
         </div>
     {% if with_link %}
index fea5a5e..6b83c35 100644 (file)
@@ -100,10 +100,9 @@ def import_picture(request):
 @ssi_included
 def picture_mini(request, pk, with_link=True):
     picture = get_object_or_404(Picture, pk=pk)
-    author_str = ", ".join(tag.name for tag in picture.tags.filter(category='author'))
     return render(request, 'picture/picture_mini_box.html', {
         'picture': picture,
-        'author_str': author_str,
+        'author': picture.author_unicode(),
         'with_link': with_link,
     })
 
index c179280..1a6738b 100644 (file)
@@ -35,10 +35,10 @@ def main_page(request):
     #         pass
 
     # FIXME: find this theme and books properly.
-    if Fragment.objects.count():
+    if Fragment.objects.exists():
         while True:
             ctx['theme'] = Tag.objects.filter(category='theme').order_by('?')[:1][0]
-            tf = Fragment.tagged.with_any([ctx['theme']]).order_by('?')[:100]
+            tf = Fragment.tagged.with_any([ctx['theme']]).select_related('book').order_by('?')[:100]
             if not tf:
                 continue
             ctx['theme_fragment'] = tf[0]