forked from mirrors/bookwyrm
Reduces feed page queries for goals
9 fewer queries
This commit is contained in:
parent
2e7d84d53c
commit
368d2cd716
3 changed files with 20 additions and 17 deletions
bookwyrm
|
@ -381,17 +381,16 @@ class AnnualGoal(BookWyrmModel):
|
||||||
return {r.book.id: r.rating for r in reviews}
|
return {r.book.id: r.rating for r in reviews}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def progress_percent(self):
|
def progress(self):
|
||||||
"""how close to your goal, in percent form"""
|
|
||||||
return int(float(self.book_count / self.goal) * 100)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def book_count(self):
|
|
||||||
"""how many books you've read this year"""
|
"""how many books you've read this year"""
|
||||||
return self.user.readthrough_set.filter(
|
count = self.user.readthrough_set.filter(
|
||||||
finish_date__year__gte=self.year,
|
finish_date__year__gte=self.year,
|
||||||
finish_date__year__lt=self.year + 1,
|
finish_date__year__lt=self.year + 1,
|
||||||
).count()
|
).count()
|
||||||
|
return {
|
||||||
|
"count": count,
|
||||||
|
"percent": int(float(count / self.goal) * 100),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.task
|
@app.task
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% load humanize %}
|
{% load humanize %}
|
||||||
|
|
||||||
|
{% with goal.progress as progress %}
|
||||||
<p>
|
<p>
|
||||||
{% if goal.progress_percent >= 100 %}
|
{% if progress.percent >= 100 %}
|
||||||
{% trans "Success!" %}
|
{% trans "Success!" %}
|
||||||
{% elif goal.progress_percent %}
|
{% elif progress.percent %}
|
||||||
{% blocktrans with percent=goal.progress_percent %}{{ percent }}% complete!{% endblocktrans %}
|
{% blocktrans with percent=progress.percent %}{{ percent }}% complete!{% endblocktrans %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if goal.user == request.user %}
|
{% if goal.user == request.user %}
|
||||||
{% blocktrans with read_count=goal.book_count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}You've read <a href="{{ path }}">{{ read_count }} of {{ goal_count}} books</a>.{% endblocktrans %}
|
{% blocktrans with read_count=progress.count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}You've read <a href="{{ path }}">{{ read_count }} of {{ goal_count}} books</a>.{% endblocktrans %}
|
||||||
{% else %}
|
{% else %}
|
||||||
{% blocktrans with username=goal.user.display_name read_count=goal.book_count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}{{ username }} has read <a href="{{ path }}">{{ read_count }} of {{ goal_count}} books</a>.{% endblocktrans %}
|
{% blocktrans with username=goal.user.display_name read_count=progress.count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}{{ username }} has read <a href="{{ path }}">{{ read_count }} of {{ goal_count}} books</a>.{% endblocktrans %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
<progress class="progress is-large" value="{{ goal.book_count }}" max="{{ goal.goal }}" aria-hidden="true">{{ goal.progress_percent }}%</progress>
|
<progress class="progress is-large" value="{{ progress.count }}" max="{{ goal.goal }}" aria-hidden="true">{{ progress.percent }}%</progress>
|
||||||
|
|
||||||
|
{% endwith %}
|
||||||
|
|
|
@ -162,14 +162,15 @@ def get_suggested_books(user, max_books=5):
|
||||||
else max_books - book_count
|
else max_books - book_count
|
||||||
)
|
)
|
||||||
shelf = user.shelf_set.get(identifier=preset)
|
shelf = user.shelf_set.get(identifier=preset)
|
||||||
|
if not shelf.books.exists():
|
||||||
shelf_books = shelf.shelfbook_set.order_by("-updated_date")[:limit]
|
|
||||||
if not shelf_books:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
shelf_preview = {
|
shelf_preview = {
|
||||||
"name": shelf.name,
|
"name": shelf.name,
|
||||||
"identifier": shelf.identifier,
|
"identifier": shelf.identifier,
|
||||||
"books": [s.book for s in shelf_books],
|
"books": shelf.books.order_by("shelfbook").prefetch_related("authors")[
|
||||||
|
:limit
|
||||||
|
],
|
||||||
}
|
}
|
||||||
suggested_books.append(shelf_preview)
|
suggested_books.append(shelf_preview)
|
||||||
book_count += len(shelf_preview["books"])
|
book_count += len(shelf_preview["books"])
|
||||||
|
|
Loading…
Reference in a new issue