Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Allow users to submit corrections for exercises

Roland Geider4d0a78ef6922
diff --git a/wger/exercises/templates/exercise/email_correction.tpl b/wger/exercises/templates/exercise/email_correction.tpl
new file mode 100644
index 000000000..5813c1a4b
--- /dev/null
+++ b/wger/exercises/templates/exercise/email_correction.tpl
@@ -0,0 +1,43 @@
+A correction has been submitted for the following exercise:
+"{{ exercise }}", ID {{ exercise.id }}
+
+User data:
+----------
+* ID: {{ user.id }}
+* Username: {{ user.username }}
+* Email: {{ user.email }}
+
+
+Changes to the exercise:
+------------------------
+* Name:
+ - Original: {{ exercise.name }}
+ - New: {{ form_data.name }}
+
+* Category:
+ - Original: {{ exercise.category }}
+ - New: {{ form_data.category }}
+
+* Description:
+ - Original: {{ exercise.description }}
+ - New: {{ form_data.description }}
+
+* Muscles (primary):
+ - Original: {{ exercise.muscles.all|join:", " }}
+ - New: {{ form_data.muscles.all|join:", " }}
+
+* Muscles (secondary):
+ - Original: {{ exercise.muscles_secondary.all|join:", " }}
+ - New: {{ form_data.muscles_secondary.all|join:", " }}
+
+* Equipment:
+ - Original: {{ exercise.equipment.all|join:", " }}
+ - New: {{ form_data.equipment.all|join:", " }}
+
+* License:
+ - Original: {{ exercise.license }}
+ - New: {{ form_data.license }}
+
+* Author:
+ - Original: {{ exercise.license_author }}
+ - New: {{ form_data.license_author }}
\ No newline at end of file
diff --git a/wger/exercises/templates/exercise/form_correct.html b/wger/exercises/templates/exercise/form_correct.html
new file mode 100644
index 000000000..29d8fa3c1
--- /dev/null
+++ b/wger/exercises/templates/exercise/form_correct.html
@@ -0,0 +1,10 @@
+{% load i18n %}
+
+{% if not perms.exercises %}
+<h3>{% trans "Correcting an exercise" %}</h3>
+<p><strong>{% blocktrans %}Thank you for correcting and improving this exercise.
+This is much appreciated!{% endblocktrans %}</strong></p>
+
+<p>{% blocktrans %}After you submit the form, the administrators will be notified.
+Once the changes are reviewed the exercise will be updated.{% endblocktrans %}</p>
+{% endif %}
\ No newline at end of file
diff --git a/wger/exercises/templates/exercise/view.html b/wger/exercises/templates/exercise/view.html
index 44c3453fb..142da95b7 100644
--- a/wger/exercises/templates/exercise/view.html
+++ b/wger/exercises/templates/exercise/view.html
@@ -332,6 +332,20 @@ $(document).ready(function() {
</p>
<hr>
+{% if user.is_authenticated and not user.userprofile.is_temporary and not perms.exercises %}
+<h3>{% trans "Options" %}</h3>
+<p>
+ <a href="{% url 'exercise:exercise:correct' exercise.id %}" {% auto_link_css flavour %}>
+ <img src="{% static 'images/icons/edit.svg' %}"
+ width="32"
+ height="32"
+ alt="{% trans 'Submit a correction' %}">
+ {% trans "Submit a correction" %}
+ </a>
+</p>
+{% endif %}
+
+
{% if perms.exercises %}
<h3>{% trans "Options" %}</h3>
<p>
diff --git a/wger/exercises/tests/test_corrected_exercise.py b/wger/exercises/tests/test_corrected_exercise.py
new file mode 100644
index 000000000..3b8cf3957
--- /dev/null
+++ b/wger/exercises/tests/test_corrected_exercise.py
@@ -0,0 +1,81 @@
+# 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 django.core import mail
+from django.core.urlresolvers import reverse
+
+from wger.exercises.models import Exercise
+
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+
+
+class ExercisesCorrectionTestCase(WorkoutManagerTestCase):
+ '''
+ Tests correcting an existing exercise
+ '''
+
+ def correct_exercise(self, fail=True):
+ '''
+ Helper function
+ '''
+ description = 'a nice, long and accurate description for the exercise'
+ response = self.client.post(reverse('exercise:exercise:correct', kwargs={'pk': 1}),
+ {'category': 3,
+ 'name': 'my test exercise',
+ 'license': 2,
+ 'description': description,
+ 'muscles': [3]})
+
+ if fail:
+ self.assertEqual(response.status_code, 403)
+ else:
+ self.assertEqual(response.status_code, 302)
+ new_location = response['Location']
+ response = self.client.get(new_location)
+ self.assertEqual(response.status_code, 200)
+
+ # Submitting a correction doesn't change the original exercise
+ exercise = Exercise.objects.get(pk=1)
+ self.assertEqual(exercise.name, 'An exercise')
+ self.assertEqual(exercise.description, '')
+ self.assertEqual(exercise.category_id, 2)
+ self.assertEqual(exercise.language_id, 1)
+ self.assertEqual([i.pk for i in exercise.muscles.all()], [1, 2])
+ self.assertEqual([i.pk for i in exercise.muscles_secondary.all()], [3])
+
+ # Check the notification email
+ if fail:
+ self.assertEqual(len(mail.outbox), 0)
+ else:
+ self.assertEqual(len(mail.outbox), 1)
+
+ def test_correct_exercise_logged_in_user(self):
+ '''
+ Tests correcting an existing exercise as a logged in user
+ '''
+ self.user_login('test')
+ self.correct_exercise(fail=False)
+
+ def test_correct_exercise_guest_user(self):
+ '''
+ Tests correcting an existing exercise as a guest user
+ '''
+ self.user_login('demo')
+ self.correct_exercise(fail=True)
+
+ def test_correct_exercise_anonymous(self):
+ '''
+ Tests correcting an existing exercise as an anonymous user
+ '''
+ self.correct_exercise(fail=True)
diff --git a/wger/exercises/urls.py b/wger/exercises/urls.py
index 4fef8f9d6..2024e4001 100644
--- a/wger/exercises/urls.py
+++ b/wger/exercises/urls.py
@@ -125,6 +125,9 @@ patterns_exercise = patterns('',
url(r'^(?P<pk>\d+)/edit/$',
exercises.ExerciseUpdateView.as_view(),
name='edit'),
+ url(r'^(?P<pk>\d+)/correct$',
+ exercises.ExerciseCorrectView.as_view(),
+ name='correct'),
url(r'^(?P<pk>\d+)/delete/$',
exercises.ExerciseDeleteView.as_view(),
name='delete'),
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index 1c46922f5..ceb13e585 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -15,6 +15,7 @@
# You should have received a copy of the GNU Affero General Public License
import logging
import uuid
+from django.core import mail
from django.shortcuts import render
from django.shortcuts import get_object_or_404
@@ -28,12 +29,14 @@ from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.decorators import permission_required
from django.contrib import messages
+from django.template.loader import render_to_string
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
from django.views.generic import ListView
from django.views.generic import DeleteView
from django.views.generic import CreateView
from django.views.generic import UpdateView
+import six
from wger.manager.models import WorkoutLog
from wger.exercises.models import Exercise, Muscle
@@ -230,6 +233,52 @@ class ExerciseAddView(ExercisesEditAddView, CreateView, WgerPermissionMixin):
return super(ExerciseAddView, self).dispatch(request, *args, **kwargs)
+class ExerciseCorrectView(ExercisesEditAddView, UpdateView, WgerPermissionMixin):
+ '''
+ Generic view to update an existing exercise
+ '''
+ login_required = True
+ sidebar = 'exercise/form_correct.html'
+ messages = _('Thank you. Once the changes are reviewed the exercise will be updated.')
+
+ def dispatch(self, request, *args, **kwargs):
+ '''
+ Only registered users can correct exercises
+ '''
+ if not request.user.is_authenticated() or request.user.userprofile.is_temporary:
+ return HttpResponseForbidden()
+
+ return super(ExerciseCorrectView, self).dispatch(request, *args, **kwargs)
+
+ def get_context_data(self, **kwargs):
+ context = super(ExerciseCorrectView, self).get_context_data(**kwargs)
+ context['form_action'] = reverse('exercise:exercise:correct', kwargs={'pk': self.object.id})
+ context['title'] = _(u'Correct {0}').format(self.object.name)
+ return context
+
+ def form_valid(self, form):
+ '''
+ If the form is valid send email notifications to the site administrators.
+
+ We don't return the super().form_valid because we don't want the data
+ to be saved.
+ '''
+ subject = 'Correction submitted for exercise #{0}'.format(self.get_object().pk)
+ context = {
+ 'exercise': self.get_object(),
+ 'form_data': form.cleaned_data,
+ 'user': self.request.user
+ }
+ message = render_to_string('exercise/email_correction.tpl', context)
+ mail.mail_admins(six.text_type(subject),
+ six.text_type(message),
+ fail_silently=True)
+
+ messages.success(self.request, self.messages)
+ return HttpResponseRedirect(reverse('exercise:exercise:view',
+ kwargs={'id': self.object.id}))
+
+
class ExerciseDeleteView(WgerDeleteMixin, DeleteView):
'''
Generic view to delete an existing exercise
diff --git a/wger/software/templates/changelog.html b/wger/software/templates/changelog.html
index fd86f446c..205e82c57 100644
--- a/wger/software/templates/changelog.html
+++ b/wger/software/templates/changelog.html
@@ -23,11 +23,12 @@
<a href="https://github.com/rolandgeider/wger/issues/102">#102</a>
</li>
<li>
-<<<<<<< HEAD
-=======
Email reminder to regularly enter (body) weight entries
<a href="https://github.com/rolandgeider/wger/issues/115">#115</a>
</li>
+ <li>
+ Allow users to submit corrections to exercises
+ </li>
<li>
Add day detail view in workout calendar
<a href="https://github.com/rolandgeider/wger/issues/103">#103</a>
@@ -37,7 +38,6 @@
<a href="https://github.com/rolandgeider/wger/issues/89">#89</a>
</li>
<li>
->>>>>>> 223e7bcdf0b40839fa796508859b13808b9cbb7b
Reduce size of generated html code
<a href="https://github.com/rolandgeider/wger/issues/125">#125</a>
</li>