Small refactoring of nutritional values cache logic

This commit is contained in:
Roland Geider
2020-08-31 23:43:12 +02:00
parent 0fb1a0e885
commit 99905118be
7 changed files with 50 additions and 30 deletions

View File

@@ -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/louiCoder
Translators
-----------

View File

@@ -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/340

View File

@@ -21,3 +21,4 @@ from wger import get_version
VERSION = get_version()
default_app_config = 'wger.nutrition.apps.NutritionConfig'

26
wger/nutrition/apps.py Normal file
View File

@@ -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.signals

View File

@@ -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 receiver
from django.template.loader import render_to_string
from django.urls import reverse
from 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))

View File

@@ -17,12 +17,16 @@
# Django
from django.core.cache import cache
from 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)

View File

@@ -1,9 +1,16 @@
# Django
from django.contrib.auth.models import User
from django.core.cache import cache
from wger.core.tests.base_testcase import WgerTestCase
from wger.nutrition.models import NutritionPlan, Meal, MealItem
from wger.utils.cache import cache_mapper
# wger
from wger.core.models import Language
from wger.core.tests.base_testcase import WgerTestCase
from wger.nutrition.models import (
Meal,
MealItem,
NutritionPlan
)
from wger.utils.cache import cache_mapper
class NutritionaCacheTestCase(WgerTestCase):