Skip to content

Commit d256a11

Browse files
authored
Merge pull request #1895 from scrapcode/issue-1814
Allow multiple weight entries per day
2 parents f7ed9b3 + 9761513 commit d256a11

17 files changed

+124
-111
lines changed

wger/core/models/profile.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from django.db import models
3030
from django.db.models import IntegerField
31+
from django.utils import timezone
3132
from django.utils.translation import gettext_lazy as _
3233

3334
# wger
@@ -535,23 +536,14 @@ def calculate_activities(self):
535536

536537
def user_bodyweight(self, weight):
537538
"""
538-
Create a new weight entry as needed
539+
Create a new weight entry and return it
539540
"""
540-
if not WeightEntry.objects.filter(user=self.user).exists() or (
541-
datetime.date.today() - WeightEntry.objects.filter(user=self.user).latest().date
542-
> datetime.timedelta(days=3)
543-
):
544-
entry = WeightEntry()
545-
entry.weight = weight
546-
entry.user = self.user
547-
entry.date = datetime.date.today()
548-
entry.save()
549-
550-
# Update the last entry
551-
else:
552-
entry = WeightEntry.objects.filter(user=self.user).latest()
553-
entry.weight = weight
554-
entry.save()
541+
entry = WeightEntry()
542+
entry.weight = weight
543+
entry.user = self.user
544+
entry.date = timezone.now()
545+
entry.save()
546+
555547
return entry
556548

557549
def get_owner_object(self):

wger/core/tests/test_preferences.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# Django
2121
from django.contrib.auth.models import User
2222
from django.urls import reverse
23+
from django.utils import timezone
2324

2425
# wger
2526
from wger.core.tests.base_testcase import WgerTestCase
@@ -146,7 +147,7 @@ def test_bodyweight_new(self):
146147
entry = user.userprofile.user_bodyweight(80)
147148
count_after = WeightEntry.objects.filter(user=user).count()
148149
self.assertEqual(count_before, count_after - 1)
149-
self.assertEqual(entry.date, datetime.date.today())
150+
self.assertEqual(entry.date.date(), timezone.now().date())
150151

