Self hosted FLOSS fitness/workout, nutrition and weight tracker
Return muscle images from model
Kevin Gengea0e131560b1
diff --git a/AUTHORS.rst b/AUTHORS.rstindex b8dde260c..f7c280e5b 100644--- a/AUTHORS.rst+++ b/AUTHORS.rst@@ -38,6 +38,7 @@ Developers* Hita Kambhamettu - https://github.com/Hita-K* Derek Li - https://github.com/derekli17* Noah Pinter - https://github.com/nopinter+* Kevin Geng - https://github.com/gengkevTranslators-----------diff --git a/wger/core/templates/tags/muscles.html b/wger/core/templates/tags/muscles.htmlindex cd36bda5d..15a35dae7 100644--- a/wger/core/templates/tags/muscles.html+++ b/wger/core/templates/tags/muscles.html@@ -1,6 +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 %};">+ style="background-image: {% for background in backgrounds %}url({{ background }}){% if not forloop.last %},{% endif %}{% endfor %};"></div>{% endif %}diff --git a/wger/core/templatetags/wger_extras.py b/wger/core/templatetags/wger_extras.pyindex 11b580b24..e1628a0bc 100644--- a/wger/core/templatetags/wger_extras.py+++ b/wger/core/templatetags/wger_extras.py@@ -18,6 +18,7 @@from django import templatefrom django.conf import settingsfrom django.db.models import QuerySet+from django.templatetags.static import staticfrom django.utils.html import strip_spaces_between_tagsfrom django.utils.safestring import mark_safefrom django.utils.translation import (@@ -131,9 +132,9 @@ def render_muscles(muscles=None, muscles_sec=None):except IndexError:front_back = "front" if out_sec[0].is_front else "back"- backgrounds = [f"images/muscles/main/muscle-{i.id}.svg" for i in out_main] \- + [f"images/muscles/secondary/muscle-{i.id}.svg" for i in out_sec] \- + [f"images/muscles/muscular_system_{front_back}.svg"]+ backgrounds = [i.image_url_main for i in out_main] \+ + [i.image_url_secondary for i in out_sec] \+ + [static(f"images/muscles/muscular_system_{front_back}.svg")]return {"backgrounds": backgrounds,"empty": False}diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.pyindex c251193b4..ac330d5ce 100644--- a/wger/exercises/api/serializers.py+++ b/wger/exercises/api/serializers.py@@ -107,4 +107,4 @@ class MuscleSerializer(serializers.ModelSerializer):"""class Meta:model = Muscle- fields = '__all__'+ fields = ['name', 'is_front', 'image_url_main', 'image_url_secondary']diff --git a/wger/exercises/models.py b/wger/exercises/models.pyindex 01769c222..8dbd1ce7b 100644--- a/wger/exercises/models.py+++ b/wger/exercises/models.py@@ -27,6 +27,7 @@ from django.core import mailfrom django.core.validators import MinLengthValidatorfrom django.db import modelsfrom django.template.loader import render_to_string+from django.templatetags.static import staticfrom django.urls import reversefrom django.utils import translationfrom django.utils.text import slugify@@ -68,6 +69,16 @@ class Muscle(models.Model):class Meta:ordering = ["name", ]+ # Image to use when displaying this as a main muscle in an exercise+ @property+ def image_url_main(self):+ return static(f"images/muscles/main/muscle-{self.id}.svg")++ # Image to use when displaying this as a secondary muscle in an exercise+ @property+ def image_url_secondary(self):+ return static(f"images/muscles/secondary/muscle-{self.id}.svg")+def __str__(self):"""Return a more human-readable representationdiff --git a/wger/exercises/tests/test_muscles.py b/wger/exercises/tests/test_muscles.pyindex b2e49e7fb..cb9530922 100644--- a/wger/exercises/tests/test_muscles.py+++ b/wger/exercises/tests/test_muscles.py@@ -41,6 +41,12 @@ class MuscleRepresentationTestCase(WgerTestCase):"""self.assertEqual("{0}".format(Muscle.objects.get(pk=1)), 'Anterior testoid')+ # Check image URL properties+ self.assertIn("images/muscles/main/muscle-2.svg",+ Muscle.objects.get(pk=2).image_url_main)+ self.assertIn("images/muscles/secondary/muscle-1.svg",+ Muscle.objects.get(pk=1).image_url_secondary)+class MuscleAdminOverviewTest(WgerAccessTestCase):"""@@ -148,3 +154,14 @@ class MuscleApiTestCase(api_base_test.ApiBaseResourceTestCase):private_resource = Falsedata = {'name': 'The name','is_front': True}++ def test_get_detail(self):+ super().test_get_detail()++ # Check that image URLs are present in response+ response = self.client.get(self.url_detail)+ response_object = response.json()+ self.assertIn("images/muscles/main/muscle-1.svg",+ response_object["image_url_main"])+ self.assertIn("images/muscles/secondary/muscle-1.svg",+ response_object["image_url_secondary"])