Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Read and write body weight through measurements everywhere

Roland Geider7c6e1d2fc698
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py
index 7863231a6..cef39553e 100644
--- a/wger/core/models/profile.py
+++ b/wger/core/models/profile.py
@@ -36,12 +36,16 @@ from allauth.account.models import EmailAddress
# wger
from wger.gym.models import Gym
+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,
AbstractWeight,
)
-from wger.weight.models import WeightEntry
# Local
from .language import Language
@@ -389,8 +393,8 @@ class UserProfile(models.Model):
more consistent with the other settings (age, height, etc.)
"""
try:
- weight = WeightEntry.objects.filter(user=self.user).latest().weight
- except WeightEntry.DoesNotExist:
+ weight = Measurement.body_weight_for(self.user).latest('date').value
+ except Measurement.DoesNotExist:
weight = 0
return weight
@@ -523,10 +527,13 @@ class UserProfile(models.Model):
"""
Create a new weight entry and return it
"""
- entry = WeightEntry()
- entry.weight = weight
- entry.user = self.user
- entry.date = timezone.now()
+ category = Category.get_or_create_official(
+ self.user,
+ MetricType.BODY_WEIGHT,
+ name='Body weight',
+ unit=self.weight_unit,
+ )
+ entry = Measurement(category=category, value=weight, date=timezone.now())
entry.save()
return entry
diff --git a/wger/core/templates/user/overview.html b/wger/core/templates/user/overview.html
index 97bd270d4..e13ee1e2e 100644
--- a/wger/core/templates/user/overview.html
+++ b/wger/core/templates/user/overview.html
@@ -142,7 +142,7 @@
{{weight.date}}
</td>
<td>
- {{weight.weight}} {% trans_weight_unit 'kg' current_user %}
+ {{weight.value}} {% trans_weight_unit 'kg' current_user %}
</td>
</tr>
{% empty %}
diff --git a/wger/core/tests/powersync_base_test.py b/wger/core/tests/powersync_base_test.py
index 82783e970..72ef19c36 100644
--- a/wger/core/tests/powersync_base_test.py
+++ b/wger/core/tests/powersync_base_test.py
@@ -98,7 +98,7 @@ class PowerSyncCreateTestCase:
Return the value of the field that the client-supplied ``id`` should
round-trip into. Defaults to ``pk`` (correct for UUID-PK models like
Category). Models with an integer PK and a separate UUID column
- (WeightEntry) override this to return ``obj.uuid``.
+ override this to return ``obj.uuid``.
"""
return obj.pk
diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.py
index 0562e2f8c..79f5efc2d 100644
--- a/wger/core/tests/test_preferences.py
+++ b/wger/core/tests/test_preferences.py
@@ -25,7 +25,7 @@ from django.utils import timezone
# wger
from wger.core.tests.base_testcase import WgerTestCase
from wger.utils.constants import TWOPLACES
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
logger = logging.getLogger(__name__)
@@ -238,10 +238,10 @@ class UserBodyweightTestCase(WgerTestCase):
Tests that a new weight entry is created
"""
user = User.objects.get(pk=2)
- count_before = WeightEntry.objects.filter(user=user).count()
+ count_before = Measurement.body_weight_for(user).count()
entry = user.userprofile.user_bodyweight(80)
- count_after = WeightEntry.objects.filter(user=user).count()
+ count_after = Measurement.body_weight_for(user).count()
self.assertEqual(count_before, count_after - 1)
self.assertEqual(entry.date.date(), timezone.now().date())
@@ -250,13 +250,13 @@ class UserBodyweightTestCase(WgerTestCase):
Tests that a new weight entry is created
"""
user = User.objects.get(pk=2)
- count_before = WeightEntry.objects.filter(user=user).count()
- last_entry = WeightEntry.objects.filter(user=user).latest()
+ count_before = Measurement.body_weight_for(user).count()
+ last_entry = Measurement.body_weight_for(user).latest('date')
last_entry.date = timezone.now() - datetime.timedelta(weeks=1)
last_entry.save()
entry = user.userprofile.user_bodyweight(80)
- count_after = WeightEntry.objects.filter(user=user).count()
+ count_after = Measurement.body_weight_for(user).count()
self.assertEqual(count_before, count_after - 1)
self.assertEqual(entry.date.date(), timezone.now().date())
@@ -265,13 +265,13 @@ class UserBodyweightTestCase(WgerTestCase):
Tests that a new weight entry is created even if others exist today
"""
user = User.objects.get(pk=2)
- count_before = WeightEntry.objects.filter(user=user).count()
- last_entry = WeightEntry.objects.filter(user=user).latest()
+ count_before = Measurement.body_weight_for(user).count()
+ last_entry = Measurement.body_weight_for(user).latest('date')
last_entry.date = timezone.now() - datetime.timedelta(hours=1)
last_entry.save()
entry = user.userprofile.user_bodyweight(80)
- count_after = WeightEntry.objects.filter(user=user).count()
+ count_after = Measurement.body_weight_for(user).count()
self.assertEqual(count_before, count_after - 1)
self.assertEqual(entry.date.date(), timezone.now().date())
@@ -280,11 +280,11 @@ class UserBodyweightTestCase(WgerTestCase):
Tests that a new weight entry is created if there are no weight entries
"""
user = User.objects.get(pk=2)
- WeightEntry.objects.filter(user=user).delete()
+ Measurement.body_weight_for(user).delete()
- count_before = WeightEntry.objects.filter(user=user).count()
+ count_before = Measurement.body_weight_for(user).count()
entry = user.userprofile.user_bodyweight(80)
- count_after = WeightEntry.objects.filter(user=user).count()
+ count_after = Measurement.body_weight_for(user).count()
self.assertEqual(count_before, count_after - 1)
self.assertEqual(entry.date.date(), timezone.now().date())
@@ -300,13 +300,9 @@ class PreferencesCalculationsTestCase(WgerTestCase):
"""
self.user_login('test')
user = User.objects.get(pk=2)
- entry = WeightEntry()
- entry.date = timezone.now()
- entry.user = user
- entry.weight = 100
- entry.save()
+ entry = user.userprofile.user_bodyweight(100)
self.assertEqual(user.userprofile.weight, 100)
- entry.weight = 150
+ entry.value = 150
entry.save()
self.assertEqual(user.userprofile.weight, 150)
@@ -316,7 +312,7 @@ class PreferencesCalculationsTestCase(WgerTestCase):
"""
self.user_login('test')
user = User.objects.get(pk=2)
- WeightEntry.objects.filter(user=user).delete()
+ Measurement.body_weight_for(user).delete()
self.assertEqual(user.userprofile.weight, 0)
def test_bmi(self):
diff --git a/wger/core/tests/test_temporary_users.py b/wger/core/tests/test_temporary_users.py
index 594c169e0..9e4d0181c 100644
--- a/wger/core/tests/test_temporary_users.py
+++ b/wger/core/tests/test_temporary_users.py
@@ -38,7 +38,11 @@ from wger.nutrition.models import (
Meal,
NutritionPlan,
)
-from wger.weight.models import WeightEntry
+from wger.measurements.models import (
+ Category,
+ Measurement,
+)
+from wger.measurements.models.category import MetricType
class DemoUserTestCase(WgerTestCase):
@@ -96,7 +100,7 @@ class DemoUserTestCase(WgerTestCase):
self.assertEqual(Meal.objects.filter(plan__user=user).count(), 20)
# Body weight
- self.assertEqual(WeightEntry.objects.filter(user=user).count(), 40)
+ self.assertEqual(Measurement.body_weight_for(user).count(), 40)
def test_demo_data_body_weight(self):
"""
@@ -107,20 +111,26 @@ 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',
+ )
temp = []
for i in range(1, 5):
creation_date = timezone.now() - datetime.timedelta(days=i)
- entry = WeightEntry(
- user=user,
- weight=80 + 0.5 * i + random.randint(1, 3),
+ entry = Measurement(
+ category=category,
+ value=80 + 0.5 * i + random.randint(1, 3),
date=creation_date,
)
temp.append(entry)
- WeightEntry.objects.bulk_create(temp)
+ Measurement.objects.bulk_create(temp)
create_demo_entries(user)
# Body weight
- self.assertEqual(WeightEntry.objects.filter(user=user).count(), 44)
+ self.assertEqual(Measurement.body_weight_for(user).count(), 44)
def test_demo_user(self):
"""
diff --git a/wger/core/views/user.py b/wger/core/views/user.py
index 8a2e87d3b..04c2dcd86 100644
--- a/wger/core/views/user.py
+++ b/wger/core/views/user.py
@@ -118,7 +118,7 @@ from wger.utils.headless_long_lived import (
revoke_all_long_lived_sessions,
revoke_long_lived_session,
)
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
logger = logging.getLogger(__name__)
@@ -660,9 +660,7 @@ class UserDetailView(LoginRequiredMixin, WgerMultiplePermissionRequiredMixin, De
}
)
context['routine_data'] = out
- context['weight_entries'] = WeightEntry.objects.filter(user=self.object).order_by('-date')[
- :5
- ]
+ context['weight_entries'] = Measurement.body_weight_for(self.object).order_by('-date')[:5]
context['nutrition_plans'] = NutritionPlan.objects.filter(user=self.object).order_by(
'-creation_date'
)[:5]
diff --git a/wger/measurements/fixtures/test-measurements.json b/wger/measurements/fixtures/test-measurements.json
index b05106262..1b5dc0678 100644
--- a/wger/measurements/fixtures/test-measurements.json
+++ b/wger/measurements/fixtures/test-measurements.json
@@ -28,7 +28,7 @@
}
},
{
- "pk": "dddddddd-dddd-dddd-dddd-0000000000b1",
+ "pk": "11111111-1111-1111-1111-000000000001",
"model": "measurements.measurement",
"fields": {
"category": "cccccccc-cccc-cccc-cccc-0000000000b0",
@@ -37,12 +37,57 @@
}
},
{
- "pk": "dddddddd-dddd-dddd-dddd-0000000000b2",
+ "pk": "11111111-1111-1111-1111-000000000002",
+ "model": "measurements.measurement",
+ "fields": {
+ "category": "cccccccc-cccc-cccc-cccc-0000000000b0",
+ "date": "2012-10-10T14:30:21.592Z",
+ "value": 77.2
+ }
+ },
+ {
+ "pk": "11111111-1111-1111-1111-000000000003",
"model": "measurements.measurement",
"fields": {
"category": "cccccccc-cccc-cccc-cccc-0000000000b0",
"date": "2012-11-01T14:30:21.592Z",
"value": 80.6
}
+ },
+ {
+ "pk": "11111111-1111-1111-1111-000000000004",
+ "model": "measurements.measurement",
+ "fields": {
+ "category": "cccccccc-cccc-cccc-cccc-0000000000b0",
+ "date": "2013-01-01T14:30:21.592Z",
+ "value": 80
+ }
+ },
+ {
+ "pk": "11111111-1111-1111-1111-000000000005",
+ "model": "measurements.measurement",
+ "fields": {
+ "category": "cccccccc-cccc-cccc-cccc-0000000000b0",
+ "date": "2013-01-10T14:30:21.592Z",
+ "value": 81
+ }
+ },
+ {
+ "pk": "11111111-1111-1111-1111-000000000006",
+ "model": "measurements.measurement",
+ "fields": {
+ "category": "cccccccc-cccc-cccc-cccc-0000000000b0",
+ "date": "2013-01-20T14:30:21.592Z",
+ "value": 82
+ }
+ },
+ {
+ "pk": "11111111-1111-1111-1111-000000000007",
+ "model": "measurements.measurement",
+ "fields": {
+ "category": "cccccccc-cccc-cccc-cccc-0000000000b0",
+ "date": "2013-01-30T14:30:21.592Z",
+ "value": 83
+ }
}
]
diff --git a/wger/measurements/management/commands/dummy-generator-measurements.py b/wger/measurements/management/commands/dummy-generator-measurements.py
index 4ac52e733..a083628db 100644
--- a/wger/measurements/management/commands/dummy-generator-measurements.py
+++ b/wger/measurements/management/commands/dummy-generator-measurements.py
@@ -71,10 +71,11 @@ class Command(BaseCommand):
new_entries = []
for user in users:
+ # Official categories (body weight) are filled by their own generator
categories = (
[Category.objects.get(pk=options['category_id'])]
if options['category_id']
- else Category.objects.filter(user=user)
+ else Category.objects.filter(user=user, is_official=False)
)
self.stdout.write(f'- processing user {user.username}')
diff --git a/wger/measurements/models/measurement.py b/wger/measurements/models/measurement.py
index a34aa2d88..504724921 100644
--- a/wger/measurements/models/measurement.py
+++ b/wger/measurements/models/measurement.py
@@ -14,6 +14,7 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
# Django
+from django.contrib.auth.models import User
from django.core.validators import (
MaxValueValidator,
MinValueValidator,
@@ -23,6 +24,7 @@ from django.utils import timezone
# wger
from wger.measurements.models import Category
+from wger.measurements.models.category import MetricType
from wger.utils.uuid import uuid7
@@ -95,3 +97,15 @@ class Measurement(models.Model):
Returns the object that has owner information
"""
return self.category
+
+ @classmethod
+ def body_weight_for(cls, user: User) -> models.QuerySet:
+ """
+ Returns the user's body weight entries: the measurements in the
+ official body-weight category
+ """
+ return cls.objects.filter(
+ category__user=user,
+ category__metric_type=MetricType.BODY_WEIGHT,
+ category__is_official=True,
+ )
diff --git a/wger/nutrition/models/plan.py b/wger/nutrition/models/plan.py
index 9c20524f4..12bc97ced 100644
--- a/wger/nutrition/models/plan.py
+++ b/wger/nutrition/models/plan.py
@@ -30,7 +30,7 @@ from wger.nutrition.consts import ENERGY_FACTOR
from wger.nutrition.helpers import NutritionalValues
from wger.utils.cache import cache_mapper
from wger.utils.uuid import uuid7
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
logger = logging.getLogger(__name__)
@@ -165,12 +165,12 @@ class NutritionPlan(models.Model):
# Per body weight
weight_entry = self.get_closest_weight_entry()
- if weight_entry and weight_entry.weight:
- result['per_kg']['protein'] = nutritional_values.protein / weight_entry.weight
+ if weight_entry and weight_entry.value:
+ result['per_kg']['protein'] = nutritional_values.protein / weight_entry.value
result['per_kg']['carbohydrates'] = (
- nutritional_values.carbohydrates / weight_entry.weight
+ nutritional_values.carbohydrates / weight_entry.value
)
- result['per_kg']['fat'] = nutritional_values.fat / weight_entry.weight
+ result['per_kg']['fat'] = nutritional_values.fat / weight_entry.value
nutritional_representation = result
cache.set(cache_mapper.get_nutrition_cache_by_key(self.pk), nutritional_representation)
@@ -184,13 +184,13 @@ class NutritionPlan(models.Model):
target = self.creation_date
target_aware = make_aware(datetime.datetime.combine(target, datetime.time()))
closest_entry_gte = (
- WeightEntry.objects.filter(user=self.user)
+ Measurement.body_weight_for(self.user)
.filter(date__gte=target_aware)
.order_by('date')
.first()
)
closest_entry_lte = (
- WeightEntry.objects.filter(user=self.user)
+ Measurement.body_weight_for(self.user)
.filter(date__lte=target_aware)
.order_by('-date')
.first()
diff --git a/wger/nutrition/tests/test_calories_calculator.py b/wger/nutrition/tests/test_calories_calculator.py
index 9c9876002..e4aa9879d 100644
--- a/wger/nutrition/tests/test_calories_calculator.py
+++ b/wger/nutrition/tests/test_calories_calculator.py
@@ -13,7 +13,6 @@
# You should have received a copy of the GNU Affero General Public License
# Standard Library
-import datetime
import decimal
import json
@@ -26,7 +25,7 @@ from django.utils import timezone
from wger.core.tests.base_testcase import WgerTestCase
from wger.nutrition.forms import BmrForm
from wger.utils.constants import TWOPLACES
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
class CaloriesCalculatorTestCase(WgerTestCase):
@@ -99,21 +98,21 @@ class CaloriesCalculatorTestCase(WgerTestCase):
user = User.objects.get(username=self.current_user)
# A new weight entry is always created
- entry1 = WeightEntry.objects.filter(user=user).latest()
+ entry1 = Measurement.body_weight_for(user).latest('date')
response = self.client.post(
reverse('nutrition:calories:bmr'), {'age': 30, 'height': 180, 'gender': 1, 'weight': 80}
)
self.assertEqual(response.status_code, 200)
- entry2 = WeightEntry.objects.filter(user=user).latest()
- self.assertEqual(entry1.weight, 83)
- self.assertEqual(entry2.weight, 80)
+ entry2 = Measurement.body_weight_for(user).latest('date')
+ self.assertEqual(entry1.value, 83)
+ self.assertEqual(entry2.value, 80)
# No existing entries
- WeightEntry.objects.filter(user=user).delete()
+ Measurement.body_weight_for(user).delete()
response = self.client.post(
reverse('nutrition:calories:bmr'), {'age': 30, 'height': 180, 'gender': 1, 'weight': 80}
)
self.assertEqual(response.status_code, 200)
- entry = WeightEntry.objects.filter(user=user).latest()
- self.assertEqual(entry.weight, 80)
+ entry = Measurement.body_weight_for(user).latest('date')
+ self.assertEqual(entry.value, 80)
self.assertEqual(entry.date.date(), timezone.now().date())
diff --git a/wger/weight/api/views.py b/wger/weight/api/views.py
index 99646d8cb..144afb8b2 100644
--- a/wger/weight/api/views.py
+++ b/wger/weight/api/views.py
@@ -48,11 +48,7 @@ class WeightEntryViewSet(viewsets.ModelViewSet):
return Measurement.objects.none()
# Measurement orders by -date, the historic weight endpoint by date
- return Measurement.objects.filter(
- category__user=self.request.user,
- category__metric_type=MetricType.BODY_WEIGHT,
- category__is_official=True,
- ).order_by('date')
+ return Measurement.body_weight_for(self.request.user).order_by('date')
def perform_create(self, serializer):
"""
diff --git a/wger/weight/helpers.py b/wger/weight/helpers.py
index f5a7984ba..9f7c3d694 100644
--- a/wger/weight/helpers.py
+++ b/wger/weight/helpers.py
@@ -25,7 +25,11 @@ import logging
from django.utils.timezone import make_aware
# wger
-from wger.weight.models import WeightEntry
+from wger.measurements.models import (
+ Category,
+ Measurement,
+)
+from wger.measurements.models.category import MetricType
logger = logging.getLogger(__name__)
@@ -53,9 +57,9 @@ def parse_weight_csv(request, cleaned_data):
datetime.datetime.strptime(row[0], cleaned_data['date_format'])
)
parsed_weight = decimal.Decimal(row[1].replace(',', '.'))
- duplicate_date_in_db = WeightEntry.objects.filter(
- date=parsed_date, user=request.user
- ).exists()
+ duplicate_date_in_db = (
+ Measurement.body_weight_for(request.user).filter(date=parsed_date).exists()
+ )
# within the list there are no duplicate dates
unique_among_csv = parsed_date not in entry_dates
@@ -75,7 +79,14 @@ def parse_weight_csv(request, cleaned_data):
break
# Create the valid weight entries
- for date, weight in distinct_weight_entries:
- weight_list.append(WeightEntry(date=date, weight=weight, user=request.user))
+ if distinct_weight_entries:
+ category = Category.get_or_create_official(
+ request.user,
+ MetricType.BODY_WEIGHT,
+ name='Body weight',
+ unit=request.user.userprofile.weight_unit,
+ )
+ for date, weight in distinct_weight_entries:
+ weight_list.append(Measurement(date=date, value=weight, category=category))
return weight_list, error_list
diff --git a/wger/weight/management/commands/dummy-generator-body-weight.py b/wger/weight/management/commands/dummy-generator-body-weight.py
index c61cb556b..1b986c1cd 100644
--- a/wger/weight/management/commands/dummy-generator-body-weight.py
+++ b/wger/weight/management/commands/dummy-generator-body-weight.py
@@ -23,7 +23,11 @@ from django.core.management.base import BaseCommand
from django.utils import timezone
# wger
-from wger.weight.models import WeightEntry
+from wger.measurements.models import (
+ Category,
+ Measurement,
+)
+from wger.measurements.models.category import MetricType
logger = logging.getLogger(__name__)
@@ -68,18 +72,24 @@ class Command(BaseCommand):
new_entries = []
self.stdout.write(f' - generating for {user.username}')
- existing_entries = [i.date for i in WeightEntry.objects.filter(user=user)]
+ category = Category.get_or_create_official(
+ user,
+ MetricType.BODY_WEIGHT,
+ name='Body weight',
+ unit=user.userprofile.weight_unit,
+ )
+ existing_entries = [i.date for i in Measurement.objects.filter(category=category)]
# Weight entries
for i in range(options['nr_entries']):
creation_date = timezone.now() - datetime.timedelta(days=i)
if creation_date not in existing_entries:
- entry = WeightEntry(
- user=user,
- weight=base_weight + 0.5 * i + random.randint(1, 3),
+ entry = Measurement(
+ category=category,
+ value=base_weight + 0.5 * i + random.randint(1, 3),
date=creation_date,
)
new_entries.append(entry)
# Bulk-create the weight entries
- WeightEntry.objects.bulk_create(new_entries)
+ Measurement.objects.bulk_create(new_entries)
diff --git a/wger/weight/management/commands/email-weight-reminder.py b/wger/weight/management/commands/email-weight-reminder.py
index c6899aaf3..02caf5e5f 100644
--- a/wger/weight/management/commands/email-weight-reminder.py
+++ b/wger/weight/management/commands/email-weight-reminder.py
@@ -12,9 +12,6 @@
#
# You should have received a copy of the GNU Affero General Public License
-# Standard Library
-import datetime
-
# Django
from django.conf import settings
from django.contrib.sites.models import Site
@@ -29,7 +26,7 @@ from django.utils.translation import gettext_lazy as _
# wger
from wger.core.models import UserProfile
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
class Command(BaseCommand):
@@ -51,12 +48,12 @@ class Command(BaseCommand):
today = timezone.now()
try:
- last_entry = WeightEntry.objects.filter(user=profile.user).latest().date
+ last_entry = Measurement.body_weight_for(profile.user).latest('date').date
datediff = (today - last_entry).days
if datediff >= profile.num_days_weight_reminder:
self.send_email(profile.user, last_entry, datediff)
- except WeightEntry.DoesNotExist:
+ except Measurement.DoesNotExist:
pass
@staticmethod
diff --git a/wger/weight/tests/test_csv_import.py b/wger/weight/tests/test_csv_import.py
index f0c8f6889..1f9929400 100644
--- a/wger/weight/tests/test_csv_import.py
+++ b/wger/weight/tests/test_csv_import.py
@@ -20,7 +20,7 @@ from django.urls import reverse
# wger
from wger.core.tests.base_testcase import WgerTestCase
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
logger = logging.getLogger(__name__)
@@ -40,7 +40,7 @@ class WeightCsvImportTestCase(WgerTestCase):
# Do a direct post request
# 1st step
- count_before = WeightEntry.objects.count()
+ count_before = Measurement.objects.count()
csv_input = """Datum Gewicht KJ
05.01.10 00:00:00 error here 111
22.01.aa 00:00:00 69,2 222
@@ -72,7 +72,7 @@ class WeightCsvImportTestCase(WgerTestCase):
},
)
- count_after = WeightEntry.objects.count()
+ count_after = Measurement.objects.count()
self.assertEqual(response.status_code, 302)
self.assertGreater(count_after, count_before)
diff --git a/wger/weight/tests/test_email_weight_reminder.py b/wger/weight/tests/test_email_weight_reminder.py
index 6474375b2..cfe299df6 100644
--- a/wger/weight/tests/test_email_weight_reminder.py
+++ b/wger/weight/tests/test_email_weight_reminder.py
@@ -13,10 +13,7 @@
# You should have received a copy of the GNU Affero General Public License
# Standard Library
-from datetime import (
- datetime,
- timedelta,
-)
+from datetime import timedelta
# Django
from django.contrib.auth.models import User
@@ -26,7 +23,10 @@ from django.utils import timezone
# wger
from wger.core.tests.base_testcase import WgerTestCase
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
+
+
+ENTRY_PK = '11111111-1111-1111-1111-000000000003'
class EmailWeightReminderTestCase(WgerTestCase):
@@ -66,7 +66,7 @@ class EmailWeightReminderTestCase(WgerTestCase):
user.email = '[email protected]'
user.save()
- weightEntry = WeightEntry.objects.filter(user=user).get(pk=3)
+ weightEntry = Measurement.body_weight_for(user).get(pk=ENTRY_PK)
weightEntry.date = timezone.now() - timedelta(days=2)
weightEntry.save()
@@ -81,7 +81,7 @@ class EmailWeightReminderTestCase(WgerTestCase):
user.email = '[email protected]'
user.save()
- weightEntry = WeightEntry.objects.filter(user=user).get(pk=3)
+ weightEntry = Measurement.body_weight_for(user).get(pk=ENTRY_PK)
weightEntry.date = timezone.now() - timedelta(days=1)
weightEntry.save()
@@ -96,7 +96,7 @@ class EmailWeightReminderTestCase(WgerTestCase):
user.email = '[email protected]'
user.save()
- weightEntry = WeightEntry.objects.filter(user=user).latest()
+ weightEntry = Measurement.body_weight_for(user).latest('date')
weightEntry.date = timezone.now()
weightEntry.save()
diff --git a/wger/weight/tests/test_entry.py b/wger/weight/tests/test_entry.py
index a13ae3a23..64a5e19d0 100644
--- a/wger/weight/tests/test_entry.py
+++ b/wger/weight/tests/test_entry.py
@@ -46,7 +46,7 @@ class WeightEntryTestCase(api_base_test.ApiBaseResourceTestCase):
Tests the weight entry overview resource
"""
- pk = 'dddddddd-dddd-dddd-dddd-0000000000b1'
+ pk = '11111111-1111-1111-1111-000000000003'
resource = Measurement
private_resource = True
date = timezone.now() - timezone.timedelta(days=25)
diff --git a/wger/weight/tests/test_generator.py b/wger/weight/tests/test_generator.py
index eac985acf..36e48141b 100644
--- a/wger/weight/tests/test_generator.py
+++ b/wger/weight/tests/test_generator.py
@@ -17,16 +17,16 @@ from django.core.management import call_command
# wger
from wger.core.tests.base_testcase import WgerTestCase
-from wger.weight.models import WeightEntry
+from wger.measurements.models import Measurement
class WeightEntryGeneratorTestCase(WgerTestCase):
def test_generator(self):
# Arrange
- WeightEntry.objects.all().delete()
+ Measurement.objects.all().delete()
# Act
call_command('dummy-generator-body-weight', '--nr-entries', 100)
# Assert
- self.assertEqual(WeightEntry.objects.filter(user_id=1).count(), 100)
+ self.assertEqual(Measurement.body_weight_for(1).count(), 100)
diff --git a/wger/weight/views.py b/wger/weight/views.py
index c58b35f7e..540f25af5 100644
--- a/wger/weight/views.py
+++ b/wger/weight/views.py
@@ -31,8 +31,8 @@ from django.utils.translation import gettext as _
from formtools.preview import FormPreview
# wger
+from wger.measurements.models import Measurement
from wger.weight import helpers
-from wger.weight.models import WeightEntry
logger = logging.getLogger(__name__)
@@ -50,11 +50,11 @@ def export_csv(request):
# Convert all weight data to CSV
writer = csv.writer(response)
- weights = WeightEntry.objects.filter(user=request.user)
+ weights = Measurement.body_weight_for(request.user).order_by('date')
writer.writerow([_('Date'), _('Weight')])
for entry in weights:
- writer.writerow([entry.date, entry.weight])
+ writer.writerow([entry.date, entry.value])
# Send the data to the browser
response['Content-Disposition'] = 'attachment; filename=Weightdata.csv'
@@ -85,5 +85,5 @@ class WeightCsvImportFormPreview(FormPreview):
def done(self, request, cleaned_data):
weight_list, error_list = helpers.parse_weight_csv(request, cleaned_data)
- WeightEntry.objects.bulk_create(weight_list)
+ Measurement.objects.bulk_create(weight_list)
return HttpResponseRedirect(reverse('weight:overview'))