Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add default rounding values to the user profile

Roland Geider4bd13a99d298
diff --git a/wger/core/api/serializers.py b/wger/core/api/serializers.py
index 05505e5c8..a63fdcd34 100644
--- a/wger/core/api/serializers.py
+++ b/wger/core/api/serializers.py
@@ -59,6 +59,8 @@ class UserprofileSerializer(serializers.ModelSerializer):
'is_trustworthy',
'date_joined',
'gym',
+ 'weight_rounding',
+ 'reps_rounding',
'is_temporary',
'show_comments',
'show_english_ingredients',
diff --git a/wger/core/migrations/0018_rounding.py b/wger/core/migrations/0018_rounding.py
new file mode 100644
index 000000000..99403a5bc
--- /dev/null
+++ b/wger/core/migrations/0018_rounding.py
@@ -0,0 +1,22 @@
+# Generated by Django 4.2.16 on 2024-12-19 23:30
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ('core', '0017_language_full_name_en'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='userprofile',
+ name='reps_rounding',
+ field=models.DecimalField(decimal_places=2, default=None, max_digits=4, null=True),
+ ),
+ migrations.AddField(
+ model_name='userprofile',
+ name='weight_rounding',
+ field=models.DecimalField(decimal_places=2, default=None, max_digits=4, null=True),
+ ),
+ ]
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py
index 581adcc7d..05212e5db 100644
--- a/wger/core/models/profile.py
+++ b/wger/core/models/profile.py
@@ -153,6 +153,7 @@ by the US Department of Agriculture. It is extremely complete, with around
default=14,
validators=[MinValueValidator(1), MaxValueValidator(30)],
)
+
workout_duration = IntegerField(
verbose_name=_('Default duration of workouts'),
help_text=_(
@@ -163,6 +164,7 @@ by the US Department of Agriculture. It is extremely complete, with around
default=12,
validators=[MinValueValidator(1), MaxValueValidator(30)],
)
+
last_workout_notification = models.DateField(editable=False, blank=False, null=True)
"""
The last time the user got a workout reminder email
@@ -184,28 +186,25 @@ by the US Department of Agriculture. It is extremely complete, with around
on_delete=models.CASCADE,
)
- @property
- def is_trustworthy(self) -> bool:
- """
- Flag indicating whether the user "is trustworthy" and can submit or edit exercises
-
- At the moment the criteria are:
- - the account has existed for 3 weeks
- - the email address has been verified
- """
-
- # Superusers are always trustworthy
- if self.user.is_superuser:
- return True
-
- # Temporary users are never trustworthy
- if self.is_temporary:
- return False
-
- days_since_joined = datetime.date.today() - self.user.date_joined.date()
- minimum_account_age = settings.WGER_SETTINGS['MIN_ACCOUNT_AGE_TO_TRUST']
+ weight_rounding = models.DecimalField(
+ default=None,
+ null=True,
+ max_digits=4,
+ decimal_places=2,
+ )
+ """
+ Default rounding for weight
+ """
- return days_since_joined.days > minimum_account_age and self.email_verified
+ reps_rounding = models.DecimalField(
+ default=None,
+ null=True,
+ max_digits=4,
+ decimal_places=2,
+ )
+ """
+ Default rounding for repetitions
+ """
#
# User statistics
@@ -374,6 +373,29 @@ by the US Department of Agriculture. It is extremely complete, with around
behalf over the REST API
"""
+ @property
+ def is_trustworthy(self) -> bool:
+ """
+ Flag indicating whether the user "is trustworthy" and can submit or edit exercises
+
+ At the moment the criteria are:
+ - the account has existed for 3 weeks
+ - the email address has been verified
+ """
+
+ # Superusers are always trustworthy
+ if self.user.is_superuser:
+ return True
+
+ # Temporary users are never trustworthy
+ if self.is_temporary:
+ return False
+
+ days_since_joined = datetime.date.today() - self.user.date_joined.date()
+ minimum_account_age = settings.WGER_SETTINGS['MIN_ACCOUNT_AGE_TO_TRUST']
+
+ return days_since_joined.days > minimum_account_age and self.email_verified
+
@property
def weight(self):
"""
diff --git a/wger/manager/dataclasses.py b/wger/manager/dataclasses.py
index fe01ce562..acdc75915 100644
--- a/wger/manager/dataclasses.py
+++ b/wger/manager/dataclasses.py
@@ -58,8 +58,8 @@ class SetConfigData:
sets: int = 1
weight_unit: int | None = 1
reps_unit: int | None = 1
- weight_rounding: Decimal | int | None = 1.25
- reps_rounding: Decimal | int | None = 1
+ weight_rounding: Decimal | int | None = None
+ reps_rounding: Decimal | int | None = None
comment: str = ''
type: str = 'normal'
@@ -225,14 +225,18 @@ def round_value(
"""
Rounds a value to the nearest base
- If the base is None, the value will be returned as a Decimal object.
+ If the base is None, the value will be returned as-is
"""
- if x is None or base == 0:
+ if x is None:
return x
# If the result is an integer, remove the decimal part
- result = Decimal(x) if base is None else Decimal(base * round(Decimal(x) / Decimal(base)))
+ result = (
+ Decimal(x)
+ if base is None or base == 0
+ else Decimal(base * round(Decimal(x) / Decimal(base)))
+ )
if result == result.to_integral_value():
result = result.quantize(1, ROUND_DOWN)
diff --git a/wger/manager/models/slot_entry.py b/wger/manager/models/slot_entry.py
index 085c19dd3..56c558c7f 100644
--- a/wger/manager/models/slot_entry.py
+++ b/wger/manager/models/slot_entry.py
@@ -147,6 +147,19 @@ class SlotEntry(models.Model):
'id',
]
+ def save(self, *args, **kwargs):
+ """
+ Save the object to the database
+ """
+
+ # For new entries add default rounding if available
+ if not self.id:
+ if not self.repetition_rounding:
+ self.repetition_rounding = self.slot.day.routine.user.userprofile.reps_rounding
+ if not self.weight_rounding:
+ self.weight_rounding = self.slot.day.routine.user.userprofile.weight_rounding
+ return super().save(*args, **kwargs)
+
def get_owner_object(self):
"""
Returns the object that has owner information