Self hosted FLOSS fitness/workout, nutrition and weight tracker
Small refactoring of nutritional values cache logic
Roland Geider99905118be29
diff --git a/AUTHORS.rst b/AUTHORS.rstindex b9a3585af..44e463b03 100644--- a/AUTHORS.rst+++ b/AUTHORS.rst@@ -25,6 +25,7 @@ Developers* Malcolm Jones: https://github.com/DevloperMal* Boniface Mwenda: https://github.com/andela-bmwenda* Scott Peshak: https://github.com/speshak+* Musanje Louis Michael: https://github.com/louiCoderTranslators-----------diff --git a/docs/changelog.rst b/docs/changelog.rstindex d9f378cf0..c0fdf414e 100644--- a/docs/changelog.rst+++ b/docs/changelog.rst@@ -32,7 +32,11 @@ Upgrade steps from 1.9:* Updated many libraries to last version (bootstrap, font awesome, etc.)* Use yarn to download CSS/JS libraries* Improvements to documentation (e.g. `#494`_)+* Improved cache handling `#246`_ (thanks `@louiCoder`_)+.. _@louiCoder: https://github.com/louiCoder++.. _#246: https://github.com/wger-project/wger/issues/246.. _#284: https://github.com/wger-project/wger/issues/284.. _#337: https://github.com/wger-project/wger/issues/337.. _#340: https://github.com/wger-project/wger/issues/340diff --git a/wger/nutrition/__init__.py b/wger/nutrition/__init__.pyindex 46a91e9bd..a46a190d3 100644--- a/wger/nutrition/__init__.py+++ b/wger/nutrition/__init__.py@@ -21,3 +21,4 @@ from wger import get_versionVERSION = get_version()+default_app_config = 'wger.nutrition.apps.NutritionConfig'diff --git a/wger/nutrition/apps.py b/wger/nutrition/apps.pynew file mode 100644index 000000000..62f4cbcf7--- /dev/null+++ b/wger/nutrition/apps.py@@ -0,0 +1,26 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# 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 General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# Django+from django.apps import AppConfig+++class NutritionConfig(AppConfig):+ name = 'wger.nutrition'+ verbose_name = "Nutrition"++ def ready(self):+ import wger.nutrition.signalsdiff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex 1aed02a92..278cd312c 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -32,8 +32,6 @@ from django.core.validators import (MinValueValidator)from django.db import models-from django.db.models.signals import post_save, post_delete-from django.dispatch import receiverfrom django.template.loader import render_to_stringfrom django.urls import reversefrom django.utils import translation@@ -813,20 +811,3 @@ class LogItem(BaseMealItem, models.Model):Returns the object that has owner information"""return self.plan---@receiver(post_save, sender=NutritionPlan)-@receiver(post_delete, sender=NutritionPlan)-@receiver(post_save, sender=Meal)-@receiver(post_delete, sender=Meal)-@receiver(post_save, sender=MealItem)-@receiver(post_delete, sender=MealItem)-def reset_nutritional_values_canonical_form(sender, **kwargs):- '''- Reset the nutrition values canonical form in cache- '''- sender_instance = kwargs["instance"]- if isinstance(sender_instance, (Meal, MealItem)):- cache.delete(cache_mapper.get_nutrition_cache_by_key(sender_instance.get_owner_object().id))- elif isinstance(sender_instance, NutritionPlan):- cache.delete(cache_mapper.get_nutrition_cache_by_key(sender_instance.id))diff --git a/wger/nutrition/signals.py b/wger/nutrition/signals.pyindex b146c7944..c2b8b8837 100644--- a/wger/nutrition/signals.py+++ b/wger/nutrition/signals.py@@ -17,12 +17,16 @@# Djangofrom django.core.cache import cachefrom django.db.models.signals import (- post_save,- post_delete+ post_delete,+ post_save)# wger-from wger.nutrition.models import NutritionPlan, Meal, MealItem+from wger.nutrition.models import (+ Meal,+ MealItem,+ NutritionPlan+)from wger.utils.cache import cache_mapper@@ -30,10 +34,7 @@ def reset_nutritional_values_canonical_form(sender, instance, **kwargs):"""Reset the nutrition values canonical form in cache"""- if isinstance(instance, (Meal, MealItem)):- cache.delete(cache_mapper.get_nutrition_cache_by_key(instance.get_owner_object().id))- elif isinstance(instance, NutritionPlan):- cache.delete(cache_mapper.get_nutrition_cache_by_key(instance.id))+ cache.delete(cache_mapper.get_nutrition_cache_by_key(instance.get_owner_object().id))post_save.connect(reset_nutritional_values_canonical_form, sender=NutritionPlan)@@ -42,4 +43,3 @@ post_save.connect(reset_nutritional_values_canonical_form, sender=Meal)post_delete.connect(reset_nutritional_values_canonical_form, sender=Meal)post_save.connect(reset_nutritional_values_canonical_form, sender=MealItem)post_delete.connect(reset_nutritional_values_canonical_form, sender=MealItem)-diff --git a/wger/nutrition/tests/test_nutritional_cache.py b/wger/nutrition/tests/test_nutritional_cache.pyindex d5c312b65..cbd8ad9e7 100644--- a/wger/nutrition/tests/test_nutritional_cache.py+++ b/wger/nutrition/tests/test_nutritional_cache.py@@ -1,9 +1,16 @@+# Djangofrom django.contrib.auth.models import Userfrom django.core.cache import cache++# wger+from wger.core.models import Languagefrom wger.core.tests.base_testcase import WgerTestCase-from wger.nutrition.models import NutritionPlan, Meal, MealItem+from wger.nutrition.models import (+ Meal,+ MealItem,+ NutritionPlan+)from wger.utils.cache import cache_mapper-from wger.core.models import Languageclass NutritionaCacheTestCase(WgerTestCase):