151152
def test_bodyweight_new_2(self):
152153
"""
@@ -155,60 +156,41 @@ def test_bodyweight_new_2(self):
155156
user = User.objects.get(pk=2)
156157
count_before = WeightEntry.objects.filter(user=user).count()
157158
last_entry = WeightEntry.objects.filter(user=user).latest()
158-
last_entry.date = datetime.date.today() - datetime.timedelta(weeks=1)
159+
last_entry.date = timezone.now() - datetime.timedelta(weeks=1)
159160
last_entry.save()
160161

161162
entry = user.userprofile.user_bodyweight(80)
162163
count_after = WeightEntry.objects.filter(user=user).count()
163164
self.assertEqual(count_before, count_after - 1)
164-
self.assertEqual(entry.date, datetime.date.today())
165+
self.assertEqual(entry.date.date(), timezone.now().date())
165166

166-
def test_bodyweight_no_entries(self):
167+
def test_bodyweight_new_3(self):
167168
"""
168-
Tests that a new weight entry is created if there are no weight entries
169+
Tests that a new weight entry is created even if others exist today
169170
"""
170171
user = User.objects.get(pk=2)
171-
WeightEntry.objects.filter(user=user).delete()
172-
173172
count_before = WeightEntry.objects.filter(user=user).count()
174-
entry = user.userprofile.user_bodyweight(80)
175-
count_after = WeightEntry.objects.filter(user=user).count()
176-
self.assertEqual(count_before, count_after - 1)
177-
self.assertEqual(entry.date, datetime.date.today())
178-
179-
def test_bodyweight_edit(self):
180-
"""
181-
Tests that the last weight entry is edited
182-
"""
183-
user = User.objects.get(pk=2)
184173
last_entry = WeightEntry.objects.filter(user=user).latest()
185-
last_entry.date = datetime.date.today() - datetime.timedelta(days=3)
174+
last_entry.date = timezone.now() - datetime.timedelta(hours=1)
186175
last_entry.save()
187176

188-
count_before = WeightEntry.objects.filter(user=user).count()
189-
entry = user.userprofile.user_bodyweight(100)
177+
entry = user.userprofile.user_bodyweight(80)
190178
count_after = WeightEntry.objects.filter(user=user).count()
191-
self.assertEqual(count_before, count_after)
192-
self.assertEqual(entry.pk, last_entry.pk)
193-
self.assertEqual(entry.date, last_entry.date)
194-
self.assertEqual(entry.weight, 100)
179+
self.assertEqual(count_before, count_after - 1)
180+
self.assertEqual(entry.date.date(), timezone.now().date())
195181

196-
def test_bodyweight_edit_2(self):
182+
def test_bodyweight_no_entries(self):
197183
"""
198-
Tests that the last weight entry is edited
184+
Tests that a new weight entry is created if there are no weight entries
199185
"""
200186
user = User.objects.get(pk=2)
201-
last_entry = WeightEntry.objects.filter(user=user).latest()
202-
last_entry.date = datetime.date.today()
203-
last_entry.save()
187+
WeightEntry.objects.filter(user=user).delete()
204188

205189
count_before = WeightEntry.objects.filter(user=user).count()
206-
entry = user.userprofile.user_bodyweight(100)
190+
entry = user.userprofile.user_bodyweight(80)
207191
count_after = WeightEntry.objects.filter(user=user).count()
208-
self.assertEqual(count_before, count_after)
209-
self.assertEqual(entry.pk, last_entry.pk)
210-
self.assertEqual(entry.date, last_entry.date)
211-
self.assertEqual(entry.weight, 100)
192+
self.assertEqual(count_before, count_after - 1)
193+
self.assertEqual(entry.date.date(), timezone.now().date())
212194

213195

214196
class PreferencesCalculationsTestCase(WgerTestCase):
@@ -223,7 +205,7 @@ def test_last_weight_entry(self):
223205
self.user_login('test')
224206
user = User.objects.get(pk=2)
225207
entry = WeightEntry()
226-
entry.date = datetime.datetime.today()
208+
entry.date = timezone.now()
227209
entry.user = user
228210
entry.weight = 100
229211
entry.save()
@@ -263,6 +245,10 @@ def test_basal_metabolic_rate(self):
263245

264246
self.user_login('test')
265247

248+
# Test User (pk=2)
249+
# 180 cm, 20 years
250+
# Last weight entry is 83 kg
251+
266252
# Male
267253
user = User.objects.get(pk=2)
268254
bmr = user.userprofile.calculate_basal_metabolic_rate()

wger/core/tests/test_temporary_users.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from django.core.management import call_command
2222
from django.http import HttpRequest
2323
from django.urls import reverse
24+
from django.utils import timezone
2425

2526
# wger
2627
from wger.core.demo import (
@@ -100,7 +101,7 @@ def test_demo_data_guest_account(self):
100101

101102
def test_demo_data_body_weight(self):
102103
"""
103-
Tests that the helper function that creates demo data filters out
104+
Tests that the helper function that creates demo data does not filter out
104105
existing dates for the weight entries
105106
"""
106107
self.client.get(reverse('core:dashboard'))
@@ -109,7 +110,7 @@ def test_demo_data_body_weight(self):
109110

110111
temp = []
111112
for i in range(1, 5):
112-
creation_date = datetime.date.today() - datetime.timedelta(days=i)
113+
creation_date = timezone.now() - datetime.timedelta(days=i)
113114
entry = WeightEntry(
114115
user=user,
115116
weight=80 + 0.5 * i + random.randint(1, 3),
@@ -120,7 +121,7 @@ def test_demo_data_body_weight(self):
120121
create_demo_entries(user)
121122

122123
# Body weight
123-
self.assertEqual(WeightEntry.objects.filter(user=user).count(), 40)
124+
self.assertEqual(WeightEntry.objects.filter(user=user).count(), 44)
124125

125126
def test_demo_user(self):
126127
"""

wger/nutrition/models/plan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def get_closest_weight_entry(self):
193193
)
194194
if closest_entry_gte is None or closest_entry_lte is None:
195195
return closest_entry_gte or closest_entry_lte
196-
if abs(closest_entry_gte.date - target) < abs(closest_entry_lte.date - target):
196+
if abs(closest_entry_gte.date.date() - target) < abs(closest_entry_lte.date.date() - target):
197197
return closest_entry_gte
198198
else:
199199
return closest_entry_lte

wger/nutrition/tests/test_calories_calculator.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# Django
2121
from django.contrib.auth.models import User
2222
from django.urls import reverse
23+
from django.utils import timezone
2324

2425
# wger
2526
from wger.core.tests.base_testcase import WgerTestCase
@@ -90,7 +91,7 @@ def test_automatic_weight_entry_bmr(self):
9091
self.user_login('test')
9192
user = User.objects.get(username=self.current_user)
9293

