Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routines #32

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions App/controllers/routine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# from App.models import Routine, Workout
# from App.database import db


# def create_routine(name, description, user_id):
# routine = Routine(name=name, description=description, user_id=user_id)
# db.session.add(routine)
# db.session.commit()
# return routine

# def get_all_routines():
# return Routine.query.all()

# def get_routine(id):
# return Routine.query.get(id)

# def get_user_routines(user_id):
# return Routine.query.filter_by(user_id=user_id).all()

# def update_routine(id, name=None, description=None):
# routine = Routine.query.get(id)
# if name is not None:
# routine.name = name
# if description is not None:
# routine.description = description
# db.session.commit()
# return routine

# def add_workout_to_routine(routine_id, workout_id):
# routine = Routine.query.get(routine_id)
# workout = Workout.query.get(workout_id)
# routine.workouts.append(workout)
# db.session.commit()

# def delete_routine(id):
# routine = Routine.query.get(id)
# db.session.delete(routine)
# db.session.commit()
39 changes: 37 additions & 2 deletions App/controllers/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from App.models import User
from App.models import User, Workout, Routine
from App.database import db

def create_user(username, password):
Expand Down Expand Up @@ -30,4 +30,39 @@ def update_user(id, username):
db.session.add(user)
return db.session.commit()
return None


def create_routine(name, description, user_id):
routine = Routine(name=name, description=description, user_id=user_id)
db.session.add(routine)
db.session.commit()
return routine

def get_all_routines():
return Routine.query.all()

def get_routine(id):
return Routine.query.get(id)

def get_user_routines(user_id):
return Routine.query.filter_by(user_id=user_id).all()

def update_routine(id, name=None, description=None):
routine = Routine.query.get(id)
if name is not None:
routine.name = name
if description is not None:
routine.description = description
db.session.commit()
return routine

def add_workout_to_routine(routine_id, workout_id):
routine = Routine.query.get(routine_id)
workout = Workout.query.get(workout_id)
routine.workouts.append(workout)
db.session.commit()

def delete_routine(id):
routine = Routine.query.get(id)
db.session.delete(routine)
db.session.commit()

75 changes: 75 additions & 0 deletions App/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,78 @@ def check_password(self, password):
"""Check hashed password."""
return check_password_hash(self.password, password)

class Workout(db.Model):
id = db.Column(db.Integer, primary_key=True)
exercise_name = db.Column(db.String, nullable=False)
exercise_image1 = db.Column(db.String, nullable=True)
exercise_image2 = db.Column(db.String, nullable=True)
muscle_group = db.Column(db.String, nullable=False)
equipment = db.Column(db.String, nullable=True)
rating = db.Column(db.Float, nullable=True)
description = db.Column(db.String, nullable=True)

def __init__(self, exercise_name, exercise_image1, exercise_image2, muscle_group, equipment, rating, description):
self.exercise_name = exercise_name
self.exercise_image1 = exercise_image1
self.exercise_image2 = exercise_image2
self.muscle_group = muscle_group
self.equipment = equipment
self.rating = rating
self.description = description

def get_json(self):
return {
'id': self.id,
'exercise_name': self.exercise_name,
'exercise_image1': self.exercise_image1,
'exercise_image2': self.exercise_image2,
'muscle_group': self.muscle_group,
'equipment': self.equipment,
'rating': self.rating,
'description': self.description
}

def save(self):
db.session.add(self)
db.session.commit()

def delete(self):
db.session.delete(self)
db.session.commit()

exercise_routine = db.Table('exercise_routine',
db.Column('routine_id', db.Integer, db.ForeignKey('routine.id'), primary_key=True),
db.Column('exercise_id', db.Integer, db.ForeignKey('exercise.id'), primary_key=True)
)

