Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix email reminders command and tests

Roland Geider1db132d42675
diff --git a/wger/manager/management/commands/email-reminders.py b/wger/manager/management/commands/email-reminders.py
index 28b3e3cce..93a09fe97 100644
--- a/wger/manager/management/commands/email-reminders.py
+++ b/wger/manager/management/commands/email-reminders.py
@@ -42,68 +42,56 @@ class Command(BaseCommand):
"""
profile_list = UserProfile.objects.filter(workout_reminder_active=True)
counter = 0
+ today = datetime.date.today()
+
for profile in profile_list:
- # Only continue if the user has provided an email address.
- # Checking it here so we check for NULL values and emtpy strings
if not profile.user.email:
continue
- # 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)
- ):
+ routine = Routine.objects.filter(user=profile.user).last()
+ if not routine:
continue
- current_workout = Routine.objects.last()
+ delta = routine.end - datetime.date.today()
- # No schedules, use the default workout length in user profile
- if current_workout:
- delta = (
- current_workout.created
- + datetime.timedelta(weeks=profile.workout_duration)
- - datetime.date.today()
- )
+ if delta.days < 0:
+ # Skip if notified this week:
+ if profile.last_workout_notification and (
+ today - profile.last_workout_notification < datetime.timedelta(weeks=1)
+ ):
+ continue
- if datetime.timedelta(days=profile.workout_reminder) > delta:
- if int(options['verbosity']) >= 3:
- self.stdout.write(f"* Workout '{current_workout}' overdue")
- counter += 1
+ if int(options['verbosity']) >= 3:
+ self.stdout.write(f"* Routine '{routine}' overdue")
- self.send_email(
- profile.user,
- current_workout,
- delta,
- )
+ self.send_email(profile.user, routine, delta)
+ profile.last_workout_notification = today
+ profile.save()
if counter and int(options['verbosity']) >= 2:
self.stdout.write(f'Sent {counter} email reminders')
@staticmethod
- def send_email(user, workout, delta):
+ def send_email(user, routine: Routine, delta: datetime.timedelta):
"""
Notify a user that a workout is about to expire
-
- :type user User
- :type workout Workout
- :type delta datetime.timedelta
"""
- # Update the last notification date field
- user.userprofile.last_workout_notification = datetime.date.today()
- user.userprofile.save()
-
# Compose and send the email
translation.activate(user.userprofile.notification_language.short_name)
context = {
'site': Site.objects.get_current(),
- 'workout': workout,
- 'expired': True if delta.days < 0 else False,
+ 'routine': routine,
+ 'expired': delta.days < 0,
'days': abs(delta.days),
}
- subject = _('Workout will expire soon')
- message = loader.render_to_string('workout/email_reminder.tpl', context)
+ subject = _('Routine will expire soon')
+ message = loader.render_to_string('routines/email_reminder.tpl', context)
mail.send_mail(
- subject, message, settings.WGER_SETTINGS['EMAIL_FROM'], [user.email], fail_silently=True
+ subject,
+ message,
+ settings.WGER_SETTINGS['EMAIL_FROM'],
+ [user.email],
+ fail_silently=True,
)
diff --git a/wger/manager/templates/workout/email_reminder.tpl b/wger/manager/templates/routines/email_reminder.tpl
similarity index 74%
rename from wger/manager/templates/workout/email_reminder.tpl
rename to wger/manager/templates/routines/email_reminder.tpl
index bed528ae9..fed689c28 100644
--- a/wger/manager/templates/workout/email_reminder.tpl
+++ b/wger/manager/templates/routines/email_reminder.tpl
@@ -1,8 +1,8 @@
{% spaceless %}{% load i18n %}
{% if expired %}
- {% blocktranslate %}Your current workout '{{workout}}' expired {{days}} days ago.{% endblocktranslate %}
+ {% blocktranslate %}Your current workout '{{routine}}' expired {{days}} days ago.{% endblocktranslate %}
{% else %}
- {% blocktranslate %}Your current workout '{{workout}}' is about to expire in {{days}} days.{% endblocktranslate %}
+ {% blocktranslate %}Your current workout '{{routine}}' is about to expire in {{days}} days.{% endblocktranslate %}
{% endif %}
{% endspaceless %}
@@ -15,6 +15,6 @@ option in your settings.{% endblocktranslate %}
— {% blocktranslate %}The {{ site }} team{% endblocktranslate %}
-* https://{{site}}{{workout.get_absolute_url}}
-* https://{{site}}{% url 'manager:workout:add' %}
+* https://{{site}}{{routine.get_absolute_url}}
+* https://{{site}}{% url 'manager:routine:overview' %}
* https://{{site}}{% url 'core:user:preferences' %}
diff --git a/wger/manager/tests/test_email_reminder.py b/wger/manager/tests/test_email_reminder.py
index b79b8c3ac..380154750 100644
--- a/wger/manager/tests/test_email_reminder.py
+++ b/wger/manager/tests/test_email_reminder.py
@@ -23,7 +23,7 @@ from django.core.management import call_command
# wger
from wger.core.models import UserProfile
from wger.core.tests.base_testcase import WgerTestCase
-from wger.manager.models import Workout
+from wger.manager.models import Routine
class EmailReminderTestCase(WgerTestCase):
@@ -33,11 +33,19 @@ class EmailReminderTestCase(WgerTestCase):
User 2 has setting in profile active
"""
+ def setUp(self):
+ super().setUp()
+
+ routine = Routine.objects.get(pk=2)
+ routine.start = datetime.date.today() - datetime.timedelta(weeks=10)
+ routine.end = datetime.date.today() - datetime.timedelta(days=3)
+ routine.save()
+
def test_reminder_no_workouts(self):
"""
Test with no schedules or workouts
"""
- Workout.objects.all().delete()
+ Routine.objects.all().delete()
call_command('email-reminders')
self.assertEqual(len(mail.outbox), 0)
@@ -45,11 +53,8 @@ class EmailReminderTestCase(WgerTestCase):
def test_reminder_one_workout(self):
"""
Test user with no schedules but one workout
-
- User 2, workout created 2012-11-20
"""
-
- Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+ Routine.objects.exclude(user_id=2).delete()
call_command('email-reminders')
self.assertEqual(len(mail.outbox), 1)
@@ -57,15 +62,13 @@ class EmailReminderTestCase(WgerTestCase):
def test_reminder_skip_if_no_email(self):
"""
Tests that no emails are sent if the user has provided no email
-
- User 2, workout created 2012-11-20
"""
user = User.objects.get(pk=2)
user.email = ''
user.save()
- Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+ Routine.objects.exclude(user_id=2).delete()
call_command('email-reminders')
self.assertEqual(len(mail.outbox), 0)
@@ -74,15 +77,13 @@ class EmailReminderTestCase(WgerTestCase):
"""
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()
- Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+ Routine.objects.exclude(user_id=2).delete()
call_command('email-reminders')
self.assertEqual(len(mail.outbox), 0)
@@ -91,15 +92,13 @@ class EmailReminderTestCase(WgerTestCase):
"""
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()
- Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+ Routine.objects.exclude(user_id=2).delete()
call_command('email-reminders')
self.assertEqual(len(mail.outbox), 1)
@@ -115,20 +114,20 @@ class EmailReminderTestCase(WgerTestCase):
profile.last_workout_notification = None
profile.save()
- Workout.objects.exclude(user=User.objects.get(pk=2)).delete()
+ Routine.objects.exclude(user_id=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
+ Test user with one routine but setting in profile off
"""
user = User.objects.get(pk=2)
user.userprofile.workout_reminder_active = False
user.userprofile.save()
- Workout.objects.exclude(user=user).delete()
+ Routine.objects.exclude(user=user).delete()
call_command('email-reminders')
self.assertEqual(len(mail.outbox), 0)