93-
# Existing weight entry is old, a new one is created
94+
# A new weight entry is always created
9495
entry1 = WeightEntry.objects.filter(user=user).latest()
9596
response = self.client.post(
9697
reverse('nutrition:calories:bmr'), {'age': 30, 'height': 180, 'gender': 1, 'weight': 80}
@@ -100,18 +101,6 @@ def test_automatic_weight_entry_bmr(self):
100101
self.assertEqual(entry1.weight, 83)
101102
self.assertEqual(entry2.weight, 80)
102103

103-
# Existing weight entry is from today, is updated
104-
entry2.delete()
105-
entry1.date = datetime.date.today()
106-
entry1.save()
107-
response = self.client.post(
108-
reverse('nutrition:calories:bmr'), {'age': 30, 'height': 180, 'gender': 1, 'weight': 80}
109-
)
110-
self.assertEqual(response.status_code, 200)
111-
entry2 = WeightEntry.objects.filter(user=user).latest()
112-
self.assertEqual(entry1.pk, entry2.pk)
113-
self.assertEqual(entry2.weight, 80)
114-
115104
# No existing entries
116105
WeightEntry.objects.filter(user=user).delete()
117106
response = self.client.post(
@@ -120,4 +109,4 @@ def test_automatic_weight_entry_bmr(self):
120109
self.assertEqual(response.status_code, 200)
121110
entry = WeightEntry.objects.filter(user=user).latest()
122111
self.assertEqual(entry.weight, 80)
123-
self.assertEqual(entry.date, datetime.date.today())
112+
self.assertEqual(entry.date.date(), timezone.now().date())

wger/utils/constants.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@
3737
'%Y-%m-%d', # '2012-10-25'
3838
]
3939

40+
# Valid datetime formats
41+
DATETIME_FORMATS = [
42+
'%d.%m.%Y %H:%M:%S', # '25.10.2012 14:30:00'
43+
'%d.%m.%Y %H:%M', # '25.10.2012 14:30'
44+
'%d.%m.%y %H:%M:%S', # '25.10.12 14:30:00'
45+
'%d.%m.%y %H:%M', # '25.10.12 14:30'
46+
'%m/%d/%Y %H:%M:%S', # '10/25/2012 14:30:00'
47+
'%m/%d/%Y %H:%M', # '10/25/2012 14:30'
48+
'%m/%d/%y %H:%M:%S', # '10/25/12 14:30:00'
49+
'%m/%d/%y %H:%M', # '10/25/12 14:30'
50+
'%Y-%m-%d %H:%M:%S', # '2012-10-25 14:30:00'
51+
'%Y-%m-%d %H:%M', # '2012-10-25 14:30'
52+
]
53+
4054
# Allowed tags, attributes and styles allowed in textareas edited with a JS
4155
# editor. Everything not in these whitelists is stripped.
4256
HTML_TAG_WHITELIST = {'b', 'i', 'strong', 'em', 'ul', 'ol', 'li', 'p'}

wger/weight/fixtures/test-weight-data.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"fields": {
66
"user": 2,
77
"weight": 77,
8-
"date": "2012-10-01"
8+
"date": "2012-10-01T14:30:21.592Z"
99
}
1010
},
1111
{
@@ -14,7 +14,7 @@
1414
"fields": {
1515
"user": 2,
1616
"weight": 77.2,
17-
"date": "2012-10-10"
17+
"date": "2012-10-10T14:30:21.592Z"
1818
}
1919
},
2020
{
@@ -23,7 +23,7 @@
2323
"fields": {
2424
"user": 2,
2525
"weight": 80.6,
26-
"date": "2012-11-01"
26+
"date": "2012-11-01T14:30:21.592Z"
2727
}
2828
},
2929
{
@@ -32,7 +32,7 @@
3232
"fields": {
3333
"user": 2,
3434
"weight": 80,
35-
"date": "2013-01-01"
35+
"date": "2013-01-01T14:30:21.592Z"
3636
}
3737
},
3838
{
@@ -41,7 +41,7 @@
4141
"fields": {
4242
"user": 2,
4343
"weight": 81,
44-
"date": "2013-01-10"
44+
"date": "2013-01-10T14:30:21.592Z"
4545
}
4646
},
4747
{
@@ -50,7 +50,7 @@
5050
"fields": {
5151
"user": 2,
5252
"weight": 82,
53-
"date": "2013-01-20"
53+
"date": "2013-01-20T14:30:21.592Z"
5454
}
5555
},
5656
{
@@ -59,7 +59,7 @@
5959
"fields": {
6060
"user": 2,
6161
"weight": 83,
62-
"date": "2013-01-30"
62+
"date": "2013-01-30T14:30:21.592Z"
6363
}
6464
}
6565
]

