Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Send emails when a new exercise was submitted or it was approved

Roland Geiderfa626bf651f8
diff --git a/wger/exercises/templates/view.html b/wger/exercises/templates/view.html
index 0d236ed4b..b205012c4 100644
--- a/wger/exercises/templates/view.html
+++ b/wger/exercises/templates/view.html
@@ -37,7 +37,7 @@ function custom_modal_init()
included in the database. Till then, it will not be shown in the overview or
the search.{% endblocktrans %}
{% if perms.exercises %}
-Please select one of the options below.
+{% trans "Please select one of the options below." %}
{% endif %}
</p>
@@ -75,6 +75,7 @@ Please select one of the options below.
</div>
{% endif %} {# end exercise is pending review #}
+
<p>{% trans "Category" %}: <strong>{{ exercise.category.name }}</strong></p>
<p>{{ exercise.description|safe }}</p>
@@ -250,6 +251,14 @@ weight_log_chart({{json|safe}}, "{{svg_uuid}}", "{{reps}}")
{% endwith %}
{% endwith %}
+{% if exercise.status == EXERCISE_STATUS_ACCEPTED %}
+<div class="alert" style="margin-top:1em;">
+<h3>{% trans "User submitted exercise" %}</h3>
+{% with username=exercise.user.username %}
+<p>{% blocktrans %}This exercise was submitted by <strong>{{username}}</strong>.{% endblocktrans %}
+{% endwith %}
+</div>
+{% endif %}
{% if perms.exercises %}
<h3>{% trans "Options" %}</h3>
diff --git a/wger/exercises/tests/exercise.py b/wger/exercises/tests/exercise.py
index a0a57d793..5e9d4f7c3 100644
--- a/wger/exercises/tests/exercise.py
+++ b/wger/exercises/tests/exercise.py
@@ -14,6 +14,7 @@
import json
+from django.core import mail
from django.core.urlresolvers import reverse
from wger.exercises.models import Exercise
@@ -178,7 +179,7 @@ class ExerciseDetailTestCase(WorkoutManagerTestCase):
self.exercise_detail(editor=False)
-class ExercisesTestCase(WorkoutManagerTestCase):
+class ExercisesAddTestCase(WorkoutManagerTestCase):
'''
Exercise test case
'''
@@ -216,14 +217,11 @@ class ExercisesTestCase(WorkoutManagerTestCase):
self.add_exercise_user_fail()
self.user_logout()
- def add_exercise_success(self, demo=False, admin=False):
+ def add_exercise_success(self, admin=False):
'''
Tests adding/editing an exercise with a user with enough rights to do this
'''
- # Log in as 'admin'
- self.user_login()
-
# Add an exercise
count_before = Exercise.objects.count()
response = self.client.post(reverse('exercise-add'),
@@ -240,12 +238,13 @@ class ExercisesTestCase(WorkoutManagerTestCase):
# Exercise was saved
exercise = Exercise.objects.get(pk=exercise_id)
- if demo:
- exercise.user_id = 3
- exercise.status = EXERCISE_STATUS_PENDING
if admin:
- exercise.user_id = 1
- exercise.status = EXERCISE_STATUS_ADMIN
+ self.assertEqual(exercise.user_id, 1)
+ self.assertEqual(exercise.status, EXERCISE_STATUS_ADMIN)
+ else:
+ self.assertEqual(exercise.user_id, 2)
+ self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+
response = self.client.get(reverse('wger.exercises.views.exercises.view',
kwargs={'id': exercise_id}))
self.assertEqual(response.status_code, 200)
@@ -268,7 +267,10 @@ class ExercisesTestCase(WorkoutManagerTestCase):
{'category': 111,
'name': 'my test exercise',
'muscles': [1, 2]})
- self.assertTrue(response.context['form'].errors['category'])
+ if admin:
+ self.assertTrue(response.context['form'].errors['category'])
+ else:
+ self.assertEqual(response.status_code, 302)
# No muscles - adding
response = self.client.post(reverse('exercise-add'),
@@ -282,25 +284,25 @@ class ExercisesTestCase(WorkoutManagerTestCase):
{'category': 1,
'name': 'my test exercise',
'muscles': []})
- self.assertTrue(response.context['form'].errors['muscles'])
- self.user_logout()
+ if admin:
+ self.assertTrue(response.context['form'].errors['muscles'])
+ else:
+ self.assertEqual(response.status_code, 302)
def test_add_exercise_success(self):
'''
Tests adding/editing an exercise with a user with enough rights to do this
'''
-
- # Log in as 'admin'
self.user_login('admin')
- self.add_exercise_success()
+ self.add_exercise_success(admin=True)
def test_add_exercise_user_no_rights(self):
'''
Tests adding an exercise with a user without enough rights to do this
'''
-
self.user_login('test')
- self.add_exercise_success()
+ self.add_exercise_success(admin=False)
+ self.assertEqual(len(mail.outbox), 1)
def search_exercise(self, fail=True):
'''
diff --git a/wger/exercises/tests/submitted_exercise.py b/wger/exercises/tests/submitted_exercise.py
index ac4fc986e..73576eb5a 100644
--- a/wger/exercises/tests/submitted_exercise.py
+++ b/wger/exercises/tests/submitted_exercise.py
@@ -14,6 +14,7 @@
import json
+from django.core import mail
from django.core.urlresolvers import reverse
from wger.exercises.models import Exercise
@@ -133,9 +134,10 @@ class ExerciseAcceptTestCase(WorkoutManagerTestCase):
self.assertEqual(exercise.status, EXERCISE_STATUS_ACCEPTED)
response = self.client.get(response['Location'])
self.assertEqual(response.status_code, 200)
-
+ self.assertEqual(len(mail.outbox), 1)
else:
self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ self.assertEqual(len(mail.outbox), 0)
def test_accept_admin(self):
'''
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index d2021d4d9..adf53d45e 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -25,6 +25,7 @@ from django.http import HttpResponseRedirect
from django.http import HttpResponseForbidden
from django.forms import ModelForm
from django.forms import ModelChoiceField
+from django.core import mail
from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
@@ -54,6 +55,8 @@ from wger.exercises.models import EXERCISE_STATUS_ADMIN
from wger.workout_manager.generic_views import YamlFormMixin
from wger.workout_manager.generic_views import YamlDeleteMixin
+from wger.workout_manager.constants import EMAIL_FROM
+
logger = logging.getLogger('workout_manager.custom')
@@ -85,6 +88,7 @@ def view(request, id, slug=None):
template_data = {}
template_data['comment_edit'] = False
template_data['EXERCISE_STATUS_PENDING'] = EXERCISE_STATUS_PENDING
+ template_data['EXERCISE_STATUS_ACCEPTED'] = EXERCISE_STATUS_ACCEPTED
# Load the exercise itself
exercise = get_object_or_404(Exercise, pk=id)
@@ -206,6 +210,13 @@ class ExerciseAddView(ExercisesEditAddView, CreateView):
form.instance.user = self.request.user
if self.request.user.has_perm('exercises.add_exercise'):
form.instance.status = EXERCISE_STATUS_ADMIN
+ else:
+ subject = _('New user submitted exercise')
+ message = _('''The user {0} submitted a new exercise "{1}".'''.format(
+ self.request.user.username, form.instance.name))
+ mail.mail_admins(_('New user submitted exercise'),
+ message,
+ fail_silently=True)
return super(ExerciseAddView, self).form_valid(form)
@@ -264,6 +275,24 @@ def accept(request, pk):
exercise.status = EXERCISE_STATUS_ACCEPTED
exercise.save()
messages.success(request, _('Exercise was sucessfully added to the general database'))
+
+ # Notify the user if possible
+ if exercise.user.email:
+ url = request.build_absolute_uri(exercise.get_absolute_url())
+ subject = _('Exercise was sucessfully added to the general database')
+ message = _("Your exercise '{0}' was successfully added to the general database.\n"
+ "\n"
+ "It is now available on the exercise and muscle overview and can be\n"
+ "added to workouts. You can access it on this address:\n"
+ "{1}\n"
+ "\n"
+ "Thank you for contributing and making this site better!\n"
+ " the wger.de team".format(exercise.name, url))
+ mail.send_mail(subject,
+ message,
+ EMAIL_FROM,
+ [exercise.user.email],
+ fail_silently=True)
return HttpResponseRedirect(exercise.get_absolute_url())
diff --git a/wger/main.py b/wger/main.py
index e3ad8967d..a48efdc97 100755
--- a/wger/main.py
+++ b/wger/main.py
@@ -39,6 +39,10 @@ from wger.settings_global import *
DEBUG = True
TEMPLATE_DEBUG = DEBUG
+ADMINS = (
+ ('Your name', '[email protected]'),
+)
+
DBPATH = %(dbpath)s
DATABASES = {
diff --git a/wger/manager/fixtures/test-user-data.json b/wger/manager/fixtures/test-user-data.json
index 4d9528819..18f8c6a65 100644
--- a/wger/manager/fixtures/test-user-data.json
+++ b/wger/manager/fixtures/test-user-data.json
@@ -52,7 +52,7 @@
"groups": [],
"user_permissions": [],
"password": "pbkdf2_sha256$10000$o9VK8DZG89YU$p5ZodV5143uKZ+TxLPSpjmklCmF7UvXVwYiitoW+6VM=",
- "email": "",
+ "email": "[email protected]",
"date_joined": "2012-09-26T19:11:43.114Z"
}
},
diff --git a/wger/settings_global.py b/wger/settings_global.py
index 0e7514098..a306172ba 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -42,6 +42,11 @@ LANGUAGES = (
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en'
+#
+# Email prefix used
+#
+EMAIL_SUBJECT_PREFIX = '[wger] '
+
# Login-URL
LOGIN_URL = '/login/'
diff --git a/wger/workout_manager/constants.py b/wger/workout_manager/constants.py
index ba13e74fc..7de17006c 100644
--- a/wger/workout_manager/constants.py
+++ b/wger/workout_manager/constants.py
@@ -53,3 +53,7 @@ HTML_STYLES_WHITELIST = ('text-decoration',)
PAGINATION_OBJECTS_PER_PAGE = 25
PAGINATION_MAX_TOTAL_PAGES = 10
PAGINATION_PAGES_AROUND_CURRENT = 5
+
+
+# Email
+EMAIL_FROM = 'wger Workout Manager <[email protected]>'