From eb7da95a4f4da86ec7672d16323ddf3f1d223dd2 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 21 Nov 2020 15:40:35 +0100 Subject: [PATCH] Allow filtering exercises based on rating --- src/exercises/forms.py | 13 +++++++++ src/exercises/models.py | 4 +-- src/exercises/templates/exercises/base.html | 1 + .../templates/exercises/exercise_list.html | 6 +++- src/exercises/views.py | 28 +++++++++++++++++-- 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/exercises/forms.py diff --git a/src/exercises/forms.py b/src/exercises/forms.py new file mode 100644 index 0000000..109b5ec --- /dev/null +++ b/src/exercises/forms.py @@ -0,0 +1,13 @@ +from django import forms + + +class QueryForm(forms.Form): + CARDIO_CHOICE = "cardio" + STRENGTHENING_CHOICE = "strengthening" + BALANCED_CHOICE = "balanced" + RATING_CHOICES = [ + (CARDIO_CHOICE, "Cardio"), + (STRENGTHENING_CHOICE, "Strengthening"), + (BALANCED_CHOICE, "Balanced"), + ] + rating = forms.ChoiceField(choices=RATING_CHOICES, widget=forms.RadioSelect) diff --git a/src/exercises/models.py b/src/exercises/models.py index fcaad35..c065f0c 100644 --- a/src/exercises/models.py +++ b/src/exercises/models.py @@ -11,10 +11,10 @@ class BodyRegion(models.Model): class ExerciseManager(models.Manager): def get_mostly_cardio(self): - return self.all().filter(rating__gt=3) + return self.all().filter(rating__gte=3) def get_mostly_strengthening(self): - return self.all().filter(rating__lt=3) + return self.all().filter(rating__lte=3) def get_balanced(self): return self.all().filter(rating=3) diff --git a/src/exercises/templates/exercises/base.html b/src/exercises/templates/exercises/base.html index 98bdbd7..6311ac0 100644 --- a/src/exercises/templates/exercises/base.html +++ b/src/exercises/templates/exercises/base.html @@ -2,6 +2,7 @@ + {% block title %}{% endblock %} diff --git a/src/exercises/templates/exercises/exercise_list.html b/src/exercises/templates/exercises/exercise_list.html index 42b021a..bd9b1f6 100644 --- a/src/exercises/templates/exercises/exercise_list.html +++ b/src/exercises/templates/exercises/exercise_list.html @@ -6,7 +6,11 @@

Random exercises

+
+ {{ form.as_p }} + +
{% endblock %} diff --git a/src/exercises/views.py b/src/exercises/views.py index 3bb2dee..d35803b 100644 --- a/src/exercises/views.py +++ b/src/exercises/views.py @@ -1,11 +1,35 @@ from django.views import generic +from django.views.generic.edit import FormMixin +from exercises.forms import QueryForm from exercises.models import Exercise -class RandomExercisesView(generic.ListView): +class RandomExercisesView(generic.ListView, FormMixin): model = Exercise context_object_name = "exercises" + form_class = QueryForm def get_queryset(self): - return Exercise.objects.order_by("?")[:5] + form = self.get_form() + base_queryset = self.get_base_queryset(form) + return base_queryset.order_by("?")[:5] + + def get_base_queryset(self, form): + if not form.is_valid(): + return Exercise.objects.all() + data = form.cleaned_data + rating = data.get("rating") + if rating == QueryForm.BALANCED_CHOICE: + return Exercise.objects.get_balanced() + elif rating == QueryForm.CARDIO_CHOICE: + return Exercise.objects.get_mostly_cardio() + elif rating == QueryForm.STRENGTHENING_CHOICE: + return Exercise.objects.get_mostly_strengthening() + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + get = self.request.GET + if get: + kwargs.update({"data": get}) + return kwargs