Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Collect the body weight category lookup in one place

Roland Geiderc87d76b9001d
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py
index f96be2bb8..5fe482b84 100644
--- a/wger/core/models/profile.py
+++ b/wger/core/models/profile.py
@@ -40,7 +40,6 @@ from wger.measurements.models import (
Category,
Measurement,
)
-from wger.measurements.models.category import MetricType
from wger.utils.constants import TWOPLACES
from wger.utils.units import (
AbstractHeight,
@@ -528,12 +527,7 @@ class UserProfile(models.Model):
"""
Create a new weight entry and return it
"""
- category = Category.get_or_create_official(
- self.user,
- MetricType.BODY_WEIGHT,
- name='Body weight',
- unit=self.weight_unit,
- )
+ category = Category.get_or_create_body_weight(self.user, unit=self.weight_unit)
entry = Measurement(
category=category,
value=weight,
diff --git a/wger/core/signals.py b/wger/core/signals.py
index 9a83be4cd..473af7a9a 100644
--- a/wger/core/signals.py
+++ b/wger/core/signals.py
@@ -32,7 +32,6 @@ from wger.core.models import (
UserProfile,
)
from wger.measurements.models import Category
-from wger.measurements.models.category import MetricType
from wger.utils.helpers import disable_for_loaddata
@@ -43,12 +42,7 @@ def create_user_profile(sender, instance, created, **kwargs):
"""
if created:
profile = UserProfile.objects.create(user=instance)
- Category.get_or_create_official(
- instance,
- MetricType.BODY_WEIGHT,
- name='Body weight',
- unit=profile.weight_unit,
- )
+ Category.get_or_create_body_weight(instance, unit=profile.weight_unit)
@disable_for_loaddata
diff --git a/wger/core/tests/test_temporary_users.py b/wger/core/tests/test_temporary_users.py
index 9e4d0181c..5bac0ab90 100644
--- a/wger/core/tests/test_temporary_users.py
+++ b/wger/core/tests/test_temporary_users.py
@@ -42,7 +42,6 @@ from wger.measurements.models import (
Category,
Measurement,
)
-from wger.measurements.models.category import MetricType
class DemoUserTestCase(WgerTestCase):
@@ -111,12 +110,7 @@ class DemoUserTestCase(WgerTestCase):
self.assertEqual(self.count_temp_users(), 2)
user = User.objects.get(pk=4)
- category = Category.get_or_create_official(
- user,
- MetricType.BODY_WEIGHT,
- name='Body weight',
- unit='kg',
- )
+ category = Category.get_or_create_body_weight(user, unit='kg')
temp = []
for i in range(1, 5):
creation_date = timezone.now() - datetime.timedelta(days=i)
diff --git a/wger/measurements/management/commands/dummy-generator-body-weight.py b/wger/measurements/management/commands/dummy-generator-body-weight.py
index 1b986c1cd..4f0591a6c 100644
--- a/wger/measurements/management/commands/dummy-generator-body-weight.py
+++ b/wger/measurements/management/commands/dummy-generator-body-weight.py
@@ -27,7 +27,6 @@ from wger.measurements.models import (
Category,
Measurement,
)
-from wger.measurements.models.category import MetricType
logger = logging.getLogger(__name__)
@@ -72,10 +71,8 @@ class Command(BaseCommand):
new_entries = []
self.stdout.write(f' - generating for {user.username}')
- category = Category.get_or_create_official(
+ category = Category.get_or_create_body_weight(
user,
- MetricType.BODY_WEIGHT,
- name='Body weight',
unit=user.userprofile.weight_unit,
)
existing_entries = [i.date for i in Measurement.objects.filter(category=category)]
diff --git a/wger/measurements/management/commands/dummy-generator-health-measurements.py b/wger/measurements/management/commands/dummy-generator-health-measurements.py
index 9f7c937fa..2efe6d677 100644
--- a/wger/measurements/management/commands/dummy-generator-health-measurements.py
+++ b/wger/measurements/management/commands/dummy-generator-health-measurements.py
@@ -267,12 +267,7 @@ class Command(BaseCommand):
metric_type, name, unit = CATEGORIES[metric]
if metric_type == MetricType.BODY_WEIGHT:
- return Category.get_or_create_official(
- user,
- metric_type,
- name=name,
- unit=user.userprofile.weight_unit,
- )
+ return Category.get_or_create_body_weight(user, unit=user.userprofile.weight_unit)
category, _ = Category.objects.get_or_create(
id=Category.deterministic_id(user.pk, metric_type),
diff --git a/wger/measurements/models/category.py b/wger/measurements/models/category.py
index f889379ba..98b2d4572 100644
--- a/wger/measurements/models/category.py
+++ b/wger/measurements/models/category.py
@@ -307,3 +307,23 @@ class Category(models.Model):
defaults={'name': name, 'unit': unit},
)
return category
+
+ @classmethod
+ def get_or_create_body_weight(cls, user, *, unit: str):
+ """
+ Returns the user's official body weight category.
+
+ Everything writing a body weight goes through here (signals, profile,
+ the legacy weight endpoint, the CSV import), so the name a category is
+ created with is decided in one place.
+
+ The unit is passed in rather than read off the profile: every caller
+ already holds it, and the profile instance in hand is the one that
+ knows about unsaved changes.
+ """
+ return cls.get_or_create_official(
+ user,
+ MetricType.BODY_WEIGHT,
+ name='Body weight',
+ unit=unit,
+ )
diff --git a/wger/weight/api/views.py b/wger/weight/api/views.py
index cde60b612..b162ff8c9 100644
--- a/wger/weight/api/views.py
+++ b/wger/weight/api/views.py
@@ -23,7 +23,6 @@ from wger.measurements.models import (
Category,
Measurement,
)
-from wger.measurements.models.category import MetricType
from wger.weight.api.filtersets import WeightEntryFilterSet
from wger.weight.api.serializers import WeightEntrySerializer
@@ -57,12 +56,7 @@ class WeightEntryViewSet(viewsets.ModelViewSet):
The value is interpreted in the user's preferred weight unit.
"""
profile = self.request.user.userprofile
- category = Category.get_or_create_official(
- self.request.user,
- MetricType.BODY_WEIGHT,
- name='Body weight',
- unit=profile.weight_unit,
- )
+ category = Category.get_or_create_body_weight(self.request.user, unit=profile.weight_unit)
serializer.save(category=category, extra_data={'unit': profile.weight_unit})
def perform_update(self, serializer):
diff --git a/wger/weight/helpers.py b/wger/weight/helpers.py
index 372ea15fa..60101d5a7 100644
--- a/wger/weight/helpers.py
+++ b/wger/weight/helpers.py
@@ -89,12 +89,7 @@ def parse_weight_csv(request, cleaned_data):
# Create the valid weight entries
if distinct_weight_entries:
profile_unit = request.user.userprofile.weight_unit
- category = Category.get_or_create_official(
- request.user,
- MetricType.BODY_WEIGHT,
- name='Body weight',
- unit=profile_unit,
- )
+ category = Category.get_or_create_body_weight(request.user, unit=profile_unit)
for date, weight in distinct_weight_entries:
weight_list.append(
Measurement(