class Routine(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
description = db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
user = db.relationship('User', backref=db.backref('routines', lazy=True))
exercises = db.relationship('Exercise', secondary=exercise_routine, backref=db.backref('routines', lazy=True))

def __init__(self, name, description, user_id):
self.name = name
self.description = description
self.user_id = user_id

def get_json(self):
return {
'id': self.id,
'name': self.name,
'description': self.description,
'user_id': self.user_id,
'exercises': [exercise.to_dict() for exercise in self.exercises]
}

def add_exercise(self, exercise):
if exercise not in self.exercises:
self.exercises.append(exercise)
db.session.commit()

def remove_exercise(self, exercise):
if exercise in self.exercises:
self.exercises.remove(exercise)
db.session.commit()
24 changes: 23 additions & 1 deletion App/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,27 @@ <h1>Flask MVC</h1>
{% if is_authenticated %}
<p> Welcome {{current_user.username}} </p>
{% endif %}
<p>This is a boileplate flask application which follows the MVC pattern for structuring the project.</p>
<p>This is a boilerplate flask application which follows the MVC pattern for structuring the project.</p>

<h2>Workouts</h2>
{% if workouts %}
<div class="row">
{% for workout in workouts %}
<div class="col s12 m3">
<div class="card">
<div class="card-image">
<img src="{{workout.exercise_image1}}" alt="Exercise Image 1">
<img src="{{workout.exercise_image2}}" alt="Exercise Image 2">
</div>
<div class="card-content">
<p>{{ workout.exercise_name }} - {{ workout.description }}</p>
</div>
</div>
</div>
{% endfor %}
</div>
{% else %}
<p>No workouts found.</p>
{% endif %}

{% endblock %}
1 change: 1 addition & 0 deletions App/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ul id="nav-mobile" class="left">
<li><a href="/">Home</a></li>
<li><a href="/users">Users Jinja</a></li>
<li><a href="/routine">Routines</a></li>
{% if is_authenticated %}
<li><a href="/identify">Identify</a></li>
{% endif %}
Expand Down
Empty file added App/templates/routine_form.html
Empty file.
Empty file added App/templates/workouts.html
Empty file.
10 changes: 7 additions & 3 deletions App/views/index.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from flask import Blueprint, redirect, render_template, request, send_from_directory, jsonify
from App.models import db
from App.models import db, User, Workout, Routine
from App.controllers import create_user

index_views = Blueprint('index_views', __name__, template_folder='../templates')

@index_views.route('/', methods=['GET'])
def index_page():
return render_template('index.html')
workouts = Workout.query.all()
workout_id = 1 # replace with a valid workout ID
workout = Workout.query.get(workout_id)
return render_template('index.html', workouts=workouts, workout=workout)

@index_views.route('/init', methods=['GET'])
def init():
Expand All @@ -17,4 +20,5 @@ def init():

@index_views.route('/health', methods=['GET'])
def health_check():
return jsonify({'status':'healthy'})
return jsonify({'status':'healthy'})

Empty file added App/views/routine.py
Empty file.
60 changes: 58 additions & 2 deletions App/views/user.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from flask import Blueprint, render_template, jsonify, request, send_from_directory, flash, redirect, url_for
from flask_jwt_extended import jwt_required, current_user as jwt_current_user
from App.models import User, Workout, Routine

from.index import index_views

from App.controllers import (
create_user,
get_all_users,
get_all_users_json,
jwt_required
jwt_required,
create_routine,
get_all_routines,
get_routine,
get_user_routines,
update_routine,
add_workout_to_routine,
delete_routine
)

user_views = Blueprint('user_views', __name__, template_folder='../templates')
Expand Down Expand Up @@ -37,4 +45,52 @@ def create_user_endpoint():

@user_views.route('/static/users', methods=['GET'])
def static_user_page():
return send_from_directory('static', 'static-user.html')
return send_from_directory('static', 'static-user.html')

# Create routines
@user_views.route('/routine/create', methods=['POST'])
@jwt_required
def create_routine2():
data = request.json
routine = create_routine(data['name'], data['description'], jwt_current_user.id)
return jsonify(routine.get_json())

# Add workout to routine
@user_views.route('/routine/<int:routine_id>/add_workout', methods=['POST'])
@jwt_required
def add_workout(routine_id):
workout_id = request.form.get('workout_id')
add_workout_to_routine(routine_id, workout_id)
return redirect(url_for('user_views.edit_routine2', id=routine_id))

# View/Edit routine
@user_views.route('/routine/edit/<int:id>', methods=['GET', 'POST'])
@jwt_required
def edit_routine2(id):
routine = get_routine(id)
if not routine or routine.user_id != jwt_current_user.id:
return redirect(url_for('user_views.display_routines'))

if request.method == 'POST':
name = request.form.get('name')
description = request.form.get('description')
updated_routine = update_routine(id, name=name, description=description)
if updated_routine:
return redirect(url_for('user_views.display_routines'))

workouts = get_all_workouts()
return render_template('routine_form.html', form_action=url_for('user_views.edit_routine2', id=id), routine=routine, workouts=workouts)

# Delete routine
@user_views.route('/routine/delete/<int:id>', methods=['POST'])
@jwt_required
def delete_routine2(id):
routine = get_routine(id)
if not routine or routine.user_id != jwt_current_user.id:
return redirect(url_for('user_views.display_routines'))

deleted_routine = delete_routine(id)
if deleted_routine:
return redirect(url_for('user_views.display_routines'))

return 'Failed to delete routine', 400
Loading