Worked on views and models

This commit is contained in:
zoeyande2
2026-03-08 21:10:55 -04:00
parent f91987da30
commit 9927473dad
8 changed files with 181 additions and 1 deletions

61
workouts/app/views.py Normal file
View File

@@ -0,0 +1,61 @@
from banjo.urls import route_get, route_post
from banjo.http import BadRequest, NotFound
from app.models import Exercise
from random import choice, sample
from app.exlist import init_starting_exs
init_starting_exs()
#Done
@route_get("all", args = {})
def list_all_exercises_sorted(params):
exercises = Exercise.objects.all()
all_ex = {'cardio':[],'legs':[],'arms':[],'back':[],'chest':[],"abs":[]}
musclegroups = all_ex.keys()
for mg in musclegroups:
for ex in exercises:
if ex.musclegroup == mg:
all_ex[mg].append(ex.__repr__())
return all_ex
#Done
@route_get("workout_length", args= {"length": int}) #Returns n many exercises & instructions at random
def workout_with_length(params):
if Exercise.objects.all().count() >= params["length"]:
workout = []
xlist = sample(list(Exercise.objects.all()),params["length"])
for ex in xlist:
xinfo = ex.__repr__()
workout.append(xinfo)
return workout
else:
raise BadRequest("There are not enough exercises to pick that many.")
@route_get("full_body", args={}) #Returns one random exercise from each muscle group
def workout_full_body(params):
return "in progress"
@route_post("edit_exercise", args={"name":str,"category to edit":str,"new value":int}) #Allows a change to an exercise weight, reps, sets, time, or distance (numbers)
def edit_exercise(params):
return "in progress"
#Done
@route_post("new_exercise",args={"name":str,"musclegroup":str,"weight":int,"reps":int,"sets":int,"timemins":int,"distance":int})
def add_new_exercise(params):
if Exercise.objects.filter(name=params["name"]).exists():
return "This exercise already exists."
else:
exercise = Exercise.from_dict(params)
exercise.save()
return exercise.__repr__()
#Done
@route_get("delete_exercise", args ={"name":str}) #Done
def delete_exercise(params):
try:
ex = Exercise.objects.get(name=params["name"])
ex.delete()
return "Successfully deleted!"
except Exercise.DoesNotExist:
raise NotFound("No such exercise exists.")