Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add an overview of exercises pending review

Roland Geider325c877f275e
diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.json
index 86c9d38fe..86cbd9c83 100644
--- a/wger/exercises/fixtures/test-exercises.json
+++ b/wger/exercises/fixtures/test-exercises.json
@@ -75,6 +75,7 @@
"pk": 1,
"model": "exercises.exercise",
"fields": {
+ "status": "5",
"category": 2,
"muscles": [
1,
@@ -84,6 +85,7 @@
3
],
"description": "",
+ "user": null,
"name": "An exercise"
}
},
@@ -91,6 +93,7 @@
"pk": 2,
"model": "exercises.exercise",
"fields": {
+ "status": "5",
"category": 2,
"muscles": [
2
@@ -100,6 +103,7 @@
3
],
"description": "",
+ "user": null,
"name": "A very cool exercise"
}
},
@@ -107,11 +111,27 @@
"pk": 3,
"model": "exercises.exercise",
"fields": {
+ "status": "5",
"category": 3,
"muscles": [
1
],
"description": "",
+ "user": null,
+ "name": "Boring exercise"
+ }
+ },
+ {
+ "pk": 4,
+ "model": "exercises.exercise",
+ "fields": {
+ "status": "1",
+ "category": 3,
+ "muscles": [
+ 1
+ ],
+ "description": "",
+ "user": 2,
"name": "Boring exercise"
}
},
@@ -119,12 +139,14 @@
"pk": 81,
"model": "exercises.exercise",
"fields": {
+ "status": "5",
"category": 3,
"muscles": [
1
],
"muscles_secondary":[],
"description": "",
+ "user": null,
"name": "Needed for demo user"
}
},
@@ -132,12 +154,14 @@
"pk": 84,
"model": "exercises.exercise",
"fields": {
+ "status": "5",
"category": 3,
"muscles": [
1
],
"muscles_secondary":[],
"description": "",
+ "user": null,
"name": "Needed for demo user"
}
},
@@ -145,26 +169,29 @@
"pk": 91,
"model": "exercises.exercise",
"fields": {
+ "status": "5",
"category": 3,
"muscles": [
1
],
"muscles_secondary":[],
"description": "",
+ "user": null,
"name": "Needed for demo user"
}
},
-
{
"pk": 111,
"model": "exercises.exercise",
"fields": {
+ "status": "5",
"category": 3,
"muscles": [
1
],
"muscles_secondary":[],
"description": "",
+ "user": null,
"name": "Needed for demo user"
}
},
diff --git a/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py b/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py
index 629c4def0..bb2eee627 100644
--- a/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py
+++ b/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py
@@ -3,6 +3,7 @@ import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
+from wger.exercises.models import EXERCISE_STATUS_SYSTEM
class Migration(SchemaMigration):
@@ -15,7 +16,16 @@ class Migration(SchemaMigration):
# Adding field 'Exercise.status'
db.add_column('exercises_exercise', 'status',
- self.gf('django.db.models.fields.CharField')(default=1, max_length=2),
+ self.gf('django.db.models.fields.CharField')(default='1', max_length=2),
+ keep_default=False)
+ # Set the status 'system' for all existing exercises in the database
+ if not db.dry_run:
+ orm.Exercise.objects.all().update(status=EXERCISE_STATUS_SYSTEM)
+
+
+ # Adding field 'Exercise.creation_date'
+ db.add_column('exercises_exercise', 'creation_date',
+ self.gf('django.db.models.fields.DateField')(auto_now_add=True, null=True, blank=True),
keep_default=False)
@@ -26,6 +36,9 @@ class Migration(SchemaMigration):
# Deleting field 'Exercise.status'
db.delete_column('exercises_exercise', 'status')
+ # Deleting field 'Exercise.creation_date'
+ db.delete_column('exercises_exercise', 'creation_date')
+
models = {
'auth.group': {
@@ -67,12 +80,13 @@ class Migration(SchemaMigration):
'exercises.exercise': {
'Meta': {'ordering': "['name']", 'object_name': 'Exercise'},
'category': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['exercises.ExerciseCategory']"}),
+ 'creation_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'null': 'True', 'blank': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'max_length': '2000', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'muscles': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['exercises.Muscle']", 'symmetrical': 'False'}),
'muscles_secondary': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'secondary_muscles'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['exercises.Muscle']"}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}),
- 'status': ('django.db.models.fields.CharField', [], {'default': '1', 'max_length': '2'}),
+ 'status': ('django.db.models.fields.CharField', [], {'default': "'1'", 'max_length': '2'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'})
},
'exercises.exercisecategory': {
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index 95fedaf57..20b2b87f9 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -99,16 +99,18 @@ class ExerciseCategory(models.Model):
return False
-EXERCISE_STATUS_PENDING = 1
-EXERCISE_STATUS_ACCEPTED = 2
-EXERCISE_STATUS_DECLINED = 3
-EXERCISE_STATUS_ADMIN = 3
+EXERCISE_STATUS_PENDING = '1'
+EXERCISE_STATUS_ACCEPTED = '2'
+EXERCISE_STATUS_DECLINED = '3'
+EXERCISE_STATUS_ADMIN = '4'
+EXERCISE_STATUS_SYSTEM = '5'
EXERCISE_STATUS = (
(EXERCISE_STATUS_PENDING, _('Pending')),
(EXERCISE_STATUS_ACCEPTED, _('Accepted')),
(EXERCISE_STATUS_DECLINED, _('Declined')),
(EXERCISE_STATUS_ADMIN, _('Submitted by administrator')),
+ (EXERCISE_STATUS_SYSTEM, _('System exercise')),
)
@@ -125,6 +127,9 @@ class Exercise(models.Model):
choices=EXERCISE_STATUS,
default=EXERCISE_STATUS_PENDING)
+ # Submission date
+ creation_date = models.DateField(_('Date'), auto_now_add=True, null=True, blank=True)
+
# Exercise description
category = models.ForeignKey(ExerciseCategory,
verbose_name=_('Category'))
diff --git a/wger/exercises/templates/exercise/pending.html b/wger/exercises/templates/exercise/pending.html
new file mode 100644
index 000000000..b217ce14b
--- /dev/null
+++ b/wger/exercises/templates/exercise/pending.html
@@ -0,0 +1,38 @@
+{% extends "base.html" %}
+{% load url from future %}
+{% load i18n %}
+{% load staticfiles %}
+{% load manager_extras %}
+
+{% block title %}{% trans "Pending exercises" %}{% endblock %}
+
+
+{% block content %}
+
+<table>
+<thead>
+<tr>
+ <th>{% trans "Submission date" %}</th>
+ <th>{% trans "Name" %}</th>
+ <th>{% trans "User" %}</th>
+</tr>
+</thead>
+<tbody>
+{% for exercise in exercise_list %}
+<tr>
+ <td>
+ {{exercise.creation_date}}
+ </td>
+ <td><a href="{{ exercise.get_absolute_url }}">{{exercise.name}}</a></td>
+ <td>{{exercise.user.username}}</td>
+</tr>
+{% empty %}
+<tr>
+ <td colspan="2">{% trans "No pending ingredients at the moment" %}</td>
+</tr>
+{% endfor %}
+</tbody>
+
+</table>
+<div style="padding-top:3em;"></div>
+{% endblock %}
\ No newline at end of file
diff --git a/wger/exercises/templates/view.html b/wger/exercises/templates/view.html
index e8315ad9c..0d236ed4b 100644
--- a/wger/exercises/templates/view.html
+++ b/wger/exercises/templates/view.html
@@ -29,6 +29,52 @@ function custom_modal_init()
Main Content
-->
{% block content %}
+
+{% if exercise.status == EXERCISE_STATUS_PENDING %}
+<div class="alert">
+<h3>{% trans "Exercise is pending review" %}</h3>
+<p>{% blocktrans %}This user submitted exercise is pending a decision to be
+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.
+{% endif %}
+</p>
+
+{% if perms.exercises %}
+<div class="ym-grid linearize-level-1">
+ <div class="ym-g50 ym-gl">
+ <div class="ym-gbox-left">
+ <div class="alert alert-success">
+ <a href="{% url 'exercise-accept' exercise.id %}">
+ <img src="{% static 'images/icons/status-on.svg' %}"
+ width="32"
+ height="32"
+ alt="{% trans 'Accept' %}">
+ {% trans "Accept" %}
+ </a>
+ </div>
+ </div>
+ </div>
+ <div class="ym-g50 ym-gl">
+ <div class="ym-gbox-left">
+ <div class="alert alert-error">
+ <a href="{% url 'exercise-decline' exercise.id %}">
+ <img src="{% static 'images/icons/status-off.svg' %}"
+ width="32"
+ height="32"
+ alt="{% trans 'Decline' %}">
+ {% trans "Decline" %}
+ </a>
+ </div>
+ </div>
+ </div>
+</div>
+{% endif %}
+
+</div>
+{% endif %} {# end exercise is pending review #}
+
<p>{% trans "Category" %}: <strong>{{ exercise.category.name }}</strong></p>
<p>{{ exercise.description|safe }}</p>
@@ -212,8 +258,7 @@ weight_log_chart({{json|safe}}, "{{svg_uuid}}", "{{reps}}")
<img src="{% static 'images/icons/edit.svg' %}"
width="32"
height="32"
- alt="{% trans 'Edit' %}"
- >
+ alt="{% trans 'Edit' %}">
{% trans "Edit" %}
</a>
</p>
diff --git a/wger/exercises/tests/__init__.py b/wger/exercises/tests/__init__.py
index fbf832e5f..321e0d2e1 100644
--- a/wger/exercises/tests/__init__.py
+++ b/wger/exercises/tests/__init__.py
@@ -1,4 +1,5 @@
from wger.exercises.tests.categories import *
from wger.exercises.tests.exercise import *
+from wger.exercises.tests.submitted_exercise import *
from wger.exercises.tests.muscles import *
from wger.exercises.tests.exercise_comments import *
diff --git a/wger/exercises/tests/exercise.py b/wger/exercises/tests/exercise.py
index 5a650ff62..a0a57d793 100644
--- a/wger/exercises/tests/exercise.py
+++ b/wger/exercises/tests/exercise.py
@@ -142,11 +142,12 @@ class ExerciseDetailTestCase(WorkoutManagerTestCase):
self.assertContains(response, 'Edit')
self.assertContains(response, 'Delete')
self.assertContains(response, 'Add new comment')
-
+ self.assertNotContains(response, 'Exercise is pending')
else:
self.assertNotContains(response, 'Edit')
self.assertNotContains(response, 'Delete')
self.assertNotContains(response, 'Add new comment')
+ self.assertNotContains(response, 'Exercise is pending')
# Ensure that non-existent exercises throw a 404.
response = self.client.get(reverse('wger.exercises.views.exercises.view',
diff --git a/wger/exercises/tests/submitted_exercise.py b/wger/exercises/tests/submitted_exercise.py
new file mode 100644
index 000000000..ac4fc986e
--- /dev/null
+++ b/wger/exercises/tests/submitted_exercise.py
@@ -0,0 +1,208 @@
+# This file is part of Workout Manager.
+#
+# 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.
+#
+# 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 json
+
+from django.core.urlresolvers import reverse
+
+from wger.exercises.models import Exercise
+from wger.exercises.models import EXERCISE_STATUS_PENDING
+from wger.exercises.models import EXERCISE_STATUS_ACCEPTED
+from wger.exercises.models import EXERCISE_STATUS_DECLINED
+from wger.exercises.models import EXERCISE_STATUS_ADMIN
+
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
+
+
+class ExercisesPendingTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the pending exercises overview page
+ '''
+
+ def pending_overview(self, fail=False):
+ '''
+ Helper function
+ '''
+ response = self.client.get(reverse('exercise-pending'))
+ if not fail:
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(len(response.context['exercise_list']), 1)
+ else:
+ self.assertEqual(response.status_code, 302)
+
+ def test_pending_overview_admin(self):
+ '''
+ Tests the pending exercises overview page as an admin user
+ '''
+
+ self.user_login('admin')
+ self.pending_overview()
+
+ def test_pending_overview_user(self):
+ '''
+ Tests the pending exercises overview page as a regular user
+ '''
+
+ self.user_login('test')
+ self.pending_overview(fail=True)
+
+ def test_pending_overview_logged_out(self):
+ '''
+ Tests the pending exercises overview page as a logged out user
+ '''
+
+ self.pending_overview(fail=True)
+
+
+class ExercisesPendingDetailTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the detail page of a pending exercise
+ '''
+
+ def pending_view(self, fail=False):
+ '''
+ Helper function
+ '''
+ response = self.client.get(reverse('wger.exercises.views.exercises.view',
+ kwargs={'id': 4}))
+ self.assertContains(response, 'Exercise is pending')
+
+ if not fail:
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, 'Please select one of the options below')
+ self.assertContains(response, 'Accept')
+ self.assertContains(response, 'Decline')
+ else:
+ self.assertNotContains(response, 'Please select one of the options below')
+ self.assertNotContains(response, 'Accept')
+ self.assertNotContains(response, 'Decline')
+
+ def test_pending_view_admin(self):
+ '''
+ Tests the detail page of a pending exercise as an admin user
+ '''
+
+ self.user_login('admin')
+ self.pending_view()
+
+ def test_pending_view_user(self):
+ '''
+ Tests the detail page of a pending exercise 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 a pending exercise as a logged out user
+ '''
+
+ self.pending_view(fail=True)
+
+
+class ExerciseAcceptTestCase(WorkoutManagerTestCase):
+ '''
+ Tests accepting a user submitted exercise
+ '''
+
+ def accept(self, fail=False):
+ '''
+ Helper function
+ '''
+ exercise = Exercise.objects.get(pk=4)
+ self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ response = self.client.get(reverse('exercise-accept', kwargs={'pk': 4}))
+ exercise = Exercise.objects.get(pk=4)
+
+ self.assertEqual(response.status_code, 302)
+
+ if not fail:
+ self.assertEqual(exercise.status, EXERCISE_STATUS_ACCEPTED)
+ response = self.client.get(response['Location'])
+ self.assertEqual(response.status_code, 200)
+
+ else:
+ self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+
+ def test_accept_admin(self):
+ '''
+ Tests accepting a user submitted exercise as an admin user
+ '''
+
+ self.user_login('admin')
+ self.accept()
+
+ def test_accept_user(self):
+ '''
+ Tests accepting a user submitted exercise 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 ExerciseRejectTestCase(WorkoutManagerTestCase):
+ '''
+ Tests rejecting a user submitted exercise
+ '''
+
+ def reject(self, fail=False):
+ '''
+ Helper function
+ '''
+ exercise = Exercise.objects.get(pk=4)
+ self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ response = self.client.get(reverse('exercise-decline', kwargs={'pk': 4}))
+ exercise = Exercise.objects.get(pk=4)
+ self.assertEqual(response.status_code, 302)
+
+ if not fail:
+ self.assertEqual(exercise.status, EXERCISE_STATUS_DECLINED)
+ response = self.client.get(response['Location'])
+ self.assertEqual(response.status_code, 200)
+
+ else:
+ self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+
+ def test_reject_admin(self):
+ '''
+ Tests rejecting a user submitted exercise as an admin user
+ '''
+
+ self.user_login('admin')
+ self.reject()
+
+ def test_reject_user(self):
+ '''
+ Tests rejecting a user submitted exercise as a regular user
+ '''
+
+ self.user_login('test')
+ self.reject(fail=True)
+
+ def test_reject_logged_out(self):
+ '''
+ Tests rejecting a user submitted exercise as a logged out user
+ '''
+
+ self.reject(fail=True)
diff --git a/wger/exercises/urls.py b/wger/exercises/urls.py
index 439285ead..0ca476371 100644
--- a/wger/exercises/urls.py
+++ b/wger/exercises/urls.py
@@ -24,6 +24,15 @@ urlpatterns = patterns('wger.exercises.views',
url(r'^(?P<pk>\d+)/delete/$',
permission_required('exercises.change_exercise')(exercises.ExerciseDeleteView.as_view()),
name='exercise-delete'),
+ url(r'^pending/$',
+ permission_required('exercises.change_exercise')(exercises.PendingExerciseListView.as_view()),
+ name='exercise-pending'),
+ url(r'^(?P<pk>\d+)/accept/$',
+ 'exercises.accept',
+ name='exercise-accept'),
+ url(r'^(?P<pk>\d+)/decline/$',
+ 'exercises.decline',
+ name='exercise-decline'),
# Muscles
url(r'^muscle/overview/$',
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index 44faead7f..d2021d4d9 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -29,6 +29,7 @@ from django.core.context_processors import csrf
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.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
@@ -83,6 +84,7 @@ def view(request, id, slug=None):
template_data = {}
template_data['comment_edit'] = False
+ template_data['EXERCISE_STATUS_PENDING'] = EXERCISE_STATUS_PENDING
# Load the exercise itself
exercise = get_object_or_404(Exercise, pk=id)
@@ -237,6 +239,46 @@ class ExerciseDeleteView(YamlDeleteMixin, DeleteView):
return context
+class PendingExerciseListView(ListView):
+ '''
+ Generic view to list all weight units
+ '''
+
+ model = Exercise
+ template_name = 'exercise/pending.html'
+ context_object_name = 'exercise_list'
+
+ def get_queryset(self):
+ '''
+ Only show pending exercises
+ '''
+ return Exercise.objects.filter(status=EXERCISE_STATUS_PENDING)
+
+
+@permission_required('exercise.add_exercise')
+def accept(request, pk):
+ '''
+ Accepts a pending user submitted exercise
+ '''
+ exercise = get_object_or_404(Exercise, pk=pk)
+ exercise.status = EXERCISE_STATUS_ACCEPTED
+ exercise.save()
+ messages.success(request, _('Exercise was sucessfully added to the general database'))
+ return HttpResponseRedirect(exercise.get_absolute_url())
+
+
+@permission_required('exercise.add_exercise')
+def decline(request, pk):
+ '''
+ Declines and deletes a pending user submitted exercise
+ '''
+ exercise = get_object_or_404(Exercise, pk=pk)
+ exercise.status = EXERCISE_STATUS_DECLINED
+ exercise.save()
+ messages.success(request, _('Exercise was sucessfully marked as rejected'))
+ return HttpResponseRedirect(exercise.get_absolute_url())
+
+
def search(request):
'''
Search an exercise, return the result as a JSON list
diff --git a/wger/manager/templates/template.html b/wger/manager/templates/template.html
index 96f9e17b8..c11d1edd0 100644
--- a/wger/manager/templates/template.html
+++ b/wger/manager/templates/template.html
@@ -108,6 +108,9 @@
<ul class="dropdown-menu">
<li><a href="{% url 'wger.exercises.views.exercises.overview' %}">{% trans "Exercises" %}</a></li>
<li><a href="{% url 'muscle-overview' %}">{% trans "Muscle overview" %}</a></li>
+ {% if perms.exercises %}
+ <li><a href="{% url 'exercise-pending' %}">{% trans "Exercises pending review" %}</a></li>
+ {% endif %}
</ul>
</li>