You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

106 lines
3.6 KiB

from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from models import db, User, Recipe, Ingredient, Instruction
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///recipes.db'
app.config['SECRET_KEY'] = 'your_secret_key'
db.init_app(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def index():
recipes = Recipe.query.all()
return render_template('index.html', recipes=recipes)
@app.route('/recipe/<int:recipe_id>')
def recipe(recipe_id):
recipe = Recipe.query.get_or_404(recipe_id)
return render_template('recipe.html', recipe=recipe)
@app.route('/new_recipe', methods=['GET', 'POST'])
@login_required
def new_recipe():
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
ingredients = request.form.getlist('ingredient')
quantities = request.form.getlist('quantity')
steps = request.form.getlist('step')
step_descriptions = request.form.getlist('step_description')
recipe = Recipe(title=title, description=description, author=current_user)
for ingredient, quantity in zip(ingredients, quantities):
recipe.ingredients.append(Ingredient(name=ingredient, quantity=quantity))
for step, description in zip(steps, step_descriptions):
recipe.instructions.append(Instruction(step=step, description=description))
db.session.add(recipe)
db.session.commit()
flash('Recipe created successfully!', 'success')
return redirect(url_for('index'))
return render_template('new_recipe.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email).first()
if user and check_password_hash(user.password, password):
login_user(user)
flash('Logged in successfully.')
return redirect(url_for('index'))
else:
flash('Invalid email or password.')
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('Logged out successfully.')
return redirect(url_for('index'))
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
confirm_password = request.form['confirm_password']
if password != confirm_password:
flash('Passwords do not match.')
return redirect(url_for('register'))
hashed_password = generate_password_hash(password, method='pbkdf2:sha256')
new_user = User(username=username, email=email, password=hashed_password)
try:
db.session.add(new_user)
db.session.commit()
flash('Registration successful.')
return redirect(url_for('login'))
except:
flash('Email address already exists.')
return redirect(url_for('register'))
return render_template('register.html')
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)