Skip to content

Commit

Permalink
Handle filtering on the json field
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed Dec 6, 2024
1 parent f1b3661 commit 7941e73
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 41 deletions.
4 changes: 3 additions & 1 deletion wger/manager/api/consts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CONFIG_FIELDS = (
BASE_CONFIG_FILTER_FIELDS = (
'id',
'slot_entry',
'iteration',
Expand All @@ -8,3 +8,5 @@
'need_log_to_apply',
'repeat',
)

BASE_CONFIG_FIELDS = BASE_CONFIG_FILTER_FIELDS + ('requirements',)
29 changes: 29 additions & 0 deletions wger/manager/api/filtersets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.

# Third Party
from django_filters import (
UnknownFieldBehavior,
rest_framework as filters,
)

# wger
from wger.manager.api.consts import BASE_CONFIG_FILTER_FIELDS


class BaseConfigFilterSet(filters.FilterSet):
class Meta:
fields = BASE_CONFIG_FILTER_FIELDS
unknown_field_behavior = UnknownFieldBehavior.IGNORE
57 changes: 34 additions & 23 deletions wger/manager/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
Expand All @@ -15,11 +13,16 @@
# 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/>.


# Third Party
from rest_framework import serializers

# wger
from wger.manager.api.consts import CONFIG_FIELDS
from wger.manager.api.consts import (
BASE_CONFIG_FIELDS,
BASE_CONFIG_FILTER_FIELDS,
)
from wger.manager.api.validators import validate_requirements
from wger.manager.models import (
Day,
MaxRepsConfig,
Expand Down Expand Up @@ -81,104 +84,112 @@ class Meta:
)


class WeightConfigSerializer(serializers.ModelSerializer):
class BaseConfigSerializer(serializers.ModelSerializer):
"""
Base Config serializer
"""

requirements = serializers.JSONField(validators=[validate_requirements])


class WeightConfigSerializer(BaseConfigSerializer):
"""
Weight Config serializer
"""

class Meta:
model = WeightConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class MaxWeightConfigSerializer(serializers.ModelSerializer):
class MaxWeightConfigSerializer(BaseConfigSerializer):
"""
Max Weight Config serializer
"""

class Meta:
model = MaxWeightConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class RepsConfigSerializer(serializers.ModelSerializer):
class RepsConfigSerializer(BaseConfigSerializer):
"""
Repetition Config serializer
"""

class Meta:
model = RepsConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class MaxRepsConfigSerializer(serializers.ModelSerializer):
class MaxRepsConfigSerializer(BaseConfigSerializer):
"""
Max Repetition Config serializer
"""

class Meta:
model = MaxRepsConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class SetNrConfigSerializer(serializers.ModelSerializer):
class SetNrConfigSerializer(BaseConfigSerializer):
"""
Set Nr config serializer
"""

class Meta:
model = SetsConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class MaxSetNrConfigSerializer(serializers.ModelSerializer):
class MaxSetNrConfigSerializer(BaseConfigSerializer):
"""
Max Set Nr config serializer
"""

class Meta:
model = MaxSetsConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class RiRConfigSerializer(serializers.ModelSerializer):
class RiRConfigSerializer(BaseConfigSerializer):
"""
RiR Config serializer
"""

class Meta:
model = RiRConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class MaxRiRConfigSerializer(serializers.ModelSerializer):
class MaxRiRConfigSerializer(BaseConfigSerializer):
"""
RiR Config serializer
"""

class Meta:
model = MaxRiRConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class RestConfigSerializer(serializers.ModelSerializer):
class RestConfigSerializer(BaseConfigSerializer):
"""
Rest Config serializer
"""

class Meta:
model = RestConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class MaxRestConfigSerializer(serializers.ModelSerializer):
class MaxRestConfigSerializer(BaseConfigSerializer):
"""
Rest Config serializer
"""

class Meta:
model = MaxRestConfig
fields = CONFIG_FIELDS
fields = BASE_CONFIG_FIELDS


class SlotEntryStructureSerializer(serializers.ModelSerializer):
Expand Down
34 changes: 34 additions & 0 deletions wger/manager/api/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.

# Third Party
from rest_framework import serializers


def validate_requirements(value: dict | None):
"""Validates the requirements field."""

if value is None:
return

if not isinstance(value, dict):
raise serializers.ValidationError('Requirements must be a JSON object.')

required_keys = {'rules'} # Set of required keys
if not all(key in value for key in required_keys):
raise serializers.ValidationError("Missing required keys: 'rules'") # More specific

if 'rules' in value and not isinstance(value['rules'], list):
raise serializers.ValidationError("'rules' must be a list.")
15 changes: 13 additions & 2 deletions wger/manager/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

# wger
from wger.exercises.models import Exercise
from wger.manager.api.consts import CONFIG_FIELDS
from wger.manager.api.consts import BASE_CONFIG_FIELDS
from wger.manager.api.filtersets import BaseConfigFilterSet
from wger.manager.api.serializers import (
DaySerializer,
LogDisplaySerializer,
Expand Down Expand Up @@ -595,7 +596,7 @@ class AbstractConfigViewSet(WgerOwnerObjectModelViewSet):

is_private = True
ordering_fields = '__all__'
filterset_fields = CONFIG_FIELDS
filterset_fields = BASE_CONFIG_FIELDS

def get_owner_objects(self):
"""
Expand All @@ -610,6 +611,7 @@ class WeightConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = WeightConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -628,6 +630,7 @@ class MaxWeightConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = MaxWeightConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -648,6 +651,7 @@ class RepsConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = RepsConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -666,6 +670,7 @@ class MaxRepsConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = MaxRepsConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -684,6 +689,7 @@ class SetsConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = SetNrConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -702,6 +708,7 @@ class MaxSetsConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = MaxSetNrConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -720,6 +727,7 @@ class RestConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = RestConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -738,6 +746,7 @@ class MaxRestConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = MaxRestConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -756,6 +765,7 @@ class RiRConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = RiRConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand All @@ -774,6 +784,7 @@ class MaxRiRConfigViewSet(AbstractConfigViewSet):
"""

serializer_class = MaxRiRConfigSerializer
filterset_class = BaseConfigFilterSet

def get_queryset(self):
"""
Expand Down
8 changes: 4 additions & 4 deletions wger/manager/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#
# You should have received a copy of the GNU Affero General Public License

# Third Party
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

# Django
from django.forms import (
BooleanField,
Expand All @@ -33,6 +29,10 @@
gettext_lazy,
)

# Third Party
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

# wger
from wger.manager.models import Workout

Expand Down
Loading

0 comments on commit 7941e73

Please sign in to comment.