Self hosted FLOSS fitness/workout, nutrition and weight tracker
Issue #115 - Reminders to enter weight
matheusc6093c527553
diff --git a/wger/core/models.py b/wger/core/models.pyindex 0740d44de..7073cf6ca 100644--- a/wger/core/models.py+++ b/wger/core/models.py@@ -303,6 +303,12 @@ by the US Department of Agriculture. It is extremely complete, with arounddefault=False)'''Allow anonymous read-only access'''+ ''' Number of Days Reminder Weight '''+ num_days_weight_reminder = models.IntegerField(verbose_name=_('Number of days Weight Reminder'),+ help_text=_('Num of days after the last entry to reminder the user'),+ max_length=30,+ null=True)+@propertydef weight(self):'''diff --git a/wger/manager/management/commands/email-weight-reminder.py b/wger/manager/management/commands/email-weight-reminder.pynew file mode 100644index 000000000..fa0f5582f--- /dev/null+++ b/wger/manager/management/commands/email-weight-reminder.py@@ -0,0 +1,85 @@+# -*- coding: utf-8 *-*++# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++import datetime++from django.template import loader+from django.core.management.base import BaseCommand+from django.core import mail+from django.utils.translation import ugettext_lazy as _+from django.utils import translation++from django.contrib.sites.models import Site+from wger.core.models import UserProfile+from wger.weight.models import WeightEntry+from wger.utils.constants import EMAIL_FROM+++class Command(BaseCommand):+ '''+ Helper admin command to send out email remainders+ '''++ help = 'Send out automatic emails to reminder the user to input the weight'++ def handle(self, *args, **options):++ profile_list = UserProfile.objects.filter(num_days_weight_reminder__gt=0)++ for profile in profile_list:++ # Onl continue if the user has provided an email address.+ # Checking it here so we check for NULL values and emtpy strings+ #print("Checking User RWeight Reminder")+ if not profile.user.email:+ continue++ today = datetime.datetime.now().date()+ #print(today)++ try:+ last_entry = WeightEntry.objects.filter(user=profile.user).latest().creation_date+ datediff = (today - last_entry).days++ if datediff >= profile.num_days_weight_reminder:+ self.send_email(profile.user, last_entry)+ except WeightEntry.DoesNotExist:+ print("At least one weight entry is required")+++ @staticmethod+ def send_email(user, last_entry):+ '''+ Notify a user to input the weight entry++ :type user User+ :type last_entry Date+ '''++ # Compose and send the email+ translation.activate(user.userprofile.notification_language.short_name)++ context = {}+ context['site'] = Site.objects.get_current()+ context['day'] = last_entry++ subject = _('You have to entry your weight')+ message = loader.render_to_string('workout/email_weight_reminder.html', context)+ mail.send_mail(subject,+ message,+ EMAIL_FROM,+ [user.email],+ fail_silently=True)diff --git a/wger/manager/templates/workout/email_weight_reminder.html b/wger/manager/templates/workout/email_weight_reminder.htmlnew file mode 100644index 000000000..08ea2b3b9--- /dev/null+++ b/wger/manager/templates/workout/email_weight_reminder.html@@ -0,0 +1,15 @@+{% spaceless %}{% load i18n %}++{% blocktrans %}Your last weight entry is from {{day}}. Please click the link above to access wger and update this information. {% endblocktrans %}++{% endspaceless %}++{% blocktrans %}You will regularly receive such reminders till you entry your weight.+If you don't want to ever receive email+reminders, deactivate the option in your settings.{% endblocktrans %}++{% trans "the wger.de team" %}+++* http://{{site}}{{workout.get_absolute_url}}+* http://{{site}}{% url 'core:user:preferences' %}\ No newline at end of filediff --git a/wger/manager/tests/test_email_weight_reminder.py b/wger/manager/tests/test_email_weight_reminder.pynew file mode 100644index 000000000..9e0bf1ce9--- /dev/null+++ b/wger/manager/tests/test_email_weight_reminder.py@@ -0,0 +1,98 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++from datetime import date, timedelta, datetime++from django.core.management import call_command+from django.core import mail+from django.contrib.auth.models import User+from wger.manager.tests.testcase import WorkoutManagerTestCase+from wger.weight.models import WeightEntry++class EmailWeightReminderTestCase(WorkoutManagerTestCase):+ def test_without_email(self):+ user = User.objects.get(pk=2)+ user.email = ''+ user.num_days_weight_reminder = 3+ user.save()++ call_command("email-weight-reminder")+ self.assertEqual(len(mail.outbox), 0)++ def test_without_num_days_weight_reminder(self):+ user = User.objects.get(pk=2)+ user.email = '[email protected]'+ user.save()++ user.userprofile.num_days_weight_reminder = 0+ user.userprofile.save()++ call_command("email-weight-reminder")+ self.assertEqual(len(mail.outbox), 0)++ def test_with_num_days_weight_reminder(self):+ user = User.objects.get(pk=2)+ user.email = '[email protected]'+ user.save()++ user.userprofile.num_days_weight_reminder = 3+ user.userprofile.save()++ call_command("email-weight-reminder")+ self.assertEqual(len(mail.outbox), 1)++ def test_send_email(self):+ user = User.objects.get(pk=2)+ user.email = '[email protected]'+ user.save()++ weightEntry = WeightEntry.objects.filter(user=user).get(pk=3)+ weightEntry.creation_date = datetime.now().date() - timedelta(days=2)+ weightEntry.save()++ user.userprofile.num_days_weight_reminder = 1+ user.userprofile.save()++ call_command("email-weight-reminder")+ self.assertEqual(len(mail.outbox), 1)++ def test_send_email_zero_days_diff(self):+ user = User.objects.get(pk=2)+ user.email = '[email protected]'+ user.save()++ weightEntry = WeightEntry.objects.filter(user=user).get(pk=3)+ weightEntry.creation_date = datetime.now().date() - timedelta(days=1)+ weightEntry.save()++ user.userprofile.num_days_weight_reminder = 1+ user.userprofile.save()++ call_command("email-weight-reminder")+ self.assertEqual(len(mail.outbox), 1)++ def test_not_send_email(self):+ user = User.objects.get(pk=2)+ user.email = '[email protected]'+ user.save()++ weightEntry = WeightEntry.objects.filter(user=user).latest()+ weightEntry.creation_date = datetime.now().date()+ weightEntry.save()++ user.userprofile.num_days_weight_reminder = 3+ user.userprofile.save()++ call_command("email-weight-reminder")+ self.assertEqual(len(mail.outbox), 0)\ No newline at end of file