Files
2026-03-11 13:19:25 -04:00

81 lines
2.8 KiB
Python

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
#Done
@route_get("populate_workouts", args ={})
def populate_workouts(params):
init_starting_exs()
return "Workouts populated."
#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.")
#Done
@route_get("full_body", args={}) #Returns one random exercise from each muscle group
def workout_full_body(params):
all_ex = {'cardio':[],'legs':[],'arms':[],'back':[],'chest':[],"abs":[]}
for mg in all_ex.keys():
rand_ex = choice(Exercise.objects.filter(musclegroup=mg))
all_ex[mg].append(rand_ex.__repr__())
return all_ex
#Done
@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):
ex_name = params["name"]
category = params["category to edit"]
new_val = params["new value"]
ex = Exercise.objects.get(name=ex_name)
ex_dict = ex.to_dict()
ex_dict[category] = new_val
edited = Exercise.from_dict(ex_dict)
ex.delete()
edited.save()
return "Updates: "+str(edited.__repr__())
#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.")