Add a basic random view

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

8
.idea/watcherTasks.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<enabled-global>
<option value="pre-commit" />
</enabled-global>
</component>
</project>

View file

@ -28,6 +28,11 @@
</component> </component>
<component name="TemplatesService"> <component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Django" /> <option name="TEMPLATE_CONFIGURATION" value="Django" />
<option name="TEMPLATE_FOLDERS">
<list>
<option value="$MODULE_DIR$/src/exercises/templates" />
</list>
</option>
</component> </component>
<component name="TestRunnerService"> <component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="pytest" /> <option name="PROJECT_TEST_RUNNER" value="pytest" />

View file

@ -1,3 +1,14 @@
from django.contrib import admin from django.contrib import admin
from django.contrib.admin import register
# Register your models here. from exercises.models import BodyRegion, Exercise
@register(BodyRegion)
class BodyRegionAdmin(admin.ModelAdmin):
list_display = ["name"]
@register(Exercise)
class ExerciseAdmin(admin.ModelAdmin):
list_display = ["name", "get_body_regions", "rating"]

View file

@ -0,0 +1,31 @@
# Generated by Django 3.1.3 on 2020-11-21 10:49
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='BodyRegion',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=250)),
],
),
migrations.CreateModel(
name='Exercise',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=250)),
('rating', models.PositiveSmallIntegerField(help_text='From 1 (strengthening) to 5 (cardio)', validators=[django.core.validators.MaxValueValidator(5), django.core.validators.MinValueValidator(0)])),
('body_regions', models.ManyToManyField(related_name='exercises', to='exercises.BodyRegion')),
],
),
]

View file

@ -0,0 +1,19 @@
# Generated by Django 3.1.3 on 2020-11-21 10:51
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('exercises', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='exercise',
name='rating',
field=models.PositiveSmallIntegerField(help_text='From 1 (strengthening) to 5 (cardio)', validators=[django.core.validators.MaxValueValidator(5), django.core.validators.MinValueValidator(1)]),
),
]

View file

@ -1,3 +1,46 @@
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models from django.db import models
# Create your models here.
class BodyRegion(models.Model):
name = models.CharField(max_length=250)
def __str__(self):
return self.name
class ExerciseManager(models.Manager):
def get_mostly_cardio(self):
return self.all().filter(rating__gt=3)
def get_mostly_strengthening(self):
return self.all().filter(rating__lt=3)
def get_balanced(self):
return self.all().filter(rating=3)
class Exercise(models.Model):
name = models.CharField(max_length=250)
rating = models.PositiveSmallIntegerField(
validators=[MaxValueValidator(5), MinValueValidator(1)],
help_text="From 1 (strengthening) to 5 (cardio)",
)
body_regions = models.ManyToManyField(BodyRegion, "exercises")
objects = ExerciseManager()
def __str__(self):
return self.name
def is_mostly_cardio(self):
return self.rating > 3
def is_mostly_strengthening(self):
return self.rating < 3
def is_balanced(self):
return self.rating == 3
def get_body_regions(self):
return ", ".join(map(str, self.body_regions.all()))

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>

View file

@ -0,0 +1,12 @@
{% extends "exercises/base.html" %}
{% block title %}
Random exercises
{% endblock %}
{% block content %}
<h1>Random exercises</h1>
<ul>
{% for exercise in exercises %}
<li>{{ exercise.name }}: {{ exercise.get_body_regions }} ({{ exercise.rating }}/5)</li>
{% endfor %}
</ul>
{% endblock %}

23
src/exercises/urls.py Normal file
View file

@ -0,0 +1,23 @@
"""workout_suggest URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
from exercises.views import RandomExercisesView
app_name = "exercises"
urlpatterns = [
path("random", RandomExercisesView.as_view(), name="random"),
]

View file

@ -1,3 +1,11 @@
from django.shortcuts import render from django.views import generic
# Create your views here. from exercises.models import Exercise
class RandomExercisesView(generic.ListView):
model = Exercise
context_object_name = "exercises"
def get_queryset(self):
return Exercise.objects.order_by("?")[:5]

View file

@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import include, path, re_path
urlpatterns = [ urlpatterns = [
path("exercises/", include("exercises.urls", namespace="exercises")),
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
] ]