Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add additional filters for fields from exercise base

Roland Geider881787b1d0fb
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 5649b08d7..8470ab55c 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -49,7 +49,7 @@ Upgrade steps from 1.9:
* `#368`_, `#379`_, `#426`_ (thanks `@austin-leung`_), `#499`_, `#505`_, `#504`_,
`#511`_, `#516`_, `#522`_, `#554`_ and `#560`_ (thanks `@sandilsranasinghe`_),
`#564`_, `#565`_, `#615`_, `#560`_ (thanks `@bradsk88`_), `#617`_ (thanks `@Sidrah-Madiha`_),
- `#636`_, `#640`_
+ `#636`_, `#640`_, `#642`_
🧰 Maintenance:
@@ -124,6 +124,7 @@ Upgrade steps from 1.9:
.. _#631: https://github.com/wger-project/wger/issues/631
.. _#636: https://github.com/wger-project/wger/issues/636
.. _#640: https://github.com/wger-project/wger/issues/640
+.. _#642: https://github.com/wger-project/wger/issues/642
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 554981021..5595ffa59 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -15,6 +15,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+# Standard Library
+import logging
+
# Django
from django.utils.translation import ugettext as _
@@ -56,6 +59,9 @@ from wger.utils.language import (
from wger.utils.permissions import CreateOnlyPermission
+logger = logging.getLogger(__name__)
+
+
class ExerciseBaseViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for exercise base objects. For a read-only endpoint with all
@@ -80,13 +86,12 @@ class ExerciseViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = ExerciseSerializer
permission_classes = (IsAuthenticatedOrReadOnly, CreateOnlyPermission)
ordering_fields = '__all__'
- filterset_fields = ('creation_date',
+ filterset_fields = ('uuid',
+ 'creation_date',
'description',
'language',
'status',
- 'name',
- 'license',
- 'license_author')
+ 'name')
def perform_create(self, serializer):
"""
@@ -98,6 +103,50 @@ class ExerciseViewSet(viewsets.ReadOnlyModelViewSet):
obj.set_author(self.request)
obj.save()
+ def get_queryset(self):
+ """Add additional filters for fields from exercise base"""
+
+ qs = Exercise.objects.accepted()
+
+ category = self.request.query_params.get('category')
+ muscles = self.request.query_params.get('muscles')
+ muscles_secondary = self.request.query_params.get('muscles_secondary')
+ equipment = self.request.query_params.get('equipment')
+ license = self.request.query_params.get('license')
+
+ if category:
+ try:
+ qs = qs.filter(exercise_base__category_id=int(category))
+ except ValueError:
+ logger.info(f"Got {category} as category ID")
+
+ if muscles:
+ try:
+ qs = qs.filter(exercise_base__muscles__in=[int(m) for m in muscles.split(',')])
+ except ValueError:
+ logger.info(f"Got {muscles} as muscle IDs")
+
+ if muscles_secondary:
+ try:
+ muscle_ids = [int(m) for m in muscles_secondary.split(',')]
+ qs = qs.filter(exercise_base__muscles_secondary__in=muscle_ids)
+ except ValueError:
+ logger.info(f"Got '{muscles_secondary}' as secondary muscle IDs")
+
+ if equipment:
+ try:
+ qs = qs.filter(exercise_base__equipment__in=[int(e) for e in equipment.split(',')])
+ except ValueError:
+ logger.info(f"Got {equipment} as equipment IDs")
+
+ if license:
+ try:
+ qs = qs.filter(exercise_base__license_id=int(license))
+ except ValueError:
+ logger.info(f"Got {license} as license ID")
+
+ return qs
+
@api_view(['GET'])
def search(request):
@@ -235,10 +284,9 @@ class ExerciseCommentViewSet(viewsets.ReadOnlyModelViewSet):
ordering_fields = '__all__'
filterset_fields = ('comment',
'exercise')
- """
- Filter by language for exercise comments
- """
+
def get_queryset(self):
+ """Filter by language for exercise comments"""
qs = ExerciseComment.objects.all()
language = self.request.query_params.get('language')
if language:
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index 0a1e16102..246aca899 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -227,7 +227,7 @@ class ExerciseBase(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
@property
def get_languages(self):
"""
- Returns the languages from the exercises tha tuse this base
+ Returns the languages from the exercises that use this base
"""
return [exercise.language for exercise in self.exercises.all()]
diff --git a/wger/software/templates/api.html b/wger/software/templates/api.html
index 2e0652307..46a0b40c1 100644
--- a/wger/software/templates/api.html
+++ b/wger/software/templates/api.html
@@ -164,7 +164,9 @@ generated. At the moment it is not possible to register via the API.</p>
</p>
<p>
Note that it is not currently possible to specify more than one value, e.g.
- category 1 or 2.
+ category 1 or 2. The <strong>only</strong> exception to this is the exercises endpoint,
+ there it is possible to OR different values, e.g. <code>?muscles=2,7</code>
+ which will search for exercises that train muscle 2 OR 7.
</p>
<p>Some examples:</p>