Allow filtering exercises based on rating

This commit is contained in:
Gabriel Augendre 2020-11-21 15:40:35 +01:00
parent f0a8dc9136
commit eb7da95a4f
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
5 changed files with 47 additions and 5 deletions

13
src/exercises/forms.py Normal file
View file

@ -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)

View file

@ -11,10 +11,10 @@ class BodyRegion(models.Model):
class ExerciseManager(models.Manager): class ExerciseManager(models.Manager):
def get_mostly_cardio(self): def get_mostly_cardio(self):
return self.all().filter(rating__gt=3) return self.all().filter(rating__gte=3)
def get_mostly_strengthening(self): def get_mostly_strengthening(self):
return self.all().filter(rating__lt=3) return self.all().filter(rating__lte=3)
def get_balanced(self): def get_balanced(self):
return self.all().filter(rating=3) return self.all().filter(rating=3)

View file

@ -2,6 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
</head> </head>
<body> <body>

View file

@ -6,7 +6,11 @@
<h1>Random exercises</h1> <h1>Random exercises</h1>
<ul> <ul>
{% for exercise in exercises %} {% for exercise in exercises %}
<li>{{ exercise.name }}: {{ exercise.get_body_regions }} ({{ exercise.rating }}/5)</li> <li>{{ exercise.get_body_regions }}: {{ exercise.name }} ({{ exercise.rating }}/5)</li>
{% endfor %} {% endfor %}
</ul> </ul>
<form action="" method="get">
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %} {% endblock %}

View file

@ -1,11 +1,35 @@
from django.views import generic from django.views import generic
from django.views.generic.edit import FormMixin
from exercises.forms import QueryForm
from exercises.models import Exercise from exercises.models import Exercise
class RandomExercisesView(generic.ListView): class RandomExercisesView(generic.ListView, FormMixin):
model = Exercise model = Exercise
context_object_name = "exercises" context_object_name = "exercises"
form_class = QueryForm
def get_queryset(self): 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