Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add last date the user was notified about expiring workouts

Roland Geider5121e67330ed
diff --git a/wger/manager/management/commands/email-reminders.py b/wger/manager/management/commands/email-reminders.py
index 9b89f5a65..5aa6cbd49 100644
--- a/wger/manager/management/commands/email-reminders.py
+++ b/wger/manager/management/commands/email-reminders.py
@@ -42,10 +42,20 @@ class Command(BaseCommand):
'''
Find if the currently active workout is overdue
'''
-
profile_list = UserProfile.objects.filter(workout_reminder_active=True)
counter = 0
for profile in profile_list:
+
+ # Check if we already notified the user and update the profile otherwise
+ if profile.last_workout_notification and \
+ (datetime.date.today()
+ - profile.last_workout_notification
+ < datetime.timedelta(weeks=1)):
+ continue
+ else:
+ profile.last_workout_notification = datetime.date.today()
+ profile.save()
+
(current_workout, schedule) = Schedule.objects.get_current_workout(profile.user)
# No schedules, use the default workout length in user profile
@@ -55,7 +65,8 @@ class Command(BaseCommand):
- datetime.date.today())
if datetime.timedelta(days=profile.workout_reminder) > delta:
- #print "* Workout '{0}' overdue".format(current_workout)
+ if int(options['verbosity']) >= 3:
+ print "* Workout '{0}' overdue".format(current_workout)
counter += 1
self.send_email(profile.user,
current_workout,
@@ -73,7 +84,8 @@ class Command(BaseCommand):
delta = schedule.get_end_date() - datetime.date.today()
if datetime.timedelta(days=profile.workout_reminder) > delta:
- #print "* Workout '{0}' overdue - schedule".format(schedule_step.workout)
+ if int(options['verbosity']) >= 3:
+ print "* Workout '{0}' overdue - schedule".format(schedule_step.workout)
counter += 1
self.send_email(profile.user,
@@ -83,7 +95,7 @@ class Command(BaseCommand):
#print '* step not last in schedule'
pass
- if counter:
+ if counter and int(options['verbosity']) >= 2:
self.stdout.write("Sent {0} email reminders".format(counter))
@staticmethod
diff --git a/wger/manager/models.py b/wger/manager/models.py
index 74330cc77..78639911e 100644
--- a/wger/manager/models.py
+++ b/wger/manager/models.py
@@ -557,7 +557,6 @@ class UserProfile(models.Model):
GENDER_MALE = '1'
GENDER_FEMALE = '2'
-
GENDER = (
(GENDER_MALE, _('Male')),
(GENDER_FEMALE, _('Female')),
@@ -566,7 +565,6 @@ class UserProfile(models.Model):
INTENSITY_LOW = '1'
INTENSITY_MEDIUM = '2'
INTENSITY_HIGH = '3'
-
INTENSITY = (
(INTENSITY_LOW, _('Low')),
(INTENSITY_MEDIUM, _('Medium')),
@@ -617,6 +615,15 @@ by the US Department of Agriculture. It is extremely complete, with around
'in a schedule. Used for email workout '
'reminders.'),
default=12)
+ last_workout_notification = models.DateField(editable=False,
+ blank=False,
+ null=True)
+ '''
+ The last time the user got a workout reminder email
+
+ This is needed e.g. to check daily per cron for old workouts but only
+ send users an email once per week
+ '''
notification_language = models.ForeignKey(Language,
verbose_name=_('Notification language'),
diff --git a/wger/manager/templates/mobile/tags/yaml_form_element.html b/wger/manager/templates/mobile/tags/yaml_form_element.html
index 428ad47b3..9ac1e5b1d 100644
--- a/wger/manager/templates/mobile/tags/yaml_form_element.html
+++ b/wger/manager/templates/mobile/tags/yaml_form_element.html
@@ -6,6 +6,10 @@
{% trans field.label %}
</label>
+ {% if not field.field.required %}
+ <span style="font-size:90%;">— <em>{% trans "Optional" %}</em></span><br>
+ {% endif %}
+
{% if field.errors %}
<div class="form-error">
{% for error in field.errors %}
@@ -18,10 +22,6 @@
<p class="dimmed">{% trans field.help_text %}</p>
{% endif %}
- {% if not field.field.required %}
- <span style="font-size:90%;">— <em>{% trans "Optional" %}</em></span><br>
- {% endif %}
-
<div {% if field.errors %}class="form-error"{% endif %}>
{{ field }}
</div>
diff --git a/wger/manager/tests/test_email_reminder.py b/wger/manager/tests/test_email_reminder.py
index afb8dd1e2..e41f8b112 100644
--- a/wger/manager/tests/test_email_reminder.py
+++ b/wger/manager/tests/test_email_reminder.py
@@ -19,7 +19,7 @@ from django.core import mail
from django.contrib.auth.models import User
from wger.manager.tests.testcase import WorkoutManagerTestCase
-from wger.manager.models import Schedule
+from wger.manager.models import Schedule, UserProfile
from wger.manager.models import Workout
@@ -53,6 +53,59 @@ class EmailReminderTestCase(WorkoutManagerTestCase):
call_command('email-reminders')
self.assertEqual(len(mail.outbox), 1)
+ def test_reminder_last_notification(self):
+ '''
+ Test that no emails are sent if the last notification field is more
+ recent than one week.
+
+ User 2, workout created 2012-11-20
+ '''
+
+ profile = UserProfile.objects.get(user=2)
+ profile.last_workout_notification = datetime.date.today() - datetime.timedelta(days=3)
+ profile.save()
+
+ Schedule.objects.all().delete()
+ Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 0)
+
+ def test_reminder_last_notification_2(self):
+ '''
+ Test that no emails are sent if the last notification field is more
+ than one week away.
+
+ User 2, workout created 2012-11-20
+ '''
+
+ profile = UserProfile.objects.get(user=2)
+ profile.last_workout_notification = datetime.date.today() - datetime.timedelta(days=10)
+ profile.save()
+
+ Schedule.objects.all().delete()
+ Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 1)
+
+ def test_reminder_last_notification_3(self):
+ '''
+ Test that no emails are sent if the last notification field is null
+
+ User 2, workout created 2012-11-20
+ '''
+
+ profile = UserProfile.objects.get(user=2)
+ profile.last_workout_notification = None
+ profile.save()
+
+ Schedule.objects.all().delete()
+ Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+
+ call_command('email-reminders')
+ self.assertEqual(len(mail.outbox), 1)
+
def test_reminder_setting_off(self):
'''
Test user with no schedules, one workout but setting in profile off