Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Show the exercise's main image on exercise list (mobile)

Roland Geider97cad1220692
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index e2146b5d2..c252389e8 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -253,6 +253,17 @@ class Exercise(models.Model):
# Own methods
#
+ @property
+ def main_image(self):
+ '''
+ Return the main image for the exercise or None if nothing is found
+ '''
+ has_image = self.exerciseimage_set.exists()
+ image = None
+ if has_image:
+ image = self.exerciseimage_set.filter(is_main=True)[0]
+ return image
+
def get_owner_object(self):
'''
Exercise has no owner information
@@ -329,6 +340,10 @@ class ExerciseImage(models.Model):
ExerciseImage.objects.filter(exercise=self.exercise).update(is_main=False)
self.is_main = True
+ # If the exercise has only one image, mark it as main
+ if not ExerciseImage.objects.filter(exercise=self.exercise).filter(is_main=False).count():
+ self.is_main = True
+
super(ExerciseImage, self).save(*args, **kwargs)
diff --git a/wger/exercises/templates/mobile/overview.html b/wger/exercises/templates/mobile/overview.html
index 17a05642c..c3da1fb49 100644
--- a/wger/exercises/templates/mobile/overview.html
+++ b/wger/exercises/templates/mobile/overview.html
@@ -2,6 +2,7 @@
{% load i18n %}
{% load staticfiles %}
{% load wger_extras %}
+{% load thumbnail %}
<!--
Title
@@ -21,7 +22,13 @@
<ul data-role="listview">
{% for exercise in category|get_active_exercises:active_languages %}
- <li><a href="{{ exercise.get_absolute_url }}" data-ajax="false">{{ exercise.name }}</a></li>
+ <li>
+ <a href="{{ exercise.get_absolute_url }}" data-ajax="false">
+ {% if exercise.main_image.image %}
+ <img src="{{ exercise.main_image.image|thumbnail_url:'thumbnail_cropped' }}">
+ {% endif %}
+ {{ exercise.name }}</a>
+ </li>
{% endfor %}
</ul>
</div>
@@ -46,7 +53,13 @@
id="exercise_search_form">
{% for category in categories %}
{% for exercise in category.exercise_set.all %}
- <li><a href="{{ exercise.get_absolute_url }}">{{ exercise.name }}</a></li>
+ <li>
+ <a href="{{ exercise.get_absolute_url }}" data-ajax="false">
+ {% if exercise.main_image.image %}
+ <img src="{{ exercise.main_image.image|thumbnail_url:'thumbnail_cropped' }}">
+ {% endif %}
+ {{ exercise.name }}</a>
+ </li>
{% endfor %}
{% endfor %}
</ul>