Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Move exercise base values to properties on exercise model

Roland Geider3456f6057d3c
diff --git a/manage.py b/manage.py
index c35ed859a..4ebf00873 100644
--- a/manage.py
+++ b/manage.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# Standard Library
import sys
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 4216254e5..db2a9afda 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -106,17 +106,10 @@ class ExerciseSerializer(serializers.ModelSerializer):
The fields from the new ExerciseBase are retrieved here as to retain
compatibility with the old model where all the fields where in Exercise.
"""
- category = serializers.PrimaryKeyRelatedField(source='exercise_base.category',
- read_only=True)
- muscles = serializers.PrimaryKeyRelatedField(source='exercise_base.muscles',
- many=True,
- read_only=True)
- muscles_secondary = serializers.PrimaryKeyRelatedField(source='exercise_base.muscles_secondary',
- many=True,
- read_only=True)
- equipment = serializers.PrimaryKeyRelatedField(source='exercise_base.equipment',
- many=True,
- read_only=True)
+ category = serializers.PrimaryKeyRelatedField(read_only=True)
+ muscles = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
+ muscles_secondary = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
+ equipment = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Exercise
@@ -141,10 +134,10 @@ class ExerciseInfoSerializer(serializers.ModelSerializer):
images = ExerciseImageSerializer(source='exerciseimage_set', many=True)
comments = ExerciseCommentSerializer(source='exercisecomment_set', many=True)
- category = ExerciseCategorySerializer(source='exercise_base.category')
- muscles = MuscleSerializer(source='exercise_base.muscles', many=True)
- muscles_secondary = MuscleSerializer(source='exercise_base.muscles_secondary', many=True)
- equipment = EquipmentSerializer(source='exercise_base.equipment', many=True)
+ category = ExerciseCategorySerializer(read_only=True)
+ muscles = MuscleSerializer(many=True, read_only=True)
+ muscles_secondary = MuscleSerializer(many=True, read_only=True)
+ equipment = EquipmentSerializer(many=True, read_only=True)
class Meta:
model = Exercise
@@ -162,5 +155,4 @@ class ExerciseInfoSerializer(serializers.ModelSerializer):
"license",
"license_author",
"images",
- "variations",
"comments"]
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index dc778506f..e5f1d4905 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -85,7 +85,6 @@ class ExerciseViewSet(viewsets.ModelViewSet):
'language',
'status',
'name',
- 'variations',
'license',
'license_author')
@@ -135,7 +134,7 @@ def search(request):
'data': {
'id': exercise.id,
'name': exercise.name,
- 'category': _(exercise.exercise_base.category.name),
+ 'category': _(exercise.category.name),
'image': image,
'image_thumbnail': thumbnail
}
@@ -161,7 +160,6 @@ class ExerciseInfoViewset(viewsets.ReadOnlyModelViewSet):
'name',
'exercise_base',
'license',
- 'variations',
'license_author')
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index 26ab429cd..710b28564 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -217,6 +217,17 @@ class ExerciseBase(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
blank=True)
"""Variations of this exercise"""
+ #
+ # Own methods
+ #
+
+ @property
+ def get_languages(self):
+ """
+ Returns the languages from the exercises tha tuse this base
+ """
+ return [exercise.language for exercise in self.exercises.all()]
+
class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
"""
@@ -319,18 +330,37 @@ class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
return self.name
#
- # Own methods
+ # Properties to expose the info from the exercise base
#
+ @property
+ def category(self):
+ return self.exercise_base.category
+ @property
+ def muscles(self):
+ return self.exercise_base.muscles
+
+ @property
+ def muscles_secondary(self):
+ return self.exercise_base.muscles_secondary
+
+ @property
+ def equipment(self):
+ return self.exercise_base.equipment
+
+ #
+ # Own methods
+ #
@property
def get_variations(self):
"""
Returns the variations for this exercise
"""
out = []
- for variation in self.exercise_base.variations.exercisebase_set.all():
- for exercise in variation.exercises.filter(language=self.language).accepted():
- out.append(exercise)
+ if self.exercise_base.variations:
+ for variation in self.exercise_base.variations.exercisebase_set.all():
+ for exercise in variation.exercises.filter(language=self.language).accepted():
+ out.append(exercise)
return out
@property
diff --git a/wger/exercises/templates/exercise/email_correction.tpl b/wger/exercises/templates/exercise/email_correction.tpl
index 4addff4b3..f25e1cfaf 100644
--- a/wger/exercises/templates/exercise/email_correction.tpl
+++ b/wger/exercises/templates/exercise/email_correction.tpl
@@ -15,7 +15,7 @@ Changes to the exercise:
- New: {{ form_data.name }}
* Category:
- - Original: {{ exercise.exercise_base.category }}
+ - Original: {{ exercise.category }}
- New: {{ form_data.category }}
* Description:
@@ -23,15 +23,15 @@ Changes to the exercise:
- New: {{ form_data.description }}
* Muscles (primary):
- - Original: {{ exercise.exercise_base.muscles.all|join:", " }}
+ - Original: {{ exercise.muscles.all|join:", " }}
- New: {{ form_data.muscles.all|join:", " }}
* Muscles (secondary):
- - Original: {{ exercise.exercise_base.muscles_secondary.all|join:", " }}
+ - Original: {{ exercise.muscles_secondary.all|join:", " }}
- New: {{ form_data.muscles_secondary.all|join:", " }}
* Equipment:
- - Original: {{ exercise.exercise_base.equipment.all|join:", " }}
+ - Original: {{ exercise.equipment.all|join:", " }}
- New: {{ form_data.equipment.all|join:", " }}
* License:
@@ -40,4 +40,4 @@ Changes to the exercise:
* Author:
- Original: {{ exercise.license_author }}
- - New: {{ form_data.license_author }}
\ No newline at end of file
+ - New: {{ form_data.license_author }}
diff --git a/wger/exercises/templates/exercise/overview.html b/wger/exercises/templates/exercise/overview.html
index 270d9d488..7ddaf4ecf 100644
--- a/wger/exercises/templates/exercise/overview.html
+++ b/wger/exercises/templates/exercise/overview.html
@@ -69,7 +69,7 @@ $(document).ready(function() {
<p class="media-heading h5">{{ exercise.name }}</p>
<span class="text-muted small">
- {{ exercise.exercise_base.equipment.all|join:", " }}
+ {{ exercise.equipment.all|join:", " }}
<!--
{% trans "Main muscles" %}: {{ exercise.exercise_base.muscles.all|join:", " }}
<br>
diff --git a/wger/exercises/templates/exercise/view.html b/wger/exercises/templates/exercise/view.html
index 6a2cab327..04e5cd94f 100644
--- a/wger/exercises/templates/exercise/view.html
+++ b/wger/exercises/templates/exercise/view.html
@@ -74,11 +74,11 @@ $(document).ready(function() {
<h5 class="mt-4">{% trans "Category" %}:</h5>
-{% trans exercise.exercise_base.category.name %}
+{% trans exercise.category.name %}
-{% if exercise.exercise_base.equipment.all %}
+{% if exercise.equipment.all %}
<h5 class="mt-4">{% trans "Equipment" %}:</h5>
- {% for equipment in exercise.exercise_base.equipment.all %}
+ {% for equipment in exercise.equipment.all %}
{% trans equipment.name %}
{% if not forloop.last %}, {% endif %}
{% endfor %}
diff --git a/wger/exercises/templates/muscles/overview.html b/wger/exercises/templates/muscles/overview.html
index 3d167b237..05c5ee752 100644
--- a/wger/exercises/templates/muscles/overview.html
+++ b/wger/exercises/templates/muscles/overview.html
@@ -66,7 +66,7 @@
<div class="media-body">
<p class="media-heading h5">{{ exercise.name }}</p>
<span class="text-muted small">
- {% trans exercise.exercise_base.category.name %}
+ {% trans exercise.category.name %}
<!--
{% trans "Main muscles" %}: {{ exercise.exercise_base.muscles.all|join:", " }}
<br>
diff --git a/wger/exercises/tests/test_corrected_exercise.py b/wger/exercises/tests/test_corrected_exercise.py
index 332adfa07..ea79e0a8a 100644
--- a/wger/exercises/tests/test_corrected_exercise.py
+++ b/wger/exercises/tests/test_corrected_exercise.py
@@ -50,10 +50,10 @@ class ExercisesCorrectionTestCase(WgerTestCase):
exercise = Exercise.objects.get(pk=1)
self.assertEqual(exercise.name, 'An exercise')
self.assertEqual(exercise.description, '')
- self.assertEqual(exercise.exercise_base.category_id, 2)
+ self.assertEqual(exercise.category.id, 2)
self.assertEqual(exercise.language_id, 1)
- self.assertEqual([i.pk for i in exercise.exercise_base.muscles.all()], [1, 2])
- self.assertEqual([i.pk for i in exercise.exercise_base.muscles_secondary.all()], [3])
+ self.assertEqual([i.pk for i in exercise.muscles.all()], [1, 2])
+ self.assertEqual([i.pk for i in exercise.muscles_secondary.all()], [3])
# Check the notification email
if fail:
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index f31593962..47c705ad0 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -26,6 +26,7 @@ from django.template import (
from django.urls import reverse
# wger
+from wger.core.tests import api_base_test
from wger.core.tests.base_testcase import (
STATUS_CODES_FAIL,
WgerDeleteTestCase,
@@ -691,21 +692,25 @@ class WorkoutCacheTestCase(WgerTestCase):
# TODO: fix test, all registered users can upload exercises
-# class ExerciseApiTestCase(api_base_test.ApiBaseResourceTestCase):
-# """
-# Tests the exercise overview resource
-# """
-# pk = 1
-# resource = Exercise
-# private_resource = False
-# data = {"category": "1",
-# "comments": [],
-# "creation_date": "2013-01-01",
-# "description": "Something here",
-# "id": 1,
-# "language": "2",
-# "muscles": [
-# "1"
-# ],
-# "name": "foobar",
-# "status": "5"}
+class ExerciseApiTestCase(api_base_test.BaseTestCase,
+ api_base_test.ApiBaseTestCase,
+ api_base_test.ApiGetTestCase):
+ """
+ Tests the exercise overview resource
+ """
+ pk = 1
+ resource = Exercise
+ private_resource = False
+
+
+class ExerciseInfoApiTestCase(api_base_test.BaseTestCase,
+ api_base_test.ApiBaseTestCase,
+ api_base_test.ApiGetTestCase):
+ """
+ Tests the exercise info resource
+ """
+ pk = 1
+ private_resource = False
+
+ def get_resource_name(self):
+ return 'exerciseinfo'
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index 3a1ec1220..a849c78f1 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -70,6 +70,7 @@ from crispy_forms.layout import (
# wger
from wger.config.models import LanguageConfig
+from wger.core.models import License
from wger.exercises.models import (
Equipment,
Exercise,
@@ -138,14 +139,13 @@ def view(request, id, slug=None):
template_data['show_shariff'] = True
exercise = get_object_or_404(Exercise, pk=id)
- exercise_base = exercise.exercise_base
template_data['exercise'] = exercise
- template_data["muscles_main_front"] = exercise_base.muscles.filter(is_front=True)
- template_data["muscles_main_back"] = exercise_base.muscles.filter(is_front=False)
- template_data["muscles_sec_front"] = exercise_base.muscles_secondary.filter(is_front=True)
- template_data["muscles_sec_back"] = exercise_base.muscles_secondary.filter(is_front=False)
+ 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
@@ -307,11 +307,17 @@ class ExerciseAddView(ExercisesEditAddView, LoginRequiredMixin, CreateView):
for elem in form.cleaned_data['muscles_secondary'].all():
existing = existing.filter(equipment=elem)
if not existing:
+ print(form.cleaned_data)
+ print('-------------------------')
exercise_base = ExerciseBase.objects.create(
- category=ExerciseCategory.objects.get(name=form.cleaned_data['category']))
+ category=ExerciseCategory.objects.get(name=form.cleaned_data['category']),
+ license=License.objects.get(id=form.cleaned_data['license']),
+ license_author=License.objects.get(id=form.cleaned_data['license_author']),
+ )
exercise_base.equipment.set(form.cleaned_data['equipment'].all())
exercise_base.muscles.set(form.cleaned_data['muscles'].all())
exercise_base.muscles_secondary.set(form.cleaned_data['muscles_secondary'].all())
+ exercise_base.save()
else:
exercise_base = existing.first()
diff --git a/wger/manager/models.py b/wger/manager/models.py
index 4b838bec4..110c81172 100644
--- a/wger/manager/models.py
+++ b/wger/manager/models.py
@@ -468,13 +468,13 @@ class Day(models.Model):
exercise_images_tmp = []
# Muscles for this set
- for muscle in exercise.exercise_base.muscles.all():
+ for muscle in exercise.muscles.all():
if muscle.is_front and muscle not in muscles_front:
muscles_front.append(muscle)
elif not muscle.is_front and muscle not in muscles_back:
muscles_back.append(muscle)
- for muscle in exercise.exercise_base.muscles_secondary.all():
+ for muscle in exercise.muscles_secondary.all():
if muscle.is_front and muscle not in muscles_front:
muscles_front_secondary.append(muscle)
elif not muscle.is_front and muscle.id not in muscles_back: