def test_canonical_form_cache(self): ''' Tests that the workout cache of the canonical form is correctly generated ''' self.assertFalse(cache.get(cache_mapper.get_workout_canonical(1))) workout = Workout.objects.get(pk=1) workout.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(1)))
def canonical_representation(self): """ Returns a canonical representation of the workout This form makes it easier to cache and use everywhere where all or part of a workout structure is needed. As an additional benefit, the template caches are not needed anymore. """ workout_canonical_form = cache.get( cache_mapper.get_workout_canonical(self.pk)) if not workout_canonical_form: day_canonical_repr = [] muscles_front = [] muscles_back = [] muscles_front_secondary = [] muscles_back_secondary = [] # Sort list by weekday day_list = [i for i in self.day_set.select_related()] day_list.sort(key=lambda day: day.get_first_day_id) for day in day_list: canonical_repr_day = day.get_canonical_representation() # Collect all muscles for i in canonical_repr_day['muscles']['front']: if i not in muscles_front: muscles_front.append(i) for i in canonical_repr_day['muscles']['back']: if i not in muscles_back: muscles_back.append(i) for i in canonical_repr_day['muscles']['frontsecondary']: if i not in muscles_front_secondary: muscles_front_secondary.append(i) for i in canonical_repr_day['muscles']['backsecondary']: if i not in muscles_back_secondary: muscles_back_secondary.append(i) day_canonical_repr.append(canonical_repr_day) workout_canonical_form = { 'obj': self, 'muscles': { 'front': muscles_front, 'back': muscles_back, 'frontsecondary': muscles_front_secondary, 'backsecondary': muscles_back_secondary }, 'day_list': day_canonical_repr } # Save to cache cache.set( cache_mapper.get_workout_canonical(self.pk), workout_canonical_form) return workout_canonical_form
def test_canonical_form_cache_delete(self): ''' Tests the workout cache when deleting ''' workout = Workout.objects.get(pk=1) workout.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(1))) workout.delete() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(1)))
def test_canonical_form_cache_save(self): """ Tests the workout cache when saving """ set = Set.objects.get(pk=1) set.exerciseday.training.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(set.exerciseday.training_id))) set.save() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(set.exerciseday.training_id)))
def test_canonical_form_cache_save(self): """ Tests the workout cache when saving """ workout = Workout.objects.get(pk=1) workout.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(1))) workout.save() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(1)))
def test_canonical_form_cache_save(self): ''' Tests the workout cache when saving ''' set = Set.objects.get(pk=1) set.exerciseday.training.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(set.exerciseday.training_id))) set.save() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(set.exerciseday.training_id)))
def test_canonical_form_cache_delete(self): ''' Tests the workout cache when deleting ''' day = Day.objects.get(pk=1) day.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(day.training_id))) day.delete() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(day.training_id)))
def test_canonical_form_cache_delete(self): """ Tests the workout cache when deleting """ day = Day.objects.get(pk=1) day.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(day.training_id))) day.delete() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(day.training_id)))
def test_canonical_form_cache_delete(self): ''' Tests the workout cache when deleting ''' set = Set.objects.get(pk=1) set.exerciseday.training.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(set.exerciseday.training_id))) set.delete() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(set.exerciseday.training_id)))
def test_canonical_form_cache_save(self): ''' Tests the workout cache when saving ''' day = Day.objects.get(pk=1) day.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(day.training_id))) day.save() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(day.training_id)))
def test_canonical_form_cache_delete(self): ''' Tests the workout cache when deleting ''' setting = Setting.objects.get(pk=1) workout_id = setting.set.exerciseday.training_id setting.set.exerciseday.training.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(workout_id))) setting.delete() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(workout_id)))
def test_canonical_form_cache_save(self): ''' Tests the workout cache when saving ''' setting = Setting.objects.get(pk=1) workout_id = setting.set.exerciseday.training_id setting.set.exerciseday.training.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(workout_id))) setting.save() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(workout_id)))
def test_canonical_form_cache_save(self): ''' Tests the workout cache when saving ''' exercise = Exercise.objects.get(pk=2) for set in exercise.set_set.all(): set.exerciseday.training.canonical_representation workout_id = set.exerciseday.training_id self.assertTrue(cache.get(cache_mapper.get_workout_canonical(workout_id))) exercise.save() self.assertFalse(cache.get(cache_mapper.get_workout_canonical(workout_id)))
def test_canonical_form_cache_save(self): """ Tests the workout cache when saving """ comment = ExerciseComment.objects.get(pk=1) for set in comment.exercise.set_set.all(): set.exerciseday.training.canonical_representation workout_id = set.exerciseday.training_id self.assertTrue( cache.get(cache_mapper.get_workout_canonical(workout_id))) comment.save() self.assertFalse( cache.get(cache_mapper.get_workout_canonical(workout_id)))
def canonical_representation(self): ''' Returns a canonical representation of the workout This form makes it easier to cache and use everywhere where all or part of a workout structure is needed. As an additional benefit, the template caches are not needed anymore. ''' workout_canonical_form = cache.get(cache_mapper.get_workout_canonical(self.pk)) if not workout_canonical_form: day_canonical_repr = [] muscles_front = [] muscles_back = [] muscles_front_secondary = [] muscles_back_secondary = [] # Sort list by weekday day_list = [i for i in self.day_set.select_related()] day_list.sort(key=lambda day: day.get_first_day_id) for day in day_list: canonical_repr_day = day.get_canonical_representation() # Collect all muscles for i in canonical_repr_day['muscles']['front']: if i not in muscles_front: muscles_front.append(i) for i in canonical_repr_day['muscles']['back']: if i not in muscles_back: muscles_back.append(i) for i in canonical_repr_day['muscles']['frontsecondary']: if i not in muscles_front_secondary: muscles_front_secondary.append(i) for i in canonical_repr_day['muscles']['backsecondary']: if i not in muscles_back_secondary: muscles_back_secondary.append(i) day_canonical_repr.append(canonical_repr_day) workout_canonical_form = {'obj': self, 'muscles': {'front': muscles_front, 'back': muscles_back, 'frontsecondary': muscles_front_secondary, 'backsecondary': muscles_back_secondary}, 'day_list': day_canonical_repr} # Save to cache cache.set(cache_mapper.get_workout_canonical(self.pk), workout_canonical_form) return workout_canonical_form
def test_canonical_form_cache_delete(self): ''' Tests the workout cache when deleting ''' exercise = Exercise.objects.get(pk=2) workout_ids = [] for set in exercise.set_set.all(): workout_id = set.exerciseday.training_id workout_ids.append(workout_id) set.exerciseday.training.canonical_representation self.assertTrue(cache.get(cache_mapper.get_workout_canonical(workout_id))) exercise.delete() for workout_id in workout_ids: self.assertFalse(cache.get(cache_mapper.get_workout_canonical(workout_id)))
def test_canonical_form_cache_delete(self): """ Tests the workout cache when deleting """ comment = ExerciseComment.objects.get(pk=1) workout_ids = [] for setting in comment.exercise.setting_set.all(): workout_id = setting.set.exerciseday.training_id workout_ids.append(workout_id) setting.set.exerciseday.training.canonical_representation self.assertTrue( cache.get(cache_mapper.get_workout_canonical(workout_id))) comment.delete() for workout_id in workout_ids: self.assertFalse( cache.get(cache_mapper.get_workout_canonical(workout_id)))
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)))