Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Remove functions to fix add or edit exercises

Roland Geider4facc311a53c
diff --git a/wger/core/templates/navigation.html b/wger/core/templates/navigation.html
index d2649ef88..237d05bd8 100644
--- a/wger/core/templates/navigation.html
+++ b/wger/core/templates/navigation.html
@@ -65,11 +65,6 @@
<li><a class="dropdown-item" href="{% url 'exercise:muscle:overview' %}">{% translate "by muscle" context "as in sorted by muscle" %}</a></li>
<li><a class="dropdown-item" href="{% url 'exercise:equipment:overview' %}">{% translate "by equipment" context "as in sorted by equipment" %}</a></li>
- {% if user.is_authenticated and not user.userprofile.is_temporary and not trainer_identity %}
- <li class="dropdown-divider"></li>
- <li><a class="dropdown-item" href="{% url 'exercise:exercise:add' %}">{% translate "Add new exercise" %}</a><li>
- {% endif %}
-
{% if user.userprofile.has_exercise_permission %}
<li class="dropdown-divider"></li>
<li class="dropdown-header">{% translate "Administration" %}</li>
diff --git a/wger/exercises/templates/equipment/overview.html b/wger/exercises/templates/equipment/overview.html
index 4a721e015..0aaea935e 100644
--- a/wger/exercises/templates/equipment/overview.html
+++ b/wger/exercises/templates/equipment/overview.html
@@ -102,16 +102,4 @@ $(function() {
{% block options %}
- {% if user.is_authenticated and not user.userprofile.is_temporary %}
- <a href="{% url 'exercise:exercise:add' %}" class="btn btn-success btn-sm">
- {% translate "Add new exercise" %}
- </a>
- {% else %}
- <a href="#" class="btn btn-success btn-sm disabled">
- {% translate "Add new exercise" %}<br>
- <small>
- {% translate "Only registered users can do this" %}
- </small>
- </a>
- {% endif %}
{% endblock %}
diff --git a/wger/exercises/templates/exercise/email_correction.tpl b/wger/exercises/templates/exercise/email_correction.tpl
deleted file mode 100644
index f25e1cfaf..000000000
--- a/wger/exercises/templates/exercise/email_correction.tpl
+++ /dev/null
@@ -1,43 +0,0 @@
-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 }}
diff --git a/wger/exercises/templates/exercise/email_new.tpl b/wger/exercises/templates/exercise/email_new.tpl
deleted file mode 100644
index fb770efc1..000000000
--- a/wger/exercises/templates/exercise/email_new.tpl
+++ /dev/null
@@ -1,9 +0,0 @@
-{% load i18n %}{% blocktranslate %}Your exercise '{{exercise}}' was successfully added to the general
-database.{% endblocktranslate %}
-
-{% blocktranslate %}It is now available on the exercise and muscle overview and can be
-added to workouts. You can access it on this address:{% endblocktranslate %}
-{{url}}
-
-{% blocktranslate %}Thank you for contributing and making this site better!{% endblocktranslate %}
-— {% blocktranslate %}The {{ site }} team{% endblocktranslate %}
diff --git a/wger/exercises/templates/exercise/form.html b/wger/exercises/templates/exercise/form.html
deleted file mode 100644
index cd6c593d1..000000000
--- a/wger/exercises/templates/exercise/form.html
+++ /dev/null
@@ -1,12 +0,0 @@
-{% load i18n %}
-
-{% if not user.userprofile.has_exercise_permission %}
-<h4>{% translate "Submitting an exercise" %}</h4>
-<p><strong>{% blocktranslate %}Thank you for submitting an exercise. This is much
-appreciated!{% endblocktranslate %}</strong></p>
-
-<p>{% blocktranslate %}After you submit the form, the administrators will be notified.
-Once it is reviewed, it will be added to the database. Till this happens,
-it will not be visible in the overview or the search. In case you added your email
-when you registered, you will get notified when the process is completed successfully.{% endblocktranslate %}</p>
-{% endif %}
diff --git a/wger/exercises/templates/exercise/form_correct.html b/wger/exercises/templates/exercise/form_correct.html
deleted file mode 100644
index 82cba341a..000000000
--- a/wger/exercises/templates/exercise/form_correct.html
+++ /dev/null
@@ -1,10 +0,0 @@
-{% load i18n %}
-
-{% if not user.userprofile.has_exercise_permission %}
-<h4>{% translate "Correcting an exercise" %}</h4>
-<p><strong>{% blocktranslate %}Thank you for correcting and improving this exercise.
-This is much appreciated!{% endblocktranslate %}</strong></p>
-
-<p>{% blocktranslate %}After you submit the form, the administrators will be notified.
-Once the changes are reviewed the exercise will be updated.{% endblocktranslate %}</p>
-{% endif %}
diff --git a/wger/exercises/templates/exercise/overview.html b/wger/exercises/templates/exercise/overview.html
index 155382b0b..15f0e538e 100644
--- a/wger/exercises/templates/exercise/overview.html
+++ b/wger/exercises/templates/exercise/overview.html
@@ -109,16 +109,4 @@ $(document).ready(function() {
{% block options %}
- {% if user.is_authenticated and not user.userprofile.is_temporary %}
- <a href="{% url 'exercise:exercise:add' %}" class="btn btn-success btn-sm">
- {% translate "Add new exercise" %}
- </a>
- {% else %}
- <a href="#" class="btn btn-success btn-sm disabled">
- {% translate "Add new exercise" %}<br>
- <small>
- {% translate "Only registered users can do this" %}
- </small>
- </a>
- {% endif %}
{% endblock %}
diff --git a/wger/exercises/templates/exercise/view.html b/wger/exercises/templates/exercise/view.html
index 6ca5014c0..696c196be 100644
--- a/wger/exercises/templates/exercise/view.html
+++ b/wger/exercises/templates/exercise/view.html
@@ -289,16 +289,6 @@ $(document).ready(function() {
{# Side bar #}
{# #}
{% block sidebar %}
-{% if user.is_authenticated and not user.userprofile.is_temporary and not perms.exercises %}
-<h5>{% translate "Options" %}</h5>
-<p>
- <a href="{% url 'exercise:exercise:correct' exercise.id %}">
- <span class="{% fa_class 'edit' %}"></span>
- {% translate "Submit a correction" %}
- </a>
-</p>
-{% endif %}
-
{% license_sidebar exercise.license exercise.license_author %}
{% endblock %}
@@ -316,11 +306,6 @@ $(document).ready(function() {
{% translate "Options" %}
</button>
<div class="dropdown-menu">
- <a href="{% url 'exercise:exercise:edit' exercise.id %}" class="dropdown-item">
- <span class="{% fa_class 'edit' %}"></span>
- {% translate "Edit" %}
- </a>
- <div role="separator" class="dropdown-divider"></div>
<a href="{% url 'exercise:exercise:delete' exercise.id %}" class="wger-modal-dialog dropdown-item">
<span class="{% fa_class 'trash' %}"></span>
{% translate "Delete" %}
diff --git a/wger/exercises/templates/muscles/overview.html b/wger/exercises/templates/muscles/overview.html
index cfe292ddd..efa5b000e 100644
--- a/wger/exercises/templates/muscles/overview.html
+++ b/wger/exercises/templates/muscles/overview.html
@@ -103,16 +103,4 @@
Options
-->
{% block options %}
- {% if user.is_authenticated and not user.userprofile.is_temporary %}
- <a href="{% url 'exercise:exercise:add' %}" class="btn btn-success btn-sm">
- {% translate "Add new exercise" %}
- </a>
- {% else %}
- <a href="#" class="btn btn-success btn-sm disabled">
- {% translate "Add new exercise" %}<br>
- <small>
- {% translate "Only registered users can do this" %}
- </small>
- </a>
- {% endif %}
{% endblock %}
diff --git a/wger/exercises/tests/test_corrected_exercise.py b/wger/exercises/tests/test_corrected_exercise.py
deleted file mode 100644
index 87de3a928..000000000
--- a/wger/exercises/tests/test_corrected_exercise.py
+++ /dev/null
@@ -1,85 +0,0 @@
-# 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
-
-# Django
-from django.core import mail
-from django.urls import reverse
-
-# wger
-from wger.core.tests.base_testcase import WgerTestCase
-from wger.exercises.models import Exercise
-
-
-class ExercisesCorrectionTestCase(WgerTestCase):
- """
- 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}), {
- 'name': 'my test exercise',
- 'license': 2,
- 'description': description,
- 'muscles': [3],
- 'category': 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/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index acd536240..e122db72f 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -108,8 +108,6 @@ class ExerciseIndexTestCase(WgerTestCase):
self.assertEqual(exercise_2.id, 1)
self.assertEqual(exercise_2.name, "An exercise")
- self.assertContains(response, 'Add new exercise')
-
# Only authorized users see the edit links
if admin:
self.assertNotContains(response, 'Only registered users can do this')
@@ -232,173 +230,6 @@ class ExercisesTestCase(WgerTestCase):
Exercise test case
"""
- def add_exercise_user_fail(self):
- """
- Helper function to test adding exercises by users that aren't authorized
- """
-
- # Add an exercise
- count_before = Exercise.objects.count()
- response = self.client.post(
- reverse('exercise:exercise:add'), {
- 'name': random_string(),
- 'license': 1,
- 'exercise_base': {
- 'category': 2,
- 'muscles': [1, 2]
- }
- }
- )
- count_after = Exercise.objects.count()
- self.assertIn(response.status_code, STATUS_CODES_FAIL)
-
- # Exercise was not added
- self.assertEqual(count_before, count_after)
-
- def test_add_exercise_temp_user(self):
- """
- Tests adding an exercise with a logged in demo user
- """
-
- self.user_login('demo')
- self.add_exercise_user_fail()
-
- def test_add_exercise_no_user(self):
- """
- Tests adding an exercise with a logged out (anonymous) user
- """
-
- self.user_logout()
- self.add_exercise_user_fail()
- self.user_logout()
-
- def test_add_exercise_name_too_similar_fail(self):
- """
- Tests that adding an exercise with a name that is too similar
- to an existing exercise fails
- """
- count_before = Exercise.objects.count()
- response = self.client.post(
- reverse('exercise:exercise:add'), {
- 'category': 2,
- 'name': 'Squats',
- 'license': 1,
- 'muscles': [1, 2]
- }
- )
- count_after = Exercise.objects.count()
- self.assertIn(response.status_code, STATUS_CODES_FAIL)
-
- # Exercise was not added
- self.assertEqual(count_before, count_after)
-
- def add_exercise_success(self, admin=False):
- """
- Tests adding/editing an exercise with a user with enough rights to do this
- """
-
- # Add an exercise
- count_before = Exercise.objects.count()
- description = 'a nice, long and accurate description for the exercise'
- name = random_string()
- response = self.client.post(
- reverse('exercise:exercise:add'), {
- 'name': name,
- 'license': 1,
- 'description': description,
- 'category': 2,
- 'muscles': [1, 2]
- }
- )
- count_after = Exercise.objects.count()
- self.assertEqual(response.status_code, 302)
- new_location = response['Location']
- self.assertEqual(count_before + 1, count_after, 'Exercise was not added')
-
- response = self.client.get(new_location)
- exercise_id = response.context['exercise'].id
-
- # Exercise was saved
- exercise = Exercise.objects.get(pk=exercise_id)
- if admin:
- self.assertEqual(exercise.license_author, 'testserver')
- else:
- self.assertEqual(exercise.license_author, 'test')
-
- response = self.client.get(reverse('exercise:exercise:view', kwargs={'id': exercise_id}))
- self.assertEqual(response.status_code, 200)
-
- # Navigation tab
- self.assertEqual(response.context['active_tab'], WORKOUT_TAB)
-
- exercise_1 = Exercise.objects.get(pk=exercise_id)
- self.assertEqual(exercise_1.name, name)
-
- # Wrong category - adding
- response = self.client.post(
- reverse('exercise:exercise:add'), {
- 'category': 111,
- 'name': random_string(),
- 'license': 1,
- 'muscles': [1, 2]
- }
- )
- self.assertTrue(response.context['form'].errors['category'])
-
- # Wrong category - editing
- response = self.client.post(
- reverse('exercise:exercise:edit', kwargs={'pk': '1'}), {
- 'category': 111,
- 'name': random_string(),
- 'license': 1,
- 'muscles': [1, 2]
- }
- )
- if admin:
- self.assertTrue(response.context['form'].errors['category'])
- else:
- self.assertIn(response.status_code, STATUS_CODES_FAIL)
-
- # No muscles - adding
- response = self.client.post(
- reverse('exercise:exercise:add'), {
- 'category': 1,
- 'name': random_string(),
- 'license': 1,
- 'muscles': []
- }
- )
- self.assertEqual(response.status_code, 302)
-
- # No muscles - editing
- response = self.client.post(
- reverse('exercise:exercise:edit', kwargs={'pk': '1'}), {
- 'category': 1,
- 'name': random_string(),
- 'license': 1,
- 'muscles': []
- }
- )
- if admin:
- self.assertEqual(response.status_code, 302)
- else:
- self.assertIn(response.status_code, STATUS_CODES_FAIL)
-
- def test_add_exercise_success(self):
- """
- Tests adding/editing an exercise with a user with enough rights to do this
- """
- self.user_login('admin')
- 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(admin=False)
- self.assertEqual(len(mail.outbox), 2)
-
def search_exercise(self, fail=True):
"""
Helper function to test searching for exercises
diff --git a/wger/exercises/urls.py b/wger/exercises/urls.py
index a3630d155..a7721185c 100644
--- a/wger/exercises/urls.py
+++ b/wger/exercises/urls.py
@@ -207,21 +207,6 @@ patterns_exercise = [
exercises.view,
name='view',
),
- path(
- 'add/',
- login_required(exercises.ExerciseAddView.as_view()),
- name='add',
- ),
- path(
- '<int:pk>/edit/',
- exercises.ExerciseUpdateView.as_view(),
- name='edit',
- ),
- path(
- '<int:pk>/correct',
- exercises.ExerciseCorrectView.as_view(),
- name='correct',
- ),
path(
'<int:pk>/delete/',
exercises.ExerciseDeleteView.as_view(),
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index 8563f5743..8fc122077 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -20,7 +20,6 @@ import uuid
# Django
from django.conf import settings
from django.contrib import messages
-from django.contrib.auth.decorators import permission_required
from django.contrib.auth.mixins import (
LoginRequiredMixin,
PermissionRequiredMixin,
@@ -49,7 +48,6 @@ from django.urls import (
reverse,
reverse_lazy,
)
-from django.utils.cache import patch_vary_headers
from django.utils.translation import (
gettext as _,
gettext_lazy,
@@ -69,7 +67,6 @@ from crispy_forms.layout import (
)
# wger
-from wger.config.models import LanguageConfig
from wger.exercises.models import (
Equipment,
Exercise,
@@ -84,10 +81,7 @@ from wger.utils.generic_views import (
WgerFormMixin,
)
from wger.utils.helpers import levenshtein
-from wger.utils.language import (
- load_item_languages,
- load_language,
-)
+from wger.utils.language import load_language
from wger.utils.widgets import TranslatedSelectMultiple
from wger.weight.helpers import process_log_entries
@@ -226,175 +220,6 @@ class ExerciseForm(ModelForm):
return name
-class ExercisesEditAddView(WgerFormMixin):
- """
- Generic view to subclass from for exercise adding and editing, since they
- share all this settings
- """
- model = Exercise
- sidebar = 'exercise/form.html'
- title = gettext_lazy('Add exercise')
- custom_js = 'wgerInitTinymce();'
- clean_html = ('description', )
-
- def get_form_class(self):
- return ExerciseForm
-
- def get_form(self, form_class=None):
- form = super(ExercisesEditAddView, self).get_form(form_class)
- exercise = self.get_form_kwargs()['instance']
- if exercise is not None:
- form.fields['category'].initial = exercise.exercise_base.category
- form.fields['equipment'].initial = exercise.exercise_base.equipment.all()
- form.fields['muscles'].initial = exercise.exercise_base.muscles.all()
- form.fields['muscles_secondary'].initial = \
- exercise.exercise_base.muscles_secondary.all()
- form.helper.layout = Layout(
- "name",
- "description",
- "category",
- "equipment",
- Row(
- Column('muscles', css_class='form-group col-6 mb-0'),
- Column('muscles_secondary', css_class='form-group col-6 mb-0'),
- css_class='form-row'
- ),
- Row(
- Column('license', css_class='form-group col-6 mb-0'),
- Column('license_author', css_class='form-group col-6 mb-0'),
- css_class='form-row'
- ),
- )
- return form
-
- def form_valid(self, form):
- base = ExerciseBase.objects.filter(id=form.instance.id)
- base.equipment.set(form.cleaned_data['equipment'].all())
- base.muscles.set(form.cleaned_data['muscles'].all())
- base.muscles_secondary.set(form.cleaned_data['muscles_secondary'].all())
-
- form.instance.exercise_base = base
- form.instance.save()
- return super(ExercisesEditAddView, self).form_valid(form)
-
-
-class ExerciseUpdateView(
- ExercisesEditAddView,
- LoginRequiredMixin,
- PermissionRequiredMixin,
- UpdateView,
-):
- """
- Generic view to update an existing exercise
- """
- permission_required = 'exercises.change_exercise'
-
- def get_context_data(self, **kwargs):
- context = super(ExerciseUpdateView, self).get_context_data(**kwargs)
- context['title'] = _('Edit {0}').format(self.object.name)
- return context
-
-
-class ExerciseAddView(ExercisesEditAddView, LoginRequiredMixin, CreateView):
- """
- Generic view to add a new exercise
- """
-
- def form_valid(self, form):
- """
- Set language and author
- """
- form.instance.language = load_language()
- form.instance.set_author(self.request)
-
- # Try retrieving a base exercise first
- existing = ExerciseBase.objects.filter(
- category=ExerciseCategory.objects.get(name=form.cleaned_data['category'])
- )
- for elem in form.cleaned_data['equipment'].all():
- existing = existing.filter(equipment=elem)
- for elem in form.cleaned_data['muscles'].all():
- existing = existing.filter(muscles=elem)
- for elem in form.cleaned_data['muscles_secondary'].all():
- existing = existing.filter(muscles_secondary=elem)
-
- # Create a new exercise base
- if not existing:
- exercise_base = ExerciseBase.objects.create(
- category=form.cleaned_data['category'],
- license=form.cleaned_data['license'],
- license_author=form.cleaned_data['license_author']
- )
-
- exercise_base.equipment.set(form.cleaned_data['equipment'].all())
- exercise_base.muscles.set(form.cleaned_data['muscles'].all())
- exercise_base.muscles_secondary.set(form.cleaned_data['muscles_secondary'].all())
- exercise_base.save()
- else:
- exercise_base = existing.first()
-
- form.instance.exercise_base = exercise_base
- form.instance.save()
-
- return super(ExerciseAddView, self).form_valid(form)
-
- def dispatch(self, request, *args, **kwargs):
- """
- Demo users can't submit exercises
- """
- if request.user.userprofile.is_temporary:
- return HttpResponseForbidden()
-
- return super(ExerciseAddView, self).dispatch(request, *args, **kwargs)
-
-
-class ExerciseCorrectView(ExercisesEditAddView, LoginRequiredMixin, UpdateView):
- """
- Generic view to update an existing exercise
- """
- 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['title'] = _('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(
- str(subject),
- str(message),
- fail_silently=True,
- )
-
- messages.success(self.request, self.messages)
- return HttpResponseRedirect(
- reverse('exercise:exercise:view', kwargs={'id': self.object.id})
- )
-
-
class ExerciseDeleteView(
WgerDeleteMixin,
LoginRequiredMixin,