Skip to content

Commit

Permalink
Rename get_exercise
Browse files Browse the repository at this point in the history
We should prepare one big change and finally rename exercisebase to just exercise
and exercise to translation. This is now more correct and is the way it's done
on the flutter and react parts of the application anyway
  • Loading branch information
rolandgeider committed Jan 6, 2024
1 parent 3447b51 commit 2726ce2
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 21 deletions.
10 changes: 6 additions & 4 deletions wger/exercises/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Standard Library
import datetime
import uuid
Expand Down Expand Up @@ -131,7 +132,7 @@ def __str__(self):
"""
Return a more human-readable representation
"""
return f"base {self.uuid} ({self.get_exercise().name})"
return f"base {self.uuid} ({self.get_translation()})"

def get_absolute_url(self):
"""
Expand Down Expand Up @@ -179,7 +180,8 @@ def last_update_global(self):
The latest update datetime of all exercises, videos and images.
"""
return max(
self.last_update, *[image.last_update for image in self.exerciseimage_set.all()],
self.last_update,
*[image.last_update for image in self.exerciseimage_set.all()],
*[video.last_update for video in self.exercisevideo_set.all()],
*[translation.last_update for translation in self.exercises.all()],
datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
Expand Down Expand Up @@ -208,7 +210,7 @@ def base_variations(self):
return []
return self.variations.exercisebase_set.filter(~Q(id=self.id))

def get_exercise(self, language: Optional[str] = None):
def get_translation(self, language: Optional[str] = None):
"""
Returns the exercise for the given language. If the language is not
available, return the English translation.
Expand Down Expand Up @@ -251,7 +253,7 @@ def delete(self, using=None, keep_parents=False, replace_by: str = None):
log = DeletionLog(
model_type=DeletionLog.MODEL_BASE,
uuid=self.uuid,
comment=f"Exercise base of {self.get_exercise(ENGLISH_SHORT_NAME)}",
comment=f"Exercise base of {self.get_translation(ENGLISH_SHORT_NAME)}",
replaced_by=replace_by,
)
log.save()
Expand Down
1 change: 1 addition & 0 deletions wger/exercises/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def sync_exercises(

Alias.objects.filter(exercise=translation).delete()
for alias in translation_data['aliases']:
print(alias)
Alias.objects.get_or_create(exercise=translation, alias=alias['alias'])

print_fn('')
Expand Down
6 changes: 3 additions & 3 deletions wger/exercises/tests/test_exercise_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ def test_language_utils_translation_exists(self):
"""
Test that the base correctly returns translated exercises
"""
exercise = ExerciseBase.objects.get(pk=1).get_exercise('de')
exercise = ExerciseBase.objects.get(pk=1).get_translation('de')
self.assertEqual(exercise.name, 'An exercise')

def test_language_utils_no_translation_exists(self):
"""
Test that the base correctly returns the English translation if the
requested language does not exist
"""
exercise = ExerciseBase.objects.get(pk=1).get_exercise('fr')
exercise = ExerciseBase.objects.get(pk=1).get_translation('fr')
self.assertEqual(exercise.name, 'Test exercise 123')

def test_language_utils_no_translation_fallback(self):
"""
Test that the base correctly returns the first translation if for whatever
reason English is not available
"""
exercise = ExerciseBase.objects.get(pk=2).get_exercise('pt')
exercise = ExerciseBase.objects.get(pk=2).get_translation('pt')

self.assertEqual(exercise.name, 'Very cool exercise')

Expand Down
6 changes: 3 additions & 3 deletions wger/exercises/tests/test_exercise_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ def test_access_date(self):
)

def test_exercise_en(self):
translation = self.exercise.get_exercise()
translation = self.exercise.get_translation()
self.assertEqual(translation.language.short_name, 'en')

def test_get_exercise_fr(self):
translation = self.exercise.get_exercise('fr')
translation = self.exercise.get_translation('fr')
self.assertEqual(translation.language.short_name, 'fr')

def test_get_exercise_unknown(self):
translation = self.exercise.get_exercise('kg')
translation = self.exercise.get_translation('kg')
self.assertEqual(translation.language.short_name, 'en')

def test_get_languages(self):
Expand Down
12 changes: 6 additions & 6 deletions wger/exercises/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def test_exercise_sync(self, mock_request):
self.assertEqual([m.id for m in new_base.muscles.all()], [2])
self.assertEqual([m.id for m in new_base.muscles_secondary.all()], [4])

translation_de = new_base.get_exercise('de')
translation_de = new_base.get_translation('de')
self.assertEqual(translation_de.language_id, 1)
self.assertEqual(translation_de.name, 'Zweihandiges Kettlebell')
self.assertEqual(translation_de.description, 'Hier könnte Ihre Werbung stehen!')
Expand All @@ -699,7 +699,7 @@ def test_exercise_sync(self, mock_request):
'Wichtig die Übung richtig zu machen',
)

translation_en = new_base.get_exercise('en')
translation_en = new_base.get_translation('en')
self.assertEqual(translation_en.language_id, 2)
self.assertEqual(translation_en.name, '2 Handed Kettlebell Swing')
self.assertEqual(translation_en.description, 'TBD')
Expand All @@ -708,15 +708,15 @@ def test_exercise_sync(self, mock_request):
base = ExerciseBase.objects.get(uuid='ae3328ba-9a35-4731-bc23-5da50720c5aa')
self.assertEqual(base.category_id, 3)

translation_de = base.get_exercise('de')
translation_de = base.get_translation('de')
self.assertEqual(translation_de.name, 'A new, better, updated name')
self.assertEqual(translation_de.pk, 2)
self.assertEqual(translation_de.alias_set.count(), 2)
self.assertEqual(translation_de.alias_set.all()[0].alias, 'yet another name')
self.assertEqual(translation_de.alias_set.all()[1].alias, 'A new alias here')
self.assertEqual(translation_de.alias_set.all()[0].alias, 'A new alias here')
self.assertEqual(translation_de.alias_set.all()[1].alias, 'yet another name')

self.assertEqual(translation_de.exercisecomment_set.count(), 1)
self.assertEqual(translation_de.exercisecomment_set.first().comment, 'Foobar')

translation_fr = base.get_exercise('fr')
translation_fr = base.get_translation('fr')
self.assertEqual(str(translation_fr.uuid), '581338a1-8e52-405b-99eb-f0724c528bc8')
2 changes: 1 addition & 1 deletion wger/manager/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def render_workout_day(day, nr_of_weeks=7, images=False, comments=False, only_ta

# Exercises
for base in set_obj.exercise_bases:
exercise = base.get_exercise()
exercise = base.get_translation()
group_exercise_marker[set_obj.id]['end'] = len(data)

# Process the settings
Expand Down
4 changes: 2 additions & 2 deletions wger/manager/templates/set/formset.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load wger_extras crispy_forms_tags i18n %}
<div id="formset-base-{{base.id}}" class="mt-3">
<div id="formset-base-{{ base.id }}" class="mt-3">
<label class="control-label fw-bold">
{{base.get_exercise.name}}
{{ base.get_translation.name }}
</label>
{{ formset.management_form }}

Expand Down
3 changes: 1 addition & 2 deletions wger/manager/views/ical.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
next_weekday,
)


logger = logging.getLogger(__name__)
"""
Exports workouts and schedules as an iCal file that can be imported to a
Expand Down Expand Up @@ -96,7 +95,7 @@ def get_events_workout(calendar, workout, duration, start_date=None):
description_list = []
for set_obj in day.set_set.all():
for base in set_obj.exercise_bases:
description_list.append(str(base.get_exercise()))
description_list.append(str(base.get_translation()))
description = ', '.join(description_list) if description_list else day.description

# Make an event for each weekday
Expand Down

0 comments on commit 2726ce2

Please sign in to comment.