wger/weight/forms.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from django import forms
1919
from django.forms import (
2020
CharField,
21+
DateTimeField,
2122
Form,
2223
Textarea,
2324
)
@@ -28,13 +29,19 @@
2829
from crispy_forms.layout import Layout
2930

3031

31-
CSV_DATE_FORMAT = (
32-
('%d.%m.%Y', 'DD.MM.YYYY (30.01.2012)'),
33-
('%d.%m.%y', 'DD.MM.YY (30.01.12)'),
34-
('%Y-%m-%d', 'YYYY-MM-DD (2012-01-30)'),
35-
('%y-%m-%d', 'YY-MM-DD (12-01-30)'),
36-
('%m/%d/%Y', 'MM/DD/YYYY (01/30/2012)'),
37-
('%m/%d/%y', 'MM/DD/YY (01/30/12)'),
32+
CSV_DATETIME_FORMAT = (
33+
('%d.%m.%Y %H:%M:%S', 'DD.MM.YYYY HH:MM:SS (30.01.2012 14:30:00)'),
34+
('%d.%m.%Y %H:%M', 'DD.MM.YYYY HH:MM (30.01.2012 14:30)'),
35+
('%d.%m.%y %H:%M:%S', 'DD.MM.YY HH:MM:SS (30.01.12 14:30:00)'),
36+
('%d.%m.%y %H:%M', 'DD.MM.YY HH:MM (30.01.12 14:30)'),
37+
('%Y-%m-%d %H:%M:%S', 'YYYY-MM-DD HH:MM:SS (2012-01-30 14:30:00)'),
38+
('%Y-%m-%d %H:%M', 'YYYY-MM-DD HH:MM (2012-01-30 14:30)'),
39+
('%y-%m-%d %H:%M:%S', 'YY-MM-DD HH:MM:SS (12-01-30 14:30:00)'),
40+
('%y-%m-%d %H:%M', 'YY-MM-DD HH:MM (12-01-30 14:30)'),
41+
('%m/%d/%Y %H:%M:%S', 'MM/DD/YYYY HH:MM:SS (01/30/2012 14:30:00)'),
42+
('%m/%d/%Y %H:%M', 'MM/DD/YYYY HH:MM (01/30/2012 14:30)'),
43+
('%m/%d/%y %H:%M:%S', 'MM/DD/YY HH:MM:SS (01/30/12 14:30:00)'),
44+
('%m/%d/%y %H:%M', 'MM/DD/YY HH:MM (01/30/12 14:30)'),
3845
)
3946

4047

@@ -44,7 +51,7 @@ class WeightCsvImportForm(Form):
4451
"""
4552

4653
csv_input = CharField(widget=Textarea, label=_('Input'))
47-
date_format = forms.ChoiceField(choices=CSV_DATE_FORMAT, label=_('Date format'))
54+
date_format = forms.ChoiceField(choices=CSV_DATETIME_FORMAT, label=_('Date format'))
4855

4956
def __init__(self, *args, **kwargs):
5057
super(WeightCsvImportForm, self).__init__(*args, **kwargs)

wger/weight/management/commands/dummy-generator-body-weight.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# Django
2121
from django.contrib.auth.models import User
2222
from django.core.management.base import BaseCommand
23+
from django.utils import timezone
2324

2425
# wger
2526
from wger.weight.models import WeightEntry
@@ -71,7 +72,7 @@ def handle(self, **options):
7172

7273
# Weight entries
7374
for i in range(options['nr_entries']):
74-
creation_date = datetime.date.today() - datetime.timedelta(days=i)
75+
creation_date = timezone.now() - datetime.timedelta(days=i)
7576
if creation_date not in existing_entries:
7677
entry = WeightEntry(
7778
user=user,

wger/weight/management/commands/email-weight-reminder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from django.core import mail
2222
from django.core.management.base import BaseCommand
2323
from django.template import loader
24-
from django.utils import translation
24+
from django.utils import translation, timezone
2525
from django.utils.translation import gettext_lazy as _
2626

2727
# wger
@@ -45,7 +45,7 @@ def handle(self, **options):
4545
if not profile.user.email:
4646
continue
4747

48-
today = datetime.datetime.now().date()
48+
today = timezone.now()
4949

5050
try:
5151
last_entry = WeightEntry.objects.filter(user=profile.user).latest().date

0 commit comments

Comments
 (0)