Self hosted FLOSS fitness/workout, nutrition and weight tracker
Allow the administrator to accept pending user submitted images
Roland Geiderd8f094f2f740
diff --git a/wger/exercises/fixtures/test-exercise-images.json b/wger/exercises/fixtures/test-exercise-images.jsonindex 96a6ead75..e7795913d 100644--- a/wger/exercises/fixtures/test-exercise-images.json+++ b/wger/exercises/fixtures/test-exercise-images.json@@ -20,5 +20,16 @@"status": "2","license": "2"}+ },+ {+ "pk": 3,+ "model": "exercises.exerciseimage",+ "fields": {+ "image": "exercise-images/1/wildschwein.jpg",+ "exercise": 2,+ "is_main": false,+ "status": "1",+ "license": "2"+ }}]diff --git a/wger/exercises/templates/exercise/view.html b/wger/exercises/templates/exercise/view.htmlindex 4e9bf7e78..6a8b43ec5 100644--- a/wger/exercises/templates/exercise/view.html+++ b/wger/exercises/templates/exercise/view.html@@ -183,6 +183,37 @@ included in the database.{% endblocktrans %}{% endif %}{% endwith %}+{% if perms.exercises.change_exerciseimage %}+{% with images=exercise.exerciseimage_set.pending %}+{% if images %}+<h3>{% trans "Pending images" %}</h3>+<div class="list-group">+ {% for image in images %}+ <div class="list-group-item">+ <div class="media">+ <div style="width:60px;height:60px;" class="pull-left">+ <a href="{{image.image.url}}">+ <img alt="{{ image.exercise.name }}"+ class="media-object "+ style="max-width:100%; max-height:100%;"+ src="{{ image.image|thumbnail_url:'thumbnail' }}">+ </a>+ </div>+ <div class="media-body">+ <h4 class="media-heading">{{ image.image }}</h4>+ <a href="{% url 'exerciseimage-accept' image.pk %}" class="btn btn-default btn-sm">{% trans "Accept" %}</a>+ <a href="{% url 'exerciseimage-decline' image.pk %}" class="btn btn-default btn-sm">{% trans "Decline" %}</a>+ <a href="{% url 'exerciseimage-delete' exercise.pk image.pk %}" class="btn btn-danger btn-sm">{% trans "Delete" %}</a>+ </div>+ </div>+ </div>+ {% endfor %}+</div>+{% endif %}+{% endwith %}+{% endif %} {# end check permission #}++{% if perms.exercises %}<p><a href="{% url 'exerciseimage-add' exercise.id %}"diff --git a/wger/exercises/tests/test_exercise_images.py b/wger/exercises/tests/test_exercise_images.pyindex c0c7e462c..ea9d73ccb 100644--- a/wger/exercises/tests/test_exercise_images.py+++ b/wger/exercises/tests/test_exercise_images.py@@ -109,7 +109,7 @@ class AddExerciseImageTestCase(WorkoutManagerAddTestCase):object_class = ExerciseImageurl = reverse('exerciseimage-add', kwargs={'exercise_pk': 1})- pk = 3+ pk = 4user_fail = Falsedata = {'is_main': True,'image': open('wger/exercises/tests/protestschwein.jpg', 'rb'),diff --git a/wger/exercises/tests/test_submitted_image.py b/wger/exercises/tests/test_submitted_image.pynew file mode 100644index 000000000..b75640b43--- /dev/null+++ b/wger/exercises/tests/test_submitted_image.py@@ -0,0 +1,161 @@+# 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, ExerciseImage++from wger.manager.tests.testcase import STATUS_CODES_FAIL+from wger.manager.tests.testcase import WorkoutManagerTestCase+++class ImagePendingDetailTestCase(WorkoutManagerTestCase):+ '''+ Tests the detail page of an exercise with a pending image+ '''++ def pending_view(self, fail=False):+ '''+ Helper function+ '''+ response = self.client.get(reverse('exercise-view', kwargs={'id': 2}))+ self.assertEqual(response.status_code, 200)++ if not fail:+ self.assertContains(response, 'Pending images')+ self.assertContains(response, 'Accept')+ self.assertContains(response, 'Decline')+ else:+ self.assertNotContains(response, 'Pending images')+ self.assertNotContains(response, 'Accept')+ self.assertNotContains(response, 'Decline')++ def test_pending_view_admin(self):+ '''+ Tests the detail page of an exercise with a pending image as an admin user+ '''++ self.user_login('admin')+ self.pending_view()++ def test_pending_view_user(self):+ '''+ Tests the detail page of an exercise with a pending image as a regular user+ '''++ self.user_login('test')+ self.pending_view(fail=True)++ def test_pending_view_logged_out(self):+ '''+ Tests the detail page of an exercise with a pending image as a logged out user+ '''++ self.pending_view(fail=True)+++class ImageAcceptTestCase(WorkoutManagerTestCase):+ '''+ Tests accepting a user submitted exercise image+ '''++ def accept(self, fail=False):+ '''+ Helper function+ '''+ image = ExerciseImage.objects.get(pk=3)+ self.assertEqual(image.status, ExerciseImage.STATUS_PENDING)+ response = self.client.get(reverse('exerciseimage-accept', kwargs={'pk': 3}))+ image = ExerciseImage.objects.get(pk=3)+ self.assertEqual(response.status_code, 302)++ if not fail:+ self.assertEqual(image.status, ExerciseImage.STATUS_ACCEPTED)+ response = self.client.get(response['Location'])+ self.assertEqual(response.status_code, 200)+ self.assertEqual(len(mail.outbox), 0)+ else:+ self.assertEqual(image.status, ExerciseImage.STATUS_PENDING)+ self.assertEqual(len(mail.outbox), 0)++ def test_accept_admin(self):+ '''+ Tests accepting a user submitted exercise image as an admin user+ '''++ self.user_login('admin')+ self.accept()++ def test_accept_user(self):+ '''+ Tests accepting a user submitted exercise image as a regular user+ '''++ self.user_login('test')+ self.accept(fail=True)++ def test_accept_logged_out(self):+ '''+ Tests accepting a user submitted exercise as a logged out user+ '''++ self.accept(fail=True)+++class ImageRejectTestCase(WorkoutManagerTestCase):+ '''+ Tests rejecting a user submitted exercise image+ '''++ def reject(self, fail=False):+ '''+ Helper function+ '''+ image = ExerciseImage.objects.get(pk=3)+ self.assertEqual(image.status, ExerciseImage.STATUS_PENDING)+ response = self.client.get(reverse('exerciseimage-decline', kwargs={'pk': 3}))+ image = ExerciseImage.objects.get(pk=3)+ self.assertEqual(response.status_code, 302)++ if not fail:+ self.assertEqual(image.status, ExerciseImage.STATUS_DECLINED)+ response = self.client.get(response['Location'])+ self.assertEqual(response.status_code, 200)++ else:+ self.assertEqual(image.status, ExerciseImage.STATUS_PENDING)++ def test_reject_admin(self):+ '''+ Tests rejecting a user submitted exercise image as an admin user+ '''++ self.user_login('admin')+ self.reject()++ def test_reject_user(self):+ '''+ Tests rejecting a user submitted exercise image as a regular user+ '''++ self.user_login('test')+ self.reject(fail=True)++ def test_reject_logged_out(self):+ '''+ Tests rejecting a user submitted exercise image as a logged out user+ '''++ self.reject(fail=True)diff --git a/wger/exercises/urls.py b/wger/exercises/urls.pyindex caea16f9c..4bd63638d 100644--- a/wger/exercises/urls.py+++ b/wger/exercises/urls.py@@ -51,10 +51,10 @@ urlpatterns = patterns('wger.exercises.views',exercises.PendingExerciseListView.as_view(),name='exercise-pending'),url(r'^(?P<pk>\d+)/accept/$',- 'exercises.accept',+ exercises.accept,name='exercise-accept'),url(r'^(?P<pk>\d+)/decline/$',- 'exercises.decline',+ exercises.decline,name='exercise-decline'),# Muscles@@ -103,6 +103,12 @@ urlpatterns = patterns('wger.exercises.views',url(r'^(?P<exercise_pk>\d+)/image/(?P<pk>\d+)/delete$',images.ExerciseImageDeleteView.as_view(),name='exerciseimage-delete'),+ url(r'^image/(?P<pk>\d+)/accept/$',+ images.accept,+ name='exerciseimage-accept'),+ url(r'^image/(?P<pk>\d+)/decline/$',+ images.decline,+ name='exerciseimage-decline'),# Equipmenturl(r'^equipment/list$',diff --git a/wger/exercises/views/images.py b/wger/exercises/views/images.pyindex 2145b20d1..a21de36d5 100644--- a/wger/exercises/views/images.py+++ b/wger/exercises/views/images.py@@ -14,8 +14,11 @@## You should have received a copy of the GNU Affero General Public Licenseimport logging+from django.contrib.auth.decorators import permission_requiredfrom django.core.urlresolvers import reverse+from django.http import HttpResponseRedirect+from django.shortcuts import get_object_or_404from django.utils.translation import ugettext_lazyfrom django.utils.translation import ugettext as _@@ -120,3 +123,29 @@ class ExerciseImageDeleteView(WgerDeleteMixin, DeleteView, WgerPermissionMixin):kwargs={'pk': pk, 'exercise_pk': exercise_pk})return context+++@permission_required('exercises.change_exerciseimage')+def accept(request, pk):+ '''+ Accepts a pending user submitted image and emails the user, if possible+ '''+ image = get_object_or_404(ExerciseImage, pk=pk)+ image.status = ExerciseImage.STATUS_ACCEPTED+ image.save()+ # image.send_email(request)++ return HttpResponseRedirect(image.exercise.get_absolute_url())+++@permission_required('exercises.change_exerciseimage')+def decline(request, pk):+ '''+ Declines and deletes a pending user submitted image+ '''+ image = get_object_or_404(ExerciseImage, pk=pk)+ image.status = ExerciseImage.STATUS_DECLINED+ image.save()+ # image.send_email(request)++ return HttpResponseRedirect(image.exercise.get_absolute_url())