-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
230 lines (189 loc) · 7.98 KB
/
app.py
File metadata and controls
230 lines (189 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from flask import Flask, render_template, request, redirect, url_for, session
from factories import MuscleQuestionFactory, OpenAIMuscleQuestionStrategy
from flask_sqlalchemy import SQLAlchemy
from functools import wraps
import openai
import time
import os
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'Nathan123'
app.config['UPLOAD_FOLDER'] = '/home/NathanBatista/mysite/static/uploads'
app.config['DEBUG'] = True
openai.api_key = ''
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
password = db.Column(db.String(200), nullable=False)
posts = db.relationship('Post', backref='user', lazy=True)
ratings = db.relationship('Rating', backref='user', lazy=True)
def __init__(self, name, password):
self.name = name
self.password = password
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
image = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
ratings = db.relationship('Rating', backref='post', lazy=True, cascade="all, delete-orphan")
class Rating(db.Model):
id = db.Column(db.Integer, primary_key=True)
score = db.Column(db.Integer, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
with app.app_context():
#db.drop_all()
db.create_all()
def calculate_average_rating(post_id):
ratings = Rating.query.filter_by(post_id=post_id).all()
if ratings:
average = sum(rating.score for rating in ratings) / len(ratings)
return f"{average:.2f}"
return None
def perguntar(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Você é um assistente útil."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message['content']
#Decorator para tempo de execução
def log_execution_time(f):
@wraps(f)
def decorated_function(*args, **kwargs):
start_time = time.time()
result = f(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
print(f"Function {f.__name__} executed in {execution_time:.4f} seconds")
return result
return decorated_function
@app.route('/', methods=['GET', 'POST'])
@log_execution_time
def homePage():
if 'user_id' not in session:
return redirect(url_for('login'))
posts = Post.query.all()
posts_with_ratings = [
{
'post': post,
'average_rating': calculate_average_rating(post.id)
} for post in posts
]
quiz_question = None
quiz_options = None
if 'quiz' in request.args:
strategy = OpenAIMuscleQuestionStrategy()
factory = MuscleQuestionFactory(strategy)
try:
quiz_question, quiz_options, correct_answer = factory.create_question()
session['correct_answer'] = correct_answer
session['quiz_options'] = quiz_options # Armazena as opções embaralhadas na sessão
except ValueError as e:
quiz_question = "Erro ao gerar a pergunta do quiz: " + str(e)
if request.method == 'POST' and 'answer' in request.form:
answer = request.form['answer']
correct_answer = session.get('correct_answer')
quiz_options = session.get('quiz_options') # Recupera as opções embaralhadas da sessão
if answer == correct_answer:
session['quiz_passed'] = True # Define que o usuário passou no quiz
return redirect(url_for('workout_plan'))
return render_template('homePage.html', posts_with_ratings=posts_with_ratings, quiz_question=quiz_question, quiz_options=quiz_options, enumerate=enumerate)
@app.route('/upload', methods=['GET', 'POST'])
@log_execution_time
def upload():
if 'user_id' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
file = request.files['file']
if file:
filename = file.filename
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
new_post = Post(image=filename, user_id=session['user_id'])
db.session.add(new_post)
db.session.commit()
return redirect(url_for('homePage'))
return redirect(url_for('homePage'))
@app.route('/post/<int:post_id>', methods=['GET', 'POST'])
@log_execution_time
def post(post_id):
if 'user_id' not in session:
return redirect(url_for('login'))
post = Post.query.get_or_404(post_id)
if request.method == 'POST':
rating = int(request.form['rating'])
new_rating = Rating(score=rating, user_id=session['user_id'], post_id=post_id)
db.session.add(new_rating)
db.session.commit()
return redirect(url_for('homePage'))
return render_template('homePage.html', posts=Post.query.all(), post=post)
@app.route('/delete/<int:id>', methods=['POST'])
@log_execution_time
def delete(id):
if 'user_id' not in session:
return redirect(url_for('login'))
post = Post.query.get(id)
if not post:
return redirect(url_for('homePage')) # Post não encontrado
if post.user_id != session['user_id']:
return redirect(url_for('homePage')) # Usuário não autorizado a excluir
db.session.delete(post)
db.session.commit()
return redirect(url_for('homePage'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
name = request.form['usernameLogin']
password = request.form['passwordLogin']
user = User.query.filter_by(name=name, password=password).first()
if user:
session['user_id'] = user.id
session['username'] = user.name
session['password'] = user.password
return redirect(url_for('homePage'))
else:
return render_template('auth.html', error='Invalid username or password', usuarios=User.query.all(), debug=app.config['DEBUG'])
return render_template('auth.html', usuarios=User.query.all(), debug=app.config['DEBUG'])
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
name = request.form['usernameRegister']
password = request.form['passwordRegister']
# Verifica se o nome de usuário já existe
existing_user = User.query.filter_by(name=name).first()
if existing_user:
return render_template('auth.html', error='Username already taken', usuarios=User.query.all(), debug=app.config['DEBUG'])
if name and password:
newUser = User(name=name, password=password)
db.session.add(newUser)
db.session.commit()
return redirect(url_for('login'))
return render_template('auth.html', error='Invalid username or password', usuarios=User.query.all(), debug=app.config['DEBUG'])
return render_template('auth.html', usuarios=User.query.all(), debug=app.config['DEBUG'])
@app.route('/logout')
@log_execution_time
def logout():
session.pop('user_id', None)
session.pop('username', None)
session.pop('password', None)
return redirect(url_for('homePage'))
@app.route('/quiz', methods=['GET'])
@log_execution_time
def quiz():
if 'user_id' not in session:
return redirect(url_for('login'))
return redirect(url_for('homePage', quiz='true'))
@app.route('/workout_plan')
@log_execution_time
def workout_plan():
if 'user_id' not in session:
return redirect(url_for('login'))
if not session.get('quiz_passed'):
return redirect(url_for('homePage'))
workout_plan = perguntar("Crie uma planilha de treino de musculação completa.")
return render_template('workout_plan.html', workout_plan=workout_plan)
if __name__ == '__main__':
app.run(debug=True)