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