Merge pull request #2636 from 0x29a/redundant-db-queries

Remove redundant DB queries
This commit is contained in:
Mouse Reeve 2023-01-30 09:03:18 -08:00 committed by GitHub
commit 006ff697b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 14 deletions

View file

@ -215,10 +215,10 @@
{% endif %} {% endif %}
{% with work=book.parent_work %} {% with work=book.parent_work editions_count=book.parent_work.editions.count %}
<p> <p>
<a href="{{ work.local_path }}/editions" id="tour-other-editions-link"> <a href="{{ work.local_path }}/editions" id="tour-other-editions-link">
{% blocktrans trimmed count counter=work.editions.count with count=work.editions.count|intcomma %} {% blocktrans trimmed count counter=editions_count with count=editions_count|intcomma %}
{{ count }} edition {{ count }} edition
{% plural %} {% plural %}
{{ count }} editions {{ count }} editions

View file

@ -25,6 +25,7 @@
{% block body %} {% block body %}
<nav class="navbar" aria-label="main navigation"> <nav class="navbar" aria-label="main navigation">
<div class="container"> <div class="container">
{% with notification_count=request.user.unread_notification_count has_unread_mentions=request.user.has_unread_mentions %}
<div class="navbar-brand"> <div class="navbar-brand">
<a class="navbar-item" href="/"> <a class="navbar-item" href="/">
<img class="image logo" src="{% if site.logo_small %}{% get_media_prefix %}{{ site.logo_small }}{% else %}{% static "images/logo-small.png" %}{% endif %}" alt="{% blocktrans with site_name=site.name %}{{ site_name }} home page{% endblocktrans %}"> <img class="image logo" src="{% if site.logo_small %}{% get_media_prefix %}{{ site.logo_small }}{% else %}{% static "images/logo-small.png" %}{% endif %}" alt="{% blocktrans with site_name=site.name %}{{ site_name }} home page{% endblocktrans %}">
@ -67,9 +68,8 @@
> >
<i class="icon-dots-three-vertical" aria-hidden="true"></i> <i class="icon-dots-three-vertical" aria-hidden="true"></i>
{% with request.user.unread_notification_count as notification_count %}
<strong <strong
class="{% if not notification_count %}is-hidden {% elif request.user.has_unread_mentions %}is-danger {% else %}is-primary {% endif %} tag is-small px-1" class="{% if not notification_count %}is-hidden {% elif has_unread_mentions %}is-danger {% else %}is-primary {% endif %} tag is-small px-1"
data-poll-wrapper data-poll-wrapper
> >
<span class="is-sr-only">{% trans "Notifications" %}</span> <span class="is-sr-only">{% trans "Notifications" %}</span>
@ -77,7 +77,6 @@
{{ notification_count }} {{ notification_count }}
</strong> </strong>
</strong> </strong>
{% endwith %}
</button> </button>
</div> </div>
@ -108,14 +107,12 @@
<span class="is-sr-only">{% trans "Notifications" %}</span> <span class="is-sr-only">{% trans "Notifications" %}</span>
</span> </span>
</span> </span>
{% with request.user.unread_notification_count as notification_count %}
<span <span
class="{% if not notification_count %}is-hidden {% elif request.user.has_unread_mentions %}is-danger {% endif %}tag is-medium transition-x" class="{% if not notification_count %}is-hidden {% elif has_unread_mentions %}is-danger {% endif %}tag is-medium transition-x"
data-poll-wrapper data-poll-wrapper
> >
<span data-poll="notifications">{{ notification_count }}</span> <span data-poll="notifications">{{ notification_count }}</span>
</span> </span>
{% endwith %}
</a> </a>
</div> </div>
{% else %} {% else %}
@ -154,6 +151,7 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% endwith %}
</div> </div>
</nav> </nav>

View file

@ -19,9 +19,9 @@ uuid: a unique identifier used to make html "id" attributes unique and clarify j
{# Supplemental fields #} {# Supplemental fields #}
<div> <div>
{% active_shelf book as active_shelf %} {% active_shelf book as active_shelf %}
{% if active_shelf.shelf.identifier == 'reading' and book.latest_readthrough %} {% if active_shelf.shelf.identifier == 'reading' %}
{% with readthrough=book.latest_readthrough %} {% with readthrough=book.latest_readthrough %}
{% if readthrough %}
<div class="field"> <div class="field">
<input type="hidden" name="id" value="{{ readthrough.id }}"/> <input type="hidden" name="id" value="{{ readthrough.id }}"/>
<label class="label" for="progress_{{ uuid }}">{% trans "Progress:" %}</label> <label class="label" for="progress_{{ uuid }}">{% trans "Progress:" %}</label>
@ -66,6 +66,7 @@ uuid: a unique identifier used to make html "id" attributes unique and clarify j
<p class="help">{% blocktrans with pages=book.pages %}of {{ pages }} pages{% endblocktrans %}</p> <p class="help">{% blocktrans with pages=book.pages %}of {{ pages }} pages{% endblocktrans %}</p>
{% endif %} {% endif %}
</div> </div>
{% endif %}
{% endwith %} {% endwith %}
{% endif %} {% endif %}
</div> </div>

View file

@ -237,16 +237,24 @@ def feed_page_data(user):
def get_suggested_books(user, max_books=5): def get_suggested_books(user, max_books=5):
"""helper to get a user's recent books""" """helper to get a user's recent books"""
book_count = 0 book_count = 0
preset_shelves = [("reading", max_books), ("read", 2), ("to-read", max_books)] preset_shelves = {"reading": max_books, "read": 2, "to-read": max_books}
suggested_books = [] suggested_books = []
for (preset, shelf_max) in preset_shelves:
user_shelves = {
shelf.identifier: shelf
for shelf in user.shelf_set.filter(
identifier__in=preset_shelves.keys()
).exclude(books__isnull=True)
}
for preset, shelf_max in preset_shelves.items():
limit = ( limit = (
shelf_max shelf_max
if shelf_max < (max_books - book_count) if shelf_max < (max_books - book_count)
else max_books - book_count else max_books - book_count
) )
shelf = user.shelf_set.get(identifier=preset) shelf = user_shelves.get(preset, None)
if not shelf.books.exists(): if not shelf:
continue continue
shelf_preview = { shelf_preview = {