Self hosted FLOSS fitness/workout, nutrition and weight tracker
Cache the ingredient list view and increase count method (only postgres)
Roland Geidere68afe7fc339
diff --git a/wger/core/templates/tags/pagination.html b/wger/core/templates/tags/pagination.htmlindex 819f8e70c..d272b2de1 100644--- a/wger/core/templates/tags/pagination.html+++ b/wger/core/templates/tags/pagination.html@@ -3,8 +3,9 @@<ul class="pagination justify-content-center">{% if page.has_previous %}<li class="page-item">- <a href="?page={{ page.previous_page_number }}"- class="load-maincontent page-link">« {% translate "previous" %}</a>+ <a href="?page={{ page.previous_page_number }}" class="page-link">+ « {% translate "previous" %}+ </a></li>{% else %}<li class="page-item disabled">@@ -15,25 +16,27 @@{% for i in page_range %}{% if page.number != i %}<li class="page-item">- <a href="?page={{ i }}"- class="load-maincontent page-link">{{i}}</a>- </li>+ <a href="?page={{ i }}" class="page-link">+ {{ i }}+ </a>+ </li>{% else %}<li class="page-item active">- <a href="#" class="page-link">{{i}}</a>+ <a href="#" class="page-link">{{ i }}</a></li>{% endif %}{% endfor %}- {% if page.has_next %}- <li class="page-item">- <a href="?page={{ page.next_page_number }}"- class="load-maincontent page-link">{%trans "next" %} »</a>- </li>- {% else %}- <li class="page-item disabled">- <a href="#" class="page-link">{%trans "next" %} »</a>- </li>- {% endif %}+ {% if page.has_next %}+ <li class="page-item">+ <a href="?page={{ page.next_page_number }}" class="page-link">+ {% trans "next" %} »+ </a>+ </li>+ {% else %}+ <li class="page-item disabled">+ <a href="#" class="page-link">{% trans "next" %} »</a>+ </li>+ {% endif %}</ul>diff --git a/wger/nutrition/managers.py b/wger/nutrition/managers.pynew file mode 100644index 000000000..9cef7590c--- /dev/null+++ b/wger/nutrition/managers.py@@ -0,0 +1,55 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) wger Team+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# Django+from django.conf import settings+from django.db import connections+from django.db.models import (+ Manager,+ QuerySet,+)++# wger+from wger.utils.db import is_postgres_db+++class ApproximateCountQuerySet(QuerySet):+ """+ Approximate count query set, postgreSQL only++ While this doesn't return an exact count, it does return an approximate+ in a much shorter amount of time, which is specially important for the+ ingredients++ https://testdriven.io/blog/django-approximate-counting/+ """++ def count(self):+ if self.query.where:+ return super(ApproximateCountQuerySet, self).count()++ if is_postgres_db() and not settings.TESTING:+ cursor = connections[self.db].cursor()+ cursor.execute(+ "SELECT reltuples FROM pg_class WHERE relname = '%s';" % self.model._meta.db_table+ )++ return int(cursor.fetchone()[0])++ return super(ApproximateCountQuerySet, self).count()+++ApproximateCountManager = Manager.from_queryset(ApproximateCountQuerySet)diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex 5c10c1922..fbc325104 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -50,6 +50,8 @@ from wger.nutrition.consts import (ENERGY_FACTOR,KJ_PER_KCAL,)+from wger.nutrition.managers import ApproximateCountManager+from wger.nutrition.models.ingredient_category import IngredientCategoryfrom wger.nutrition.models.sources import Sourcefrom wger.utils.cache import cache_mapperfrom wger.utils.constants import TWOPLACES@@ -57,9 +59,6 @@ from wger.utils.language import load_languagefrom wger.utils.models import AbstractLicenseModelfrom wger.utils.requests import wger_user_agent-# Local-from .ingredient_category import IngredientCategory-logger = logging.getLogger(__name__)@@ -75,6 +74,8 @@ class Ingredient(AbstractLicenseModel, models.Model):energy amount given (in percent)."""+ objects = ApproximateCountManager()+language = models.ForeignKey(Language,verbose_name=_('Language'),diff --git a/wger/nutrition/templates/ingredient/overview.html b/wger/nutrition/templates/ingredient/overview.htmlindex 926caabc3..1e2733de3 100644--- a/wger/nutrition/templates/ingredient/overview.html+++ b/wger/nutrition/templates/ingredient/overview.html@@ -41,8 +41,8 @@{% endif %}- {# <div style="padding-top:3em;"></div> #}- {# {% pagination paginator page_obj %} #}+ <div style="padding-top:3em;"></div>+ {% pagination paginator page_obj %}{% endblock %}{% block sidebar %}diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.pyindex 4b7b243a5..d4ae9f6e2 100644--- a/wger/nutrition/views/ingredient.py+++ b/wger/nutrition/views/ingredient.py@@ -18,6 +18,7 @@import logging# Django+from django.conf import settingsfrom django.contrib.auth.mixins import (LoginRequiredMixin,PermissionRequiredMixin,@@ -29,10 +30,12 @@ from django.shortcuts import (render,)from django.urls import reverse_lazy+from django.utils.decorators import method_decoratorfrom django.utils.translation import (gettext as _,gettext_lazy,)+from django.views.decorators.cache import cache_pagefrom django.views.generic import (CreateView,DeleteView,@@ -61,6 +64,7 @@ logger = logging.getLogger(__name__)# ************************# Ingredient functions# ************************+@method_decorator(cache_page(settings.WGER_SETTINGS['INGREDIENT_CACHE_TTL']), name='dispatch')class IngredientListView(ListView):"""Show an overview of all ingredients@@ -76,9 +80,11 @@ class IngredientListView(ListView):Filter the ingredients the user will see by its language"""language = load_language()- return Ingredient.objects.filter(language=language).only('id', 'name')+ return Ingredient.objects.filter(language=language)+# Cache for one week+@cache_page(settings.WGER_SETTINGS['INGREDIENT_CACHE_TTL'])def view(request, pk, slug=None):context = {}diff --git a/wger/settings_global.py b/wger/settings_global.pyindex 46cd492da..190408139 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -540,6 +540,7 @@ WGER_SETTINGS = {'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_WGER,'EMAIL_FROM': 'wger Workout Manager <[email protected]>','EXERCISE_CACHE_TTL': 3600,+ 'INGREDIENT_CACHE_TTL': 604800, # one week'MIN_ACCOUNT_AGE_TO_TRUST': 21,'SYNC_EXERCISES_CELERY': False,'SYNC_EXERCISE_IMAGES_CELERY': False,