Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Simplify exercise detail view by using the muscle render tag

Roland Geider11f1c40f999e
diff --git a/wger/core/templates/tags/muscles.html b/wger/core/templates/tags/muscles.html
index 70e7678b5..cd36bda5d 100644
--- a/wger/core/templates/tags/muscles.html
+++ b/wger/core/templates/tags/muscles.html
@@ -1,4 +1,6 @@
{% load static %}
+{% if not empty %}
<div class="muscle-background mx-auto"
style="background-image: {% for background in backgrounds %}url({% static background %}){% if not forloop.last %},{% endif %}{% endfor %};">
</div>
+{% endif %}
diff --git a/wger/core/templatetags/wger_extras.py b/wger/core/templatetags/wger_extras.py
index a8f038b83..8ec4d518a 100644
--- a/wger/core/templatetags/wger_extras.py
+++ b/wger/core/templatetags/wger_extras.py
@@ -111,22 +111,32 @@ def license_sidebar(license, author=None):
@register.inclusion_tag('tags/muscles.html')
-def render_muscles(muscles_main, muscles_sec=None):
+def render_muscles(muscles=None, muscles_sec=None):
"""
Renders the given muscles
"""
- out_main = muscles_main if isinstance(muscles_main, (list, tuple, QuerySet)) else [muscles_main]
- muscles_sec = muscles_sec if muscles_sec is not None else []
+ if not muscles and not muscles_sec:
+ return {"empty": True}
- out_sec = muscles_sec if isinstance(muscles_sec, (list, tuple, QuerySet)) else [muscles_sec]
- front_back = "front" if out_main[0].is_front else "back"
- backgrounds_main = ["images/muscles/main/muscle-{}.svg".format(i.id) for i in out_main]
- backgrounds_sec = ["images/muscles/secondary/muscle-{}.svg".format(i.id) for i in out_sec]
- backgrounds = backgrounds_main + backgrounds_sec
- backgrounds.append("images/muscles/muscular_system_{}.svg".format(front_back))
+ out_main = []
+ if muscles:
+ out_main = muscles if isinstance(muscles, (list, tuple, QuerySet)) else [muscles]
- return {"backgrounds": backgrounds}
+ out_sec = []
+ if muscles_sec:
+ out_sec = muscles_sec if isinstance(muscles_sec, (list, tuple, QuerySet)) else [muscles_sec]
+ try:
+ front_back = "front" if out_main[0].is_front else "back"
+ except IndexError:
+ front_back = "front" if out_sec[0].is_front else "back"
+
+ backgrounds = ["images/muscles/main/muscle-{}.svg".format(i.id) for i in out_main] \
+ + ["images/muscles/secondary/muscle-{}.svg".format(i.id) for i in out_sec] \
+ + ["images/muscles/muscular_system_{}.svg".format(front_back)]
+
+ return {"backgrounds": backgrounds,
+ "empty": False}
@register.inclusion_tag('tags/language_select.html', takes_context=True)
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index e375095c0..730262463 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -24,7 +24,6 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core import mail
-from django.core.cache import cache
from django.core.validators import MinLengthValidator
from django.db import models
from django.template.loader import render_to_string
@@ -39,7 +38,6 @@ import bleach
# wger
from wger.core.models import Language
from wger.utils.cache import (
- cache_mapper,
delete_template_fragment_cache,
reset_workout_canonical_form
)
@@ -233,9 +231,6 @@ class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
self.name = smart_capitalize(self.name_original)
super(Exercise, self).save(*args, **kwargs)
- # Cached objects
- cache.delete(cache_mapper.get_exercise_muscle_bg_key(self))
-
# Cached template fragments
for language in Language.objects.all():
delete_template_fragment_cache('muscle-overview', language.id)
@@ -251,9 +246,6 @@ class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
Reset all cached infos
"""
- # Cached objects
- cache.delete(cache_mapper.get_exercise_muscle_bg_key(self))
-
# Cached template fragments
for language in Language.objects.all():
delete_template_fragment_cache('muscle-overview', language.id)
diff --git a/wger/exercises/templates/exercise/view.html b/wger/exercises/templates/exercise/view.html
index 0fe3081db..7b56161a8 100644
--- a/wger/exercises/templates/exercise/view.html
+++ b/wger/exercises/templates/exercise/view.html
@@ -69,11 +69,11 @@ $(document).ready(function() {
{% endif %} {# end exercise is pending review #}
-<h5>{% trans "Category" %}:</h5>
+<h5 class="mt-4">{% trans "Category" %}:</h5>
{% trans exercise.category.name %}
{% if exercise.equipment.all %}
- <h5>{% trans "Equipment" %}:</h5>
+ <h5 class="mt-4">{% trans "Equipment" %}:</h5>
{% for equipment in exercise.equipment.all %}
{% trans equipment.name %}
{% if not forloop.last %}, {% endif %}
@@ -195,10 +195,7 @@ $(document).ready(function() {
{% cache cache_timeout exercise-detail-muscles cache_vary_on %}
-{% with muscles=exercise.muscles.all %}
-{% with muscles_secondary=exercise.muscles_secondary.all %}
-
-{% if muscles %}
+{% if muscles_main_front or muscles_main_back %}
<h5 class="mt-4">{% trans "Muscles" %}:</h5>
<p>
{% trans "The diagram shows the most used muscles on this exercise" %}
@@ -214,43 +211,31 @@ $(document).ready(function() {
<div class="row ">
<div class="col">
- <div id="muscle-front" class="muscle-background mx-auto" style="background-image: {% for background in muscle_backgrounds_front %}url({% static background %}){% if not forloop.last %},{% endif %}{% endfor %};">
- </div>
+ {% render_muscles muscles_main_front muscles_sec_front %}
<ul class="list-unstyled">
- {% for muscle in muscles %}
- {% if muscle.is_front %}
- <li>{{muscle.name}}</li>
- {% endif %}
+ {% for muscle in muscles_main_front %}
+ <li>{{muscle.name}}</li>
{% endfor %}
- {% for muscle in muscles_secondary %}
- {% if muscle.is_front %}
- <li>{{muscle.name}}</li>
- {% endif %}
+ {% for muscle in muscles_sec_front %}
+ <li>{{muscle.name}}</li>
{% endfor %}
</ul>
</div>
<div class="col">
- <div id="muscle-back" class="muscle-background mx-auto" style="background-image: {% for background in muscle_backgrounds_back %}url({% static background %}){% if not forloop.last %},{% endif %}{% endfor %};">
- </div>
+ {% render_muscles muscles_main_back muscles_sec_back %}
<ul class="list-unstyled">
- {% for muscle in muscles %}
- {% if not muscle.is_front %}
- <li>{{muscle.name}}</li>
- {% endif %}
+ {% for muscle in muscles_main_back %}
+ <li>{{muscle.name}}</li>
{% endfor %}
- {% for muscle in muscles_secondary %}
- {% if not muscle.is_front %}
- <li>{{muscle.name}}</li>
- {% endif %}
+ {% for muscle in muscles_sec_back %}
+ <li>{{muscle.name}}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
-{% endwith %}
-{% endwith %} {# end musclelist #}
{% endcache %}
{% if logs %}
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index a35bbffef..52b7f1f11 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -26,7 +26,6 @@ from django.contrib.auth.mixins import (
PermissionRequiredMixin
)
from django.core import mail
-from django.core.cache import cache
from django.forms import (
CheckboxSelectMultiple,
ModelChoiceField,
@@ -74,7 +73,6 @@ from wger.exercises.models import (
Muscle
)
from wger.manager.models import WorkoutLog
-from wger.utils.cache import cache_mapper
from wger.utils.generic_views import (
WgerDeleteMixin,
WgerFormMixin
@@ -136,35 +134,10 @@ def view(request, id, slug=None):
template_data['exercise'] = exercise
- # Create the backgrounds that show what muscles the exercise works on
- backgrounds = cache.get(cache_mapper.get_exercise_muscle_bg_key(int(id)))
- if not backgrounds:
- backgrounds_back = []
- backgrounds_front = []
-
- for muscle in exercise.muscles.all():
- if muscle.is_front:
- backgrounds_front.append('images/muscles/main/muscle-%s.svg' % muscle.id)
- else:
- backgrounds_back.append('images/muscles/main/muscle-%s.svg' % muscle.id)
-
- for muscle in exercise.muscles_secondary.all():
- if muscle.is_front:
- backgrounds_front.append('images/muscles/secondary/muscle-%s.svg' % muscle.id)
- else:
- backgrounds_back.append('images/muscles/secondary/muscle-%s.svg' % muscle.id)
-
- # Append the "main" background, with the silhouette of the human body
- # This has to happen as the last step, so it is rendered behind the muscles.
- backgrounds_front.append('images/muscles/muscular_system_front.svg')
- backgrounds_back.append('images/muscles/muscular_system_back.svg')
- backgrounds = (backgrounds_front, backgrounds_back)
-
- cache.set(cache_mapper.get_exercise_muscle_bg_key(int(id)),
- (backgrounds_front, backgrounds_back))
-
- template_data['muscle_backgrounds_front'] = backgrounds[0]
- template_data['muscle_backgrounds_back'] = backgrounds[1]
+ template_data["muscles_main_front"] = exercise.muscles.filter(is_front=True)
+ template_data["muscles_main_back"] = exercise.muscles.filter(is_front=False)
+ template_data["muscles_sec_front"] = exercise.muscles_secondary.filter(is_front=True)
+ template_data["muscles_sec_back"] = exercise.muscles_secondary.filter(is_front=False)
# If the user is logged in, load the log and prepare the entries for
# rendering in the D3 chart
diff --git a/wger/manager/tests/test_weight_log.py b/wger/manager/tests/test_weight_log.py
index ddc71fc10..ebb97c960 100644
--- a/wger/manager/tests/test_weight_log.py
+++ b/wger/manager/tests/test_weight_log.py
@@ -39,6 +39,7 @@ from wger.manager.models import (
from wger.utils.cache import cache_mapper
from wger.utils.constants import WORKOUT_TAB
+
logger = logging.getLogger(__name__)
diff --git a/wger/utils/cache.py b/wger/utils/cache.py
index 428097e8a..adafa77f1 100644
--- a/wger/utils/cache.py
+++ b/wger/utils/cache.py
@@ -55,7 +55,6 @@ class CacheKeyMapper(object):
# Keys used by the cache
LANGUAGE_CACHE_KEY = 'language-{0}'
LANGUAGE_CONFIG_CACHE_KEY = 'language-config-{0}-{1}'
- EXERCISE_CACHE_KEY_MUSCLE_BG = 'exercise-muscle-bg-{0}'
INGREDIENT_CACHE_KEY = 'ingredient-{0}'
WORKOUT_CANONICAL_REPRESENTATION = 'workout-canonical-representation-{0}'
WORKOUT_LOG_LIST = 'workout-log-hash-{0}'
@@ -71,12 +70,6 @@ class CacheKeyMapper(object):
return pk
- def get_exercise_muscle_bg_key(self, param):
- """
- Return the exercise muscle background cache key
- """
- return self.EXERCISE_CACHE_KEY_MUSCLE_BG.format(self.get_pk(param))
-
def get_language_key(self, param):
"""
Return the language cache key