fnp
/
wolnelektury.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
0d2ad83
)
db optimizations
author
Jan Szejko
<jan.szejko@gmail.com>
Wed, 6 Apr 2016 10:56:10 +0000
(12:56 +0200)
committer
Jan 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
patch
|
blob
|
history
src/catalogue/feeds.py
patch
|
blob
|
history
src/catalogue/models/book.py
patch
|
blob
|
history
src/catalogue/templates/catalogue/book_mini_box.html
patch
|
blob
|
history
src/catalogue/templatetags/catalogue_tags.py
patch
|
blob
|
history
src/catalogue/tests/tags.py
patch
|
blob
|
history
src/catalogue/views.py
patch
|
blob
|
history
src/opds/views.py
patch
|
blob
|
history
src/picture/models.py
patch
|
blob
|
history
src/picture/templates/picture/picture_mini_box.html
patch
|
blob
|
history
src/picture/views.py
patch
|
blob
|
history
src/wolnelektury/views.py
patch
|
blob
|
history
diff --git
a/src/api/management/commands/mobileinit.py
b/src/api/management/commands/mobileinit.py
index
b89fede
..
ccbff3e
100755
(executable)
--- a/
src/api/management/commands/mobileinit.py
+++ b/
src/api/management/commands/mobileinit.py
@@
-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)
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())
db.execute(book_sql, locals())
diff --git
a/src/catalogue/feeds.py
b/src/catalogue/feeds.py
index
2a65c59
..
0b7e4d4
100644
(file)
--- a/
src/catalogue/feeds.py
+++ b/
src/catalogue/feeds.py
@@
-52,8
+52,7
@@
class AudiobookFeed(Feed):
return item.name
def item_categories(self, item):
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 = []
def item_description(self, item):
lines = []
diff --git
a/src/catalogue/models/book.py
b/src/catalogue/models/book.py
index
a4017fb
..
f1501d0
100644
(file)
--- a/
src/catalogue/models/book.py
+++ b/
src/catalogue/models/book.py
@@
-106,8
+106,11
@@
class Book(models.Model):
except AttributeError:
return ''
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
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:
self.title = unicode(self.title) # ???
try:
- author = self.
tags.filter(category='author')[0]
.sort_key
- except
Index
Error:
+ author = self.
authors().first()
.sort_key
+ except
Attribute
Error:
author = u''
self.sort_key_author = author
author = u''
self.sort_key_author = author
@@
-474,7
+477,7
@@
class Book(models.Model):
return books
def pretty_title(self, html_links=False):
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])
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_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()
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, ()):
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)
if authors:
for author in authors:
books_by_author[author].append(book)
diff --git
a/src/catalogue/templates/catalogue/book_mini_box.html
b/src/catalogue/templates/catalogue/book_mini_box.html
index
88ec16d
..
d77f910
100755
(executable)
--- a/
src/catalogue/templates/catalogue/book_mini_box.html
+++ b/
src/catalogue/templates/catalogue/book_mini_box.html
@@
-5,13
+5,13
@@
<a href="{{ book.get_absolute_url }}">
{% endif %}
{% if book.cover_thumb %}
<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">
{% 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 %}
<span class="title">{{ book.title }}</span>
</div>
{% if with_link %}
diff --git
a/src/catalogue/templatetags/catalogue_tags.py
b/src/catalogue/templatetags/catalogue_tags.py
index
23eeeda
..
5064cb6
100644
(file)
--- a/
src/catalogue/templatetags/catalogue_tags.py
+++ b/
src/catalogue/templatetags/catalogue_tags.py
@@
-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
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()
names[-1][1].append(obj)
return locals()
diff --git
a/src/catalogue/tests/tags.py
b/src/catalogue/tests/tags.py
index
2ba617e
..
8d651a1
100644
(file)
--- a/
src/catalogue/tests/tags.py
+++ b/
src/catalogue/tests/tags.py
@@
-276,7
+276,7
@@
class BookTagsTests(WLTestCase):
book = models.Book.objects.get(slug='parent')
related_themes = book.related_themes()
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'])
['common-man'])
self.assertEqual([t.slug for t in book.tags.filter(category='kind')],
['kind'])
diff --git
a/src/catalogue/views.py
b/src/catalogue/views.py
index
50dde9f
..
c263608
100644
(file)
--- a/
src/catalogue/views.py
+++ b/
src/catalogue/views.py
@@
-609,11
+609,14
@@
class CustomPDFFormView(AjaxableFormView):
@ssi_included
def book_mini(request, pk, with_link=True):
@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,
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,
})
'with_link': with_link,
'show_lang': book.language_code() != settings.LANGUAGE_CODE,
})
diff --git
a/src/opds/views.py
b/src/opds/views.py
index
001b69d
..
189ff0f
100644
(file)
--- a/
src/opds/views.py
+++ b/
src/opds/views.py
@@
-190,14
+190,14
@@
class AcquisitionFeed(Feed):
def item_author_name(self, book):
try:
def item_author_name(self, book):
try:
- return book.
tags.filter(category='author')[0]
.name
- except
Key
Error:
+ return book.
authors().first()
.name
+ except
Attribute
Error:
return u''
def item_author_link(self, book):
try:
return u''
def item_author_link(self, book):
try:
- return book.
tags.filter(category='author')[0]
.get_absolute_url()
- except
Key
Error:
+ return book.
authors().first()
.get_absolute_url()
+ except
Attribute
Error:
return u''
def item_enclosure_url(self, book):
return u''
def item_enclosure_url(self, book):
diff --git
a/src/picture/models.py
b/src/picture/models.py
index
8ab5d21
..
b39661f
100644
(file)
--- a/
src/picture/models.py
+++ b/
src/picture/models.py
@@
-112,8
+112,8
@@
class Picture(models.Model):
self.sort_key = sortify(self.title)[:120]
try:
self.sort_key = sortify(self.title)[:120]
try:
- author = self.
tags.filter(category='author')[0]
.sort_key
- except
Index
Error:
+ author = self.
authors().first()
.sort_key
+ except
Attribute
Error:
author = u''
self.sort_key_author = author
author = u''
self.sort_key_author = author
@@
-124,8
+124,11
@@
class Picture(models.Model):
def __unicode__(self):
return self.title
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):
@permalink
def get_absolute_url(self):
@@
-313,7
+316,7
@@
class Picture(models.Model):
pics_by_author[tag] = []
for pic in pics.iterator():
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)
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):
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:
names.append((self.title, self.get_absolute_url()))
if html_links:
diff --git
a/src/picture/templates/picture/picture_mini_box.html
b/src/picture/templates/picture/picture_mini_box.html
index
d8ebbf7
..
1ad2acc
100644
(file)
--- a/
src/picture/templates/picture/picture_mini_box.html
+++ b/
src/picture/templates/picture/picture_mini_box.html
@@
-7,10
+7,10
@@
<a href="{{ picture.get_absolute_url }}">
{% endif %}
{% if picture.image_file %}
<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">
{% 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 %}
<span class="title">{{ picture.title }}</span>
</div>
{% if with_link %}
diff --git
a/src/picture/views.py
b/src/picture/views.py
index
fea5a5e
..
6b83c35
100644
(file)
--- a/
src/picture/views.py
+++ b/
src/picture/views.py
@@
-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)
@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,
return render(request, 'picture/picture_mini_box.html', {
'picture': picture,
- 'author
_str': author_str
,
+ 'author
': picture.author_unicode()
,
'with_link': with_link,
})
'with_link': with_link,
})
diff --git
a/src/wolnelektury/views.py
b/src/wolnelektury/views.py
index
c179280
..
1a6738b
100644
(file)
--- a/
src/wolnelektury/views.py
+++ b/
src/wolnelektury/views.py
@@
-35,10
+35,10
@@
def main_page(request):
# pass
# FIXME: find this theme and books properly.
# 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]
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]
if not tf:
continue
ctx['theme_fragment'] = tf[0]