-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature #159236196] Cache nutritional plan values
- Loading branch information
Showing
6 changed files
with
165 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- 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 | ||
|
||
from django.apps import AppConfig | ||
|
||
|
||
class NutritionPlanValuesConfig(AppConfig): | ||
name = 'wger.nutrition' | ||
verbose_name = "NutritionPlan" | ||
|
||
def ready(self): | ||
import wger.nutrition.signals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.db.models.signals import post_delete, post_save | ||
from django.dispatch import receiver | ||
from wger.nutrition.models import NutritionPlan, Meal, MealItem | ||
from django.core.cache import cache | ||
from wger.utils.cache import cache_mapper | ||
|
||
|
||
@receiver(post_save, sender=NutritionPlan) | ||
@receiver(post_delete, sender=NutritionPlan) | ||
@receiver(post_delete, sender=MealItem) | ||
@receiver(post_save, sender=MealItem) | ||
@receiver(post_save, sender=Meal) | ||
@receiver(post_delete, sender=Meal) | ||
def reset_nutrional_plan_canonical(sender, **kwargs): | ||
''' | ||
Function to send signal | ||
''' | ||
instance_sender = kwargs['instance'] | ||
if isinstance(instance_sender, (MealItem, NutritionPlan, Meal)): | ||
cache.delete(cache_mapper.get_nutritional_plan_canonical( | ||
instance_sender.get_owner_object().id)) |
54 changes: 54 additions & 0 deletions
54
wger/nutrition/tests/test_nutrional_plan_values_canonical.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# 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 | ||
from django.core.cache import cache | ||
|
||
from wger.nutrition.models import NutritionPlan, Meal, MealItem | ||
from wger.utils.cache import cache_mapper | ||
from wger.core.tests.base_testcase import WorkoutManagerTestCase | ||
|
||
|
||
class NutritionPlanCacheTestCase(WorkoutManagerTestCase): | ||
''' | ||
Test case for the NutritionalPlan canonical representation | ||
''' | ||
def test_nutritional_plan_canonical_form_cache(self): | ||
''' | ||
Test that the nuttritional plan cache is correctly generated | ||
''' | ||
self.assertFalse(cache.get(cache_mapper.get_nutritional_plan_canonical(1))) | ||
nutritional_plan = NutritionPlan.objects.get(pk=1) | ||
nutritional_plan.get_nutritional_values() | ||
self.assertTrue(cache.get(cache_mapper.get_nutritional_plan_canonical(1))) | ||
|
||
def test_nutritional_plan_canonical_form_cache_save(self): | ||
''' | ||
Tests the nutritional_plan values cache when saving a nutritional plan | ||
''' | ||
nutritional_plan = NutritionPlan.objects.get(pk=1) | ||
nutritional_plan.get_nutritional_values() | ||
self.assertTrue(cache.get(cache_mapper.get_nutritional_plan_canonical(1))) | ||
|
||
nutritional_plan.save() | ||
self.assertFalse(cache.get(cache_mapper.get_nutritional_plan_canonical(1))) | ||
|
||
def test_nutritional_plan_canonical_form_cache_delete(self): | ||
''' | ||
Tests the nutrional plan values cache when deleting | ||
''' | ||
nutritional_plan = NutritionPlan.objects.get(pk=1) | ||
nutritional_plan.get_nutritional_values() | ||
self.assertTrue(cache.get(cache_mapper.get_nutritional_plan_canonical(1))) | ||
|
||
nutritional_plan.delete() | ||
self.assertFalse(cache.get(cache_mapper.get_workout_canonical(1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters