Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Automatically create a session for logs

Roland Geiderba5cc26dbc82
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py
index 86fc15353..7bba11366 100644
--- a/wger/manager/api/serializers.py
+++ b/wger/manager/api/serializers.py
@@ -294,6 +294,7 @@ class SetConfigDataSerializer(serializers.Serializer):
SetData serializer
"""
+ slot_config_id = serializers.IntegerField()
exercise = serializers.IntegerField()
sets = serializers.IntegerField()
weight = serializers.DecimalField(max_digits=5, decimal_places=2)
diff --git a/wger/manager/fixtures/test-workout-session.json b/wger/manager/fixtures/test-workout-session.json
index f026c5ffe..ee697e1a6 100644
--- a/wger/manager/fixtures/test-workout-session.json
+++ b/wger/manager/fixtures/test-workout-session.json
@@ -1,54 +1,54 @@
[
- {
- "pk": 1,
- "model": "manager.workoutsession",
- "fields": {
- "impression": "2",
- "time_start": "17:00:00",
- "workout": 1,
- "time_end": "18:30:00",
- "user": 1,
- "date": "2012-10-01",
- "notes": "Notes come here"
- }
- },
- {
- "pk": 2,
- "model": "manager.workoutsession",
- "fields": {
- "impression": "3",
- "time_start": "17:00:00",
- "workout": 1,
- "time_end": "18:00:00",
- "user": 1,
- "date": "2014-01-20",
- "notes": "Even more notes and impressions"
- }
- },
- {
- "pk": 3,
- "model": "manager.workoutsession",
- "fields": {
- "impression": "3",
- "time_start": "10:00:00",
- "workout": 2,
- "time_end": "15:00:00",
- "user": 1,
- "date": "2014-01-30",
- "notes": "I spent the whole day in the gym"
- }
- },
- {
- "pk": 4,
- "model": "manager.workoutsession",
- "fields": {
- "impression": "3",
- "time_start": "10:00:00",
- "workout": 3,
- "time_end": "10:15:00",
- "user": 2,
- "date": "2014-01-30",
- "notes": "Something, something else"
- }
+ {
+ "pk": 1,
+ "model": "manager.workoutsession",
+ "fields": {
+ "impression": "2",
+ "time_start": "17:00:00",
+ "routine": 1,
+ "time_end": "18:30:00",
+ "user": 1,
+ "date": "2012-10-01",
+ "notes": "Notes come here"
}
+ },
+ {
+ "pk": 2,
+ "model": "manager.workoutsession",
+ "fields": {
+ "impression": "3",
+ "time_start": "17:00:00",
+ "routine": 1,
+ "time_end": "18:00:00",
+ "user": 1,
+ "date": "2014-01-20",
+ "notes": "Even more notes and impressions"
+ }
+ },
+ {
+ "pk": 3,
+ "model": "manager.workoutsession",
+ "fields": {
+ "impression": "3",
+ "time_start": "10:00:00",
+ "routine": 2,
+ "time_end": "15:00:00",
+ "user": 1,
+ "date": "2014-01-30",
+ "notes": "I spent the whole day in the gym"
+ }
+ },
+ {
+ "pk": 4,
+ "model": "manager.workoutsession",
+ "fields": {
+ "impression": "3",
+ "time_start": "10:00:00",
+ "routine": 3,
+ "time_end": "10:15:00",
+ "user": 2,
+ "date": "2014-01-30",
+ "notes": "Something, something else"
+ }
+ }
]
diff --git a/wger/manager/migrations/0021_flexible_routines_cleanup.py b/wger/manager/migrations/0021_flexible_routines_cleanup.py
index 362d4095e..0c8d5e3f0 100644
--- a/wger/manager/migrations/0021_flexible_routines_cleanup.py
+++ b/wger/manager/migrations/0021_flexible_routines_cleanup.py
@@ -13,6 +13,10 @@ class Migration(migrations.Migration):
model_name='workoutlog',
name='workout',
),
+ migrations.RemoveField(
+ model_name='workoutsession',
+ name='workout',
+ ),
migrations.RenameField(
model_name='workoutlog',
old_name='exercise_base',
diff --git a/wger/manager/models/log.py b/wger/manager/models/log.py
index 72d9beeff..0a1d74098 100644
--- a/wger/manager/models/log.py
+++ b/wger/manager/models/log.py
@@ -162,11 +162,9 @@ class WorkoutLog(models.Model):
"""
return self
- def get_workout_session(self, date=None):
+ def get_workout_session(self, date=None) -> WorkoutSession:
"""
Returns the corresponding workout session
-
- :return the WorkoutSession object or None if nothing was found
"""
if not date:
date = self.date
@@ -178,14 +176,26 @@ class WorkoutLog(models.Model):
def save(self, *args, **kwargs):
"""
- Reset cache
+ Plumbing
"""
+
+ # Reset cache
reset_workout_log(self.user_id, self.date.year, self.date.month, self.date.day)
+ # If there is no session for this date and routine, create one
+ if not self.session_id:
+ self.session = WorkoutSession.objects.get_or_create(
+ user=self.user,
+ date=self.date,
+ routine=self.routine,
+ )[0]
+
# If the user selected "Until Failure", do only 1 "repetition",
- # everything else doesn't make sense.
+ # anything else doesn't make sense.
if self.repetition_unit == 2:
self.reps = 1
+
+ # Save to db
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
diff --git a/wger/manager/models/session.py b/wger/manager/models/session.py
index 103961a8b..52145192e 100644
--- a/wger/manager/models/session.py
+++ b/wger/manager/models/session.py
@@ -54,15 +54,6 @@ class WorkoutSession(models.Model):
See note in weight.models.WeightEntry about why this is not editable=False
"""
- workout = models.ForeignKey(
- 'Workout',
- verbose_name=_('Workout'),
- on_delete=models.CASCADE,
- )
- """
- The workout the session belongs to
- """
-
routine = models.ForeignKey(
'Routine',
on_delete=models.CASCADE,
diff --git a/wger/manager/models/slot_config.py b/wger/manager/models/slot_config.py
index a656d724a..a97375b73 100644
--- a/wger/manager/models/slot_config.py
+++ b/wger/manager/models/slot_config.py
@@ -233,6 +233,7 @@ class SlotConfig(models.Model):
rir=self.get_rir(iteration),
rest=self.get_rest(iteration),
type=self.type,
+ slot_config_id=self.id,
)
def get_sets(self, iteration: int) -> Decimal | None:
diff --git a/wger/manager/tests/test_log_model.py b/wger/manager/tests/test_log_model.py
new file mode 100644
index 000000000..ec641770d
--- /dev/null
+++ b/wger/manager/tests/test_log_model.py
@@ -0,0 +1,62 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Standard Library
+import logging
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.manager.models import (
+ WorkoutLog,
+ WorkoutSession,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class LogModelTestCase(WgerTestCase):
+ """
+ Test some logic in the workout log model
+ """
+
+ def test_create_session(self):
+ """
+ Test that a new session is created if needed
+ """
+
+ WorkoutLog.objects.all().delete()
+ WorkoutSession.objects.all().delete()
+
+ self.assertEqual(WorkoutSession.objects.count(), 0)
+
+ WorkoutLog(
+ user_id=1,
+ exercise_id=1,
+ weight=10,
+ reps=10,
+ ).save()
+
+ self.assertEqual(WorkoutSession.objects.count(), 1)
+
+ def test_dont_create_session(self):
+ """
+ Test that no session is created if the log already has one
+ """
+
+ self.assertEqual(WorkoutSession.objects.count(), 4)
+
+ log = WorkoutLog.objects.get(pk=1)
+ log.session_id = 1
+
+ self.assertEqual(WorkoutSession.objects.count(), 4)
diff --git a/wger/manager/tests/test_weight_log.py b/wger/manager/tests/test_weight_log.py
index 0e30f0a3b..16ab1eda6 100644
--- a/wger/manager/tests/test_weight_log.py
+++ b/wger/manager/tests/test_weight_log.py
@@ -37,7 +37,6 @@ from wger.manager.models import (
WorkoutSession,
)
from wger.utils.cache import cache_mapper
-from wger.utils.constants import WORKOUT_TAB
logger = logging.getLogger(__name__)