mirror of
https://github.com/shouptech/flask-tutorial.git
synced 2026-02-03 15:39:44 +00:00
Flake8 fixes
This commit is contained in:
parent
f4abfe88ab
commit
5b10dcacfa
4 changed files with 18 additions and 9 deletions
|
|
@ -26,10 +26,6 @@ def create_app(test_config=None):
|
||||||
os.makedirs(app.instance_path)
|
os.makedirs(app.instance_path)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
# a simple page that says hello
|
|
||||||
@app.route('/hello')
|
|
||||||
def hello():
|
|
||||||
return 'Hello, World!'
|
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ from flaskr.db import db, User
|
||||||
|
|
||||||
bp = Blueprint('auth', __name__, url_prefix='/auth')
|
bp = Blueprint('auth', __name__, url_prefix='/auth')
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/register', methods=('GET', 'POST'))
|
@bp.route('/register', methods=('GET', 'POST'))
|
||||||
def register():
|
def register():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
|
@ -32,6 +33,7 @@ def register():
|
||||||
|
|
||||||
return render_template('auth/register.html')
|
return render_template('auth/register.html')
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/login', methods=('GET', 'POST'))
|
@bp.route('/login', methods=('GET', 'POST'))
|
||||||
def login():
|
def login():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
|
@ -55,6 +57,7 @@ def login():
|
||||||
|
|
||||||
return render_template('auth/login.html')
|
return render_template('auth/login.html')
|
||||||
|
|
||||||
|
|
||||||
@bp.before_app_request
|
@bp.before_app_request
|
||||||
def load_logged_in_user():
|
def load_logged_in_user():
|
||||||
user_id = session.get('user_id')
|
user_id = session.get('user_id')
|
||||||
|
|
@ -64,11 +67,13 @@ def load_logged_in_user():
|
||||||
else:
|
else:
|
||||||
g.user = User.query.filter_by(id=user_id).first()
|
g.user = User.query.filter_by(id=user_id).first()
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/logout')
|
@bp.route('/logout')
|
||||||
def logout():
|
def logout():
|
||||||
session.clear()
|
session.clear()
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
|
|
||||||
def login_required(view):
|
def login_required(view):
|
||||||
@functools.wraps(view)
|
@functools.wraps(view)
|
||||||
def wrapped_view(**kwargs):
|
def wrapped_view(**kwargs):
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,13 @@ from flaskr.db import db, Post, User
|
||||||
|
|
||||||
bp = Blueprint('blog', __name__)
|
bp = Blueprint('blog', __name__)
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/')
|
@bp.route('/')
|
||||||
def index():
|
def index():
|
||||||
posts = Post.query.join(User).order_by(Post.created.desc()).all()
|
posts = Post.query.join(User).order_by(Post.created.desc()).all()
|
||||||
return render_template('blog/index.html', posts=posts)
|
return render_template('blog/index.html', posts=posts)
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/create', methods=('GET', 'POST'))
|
@bp.route('/create', methods=('GET', 'POST'))
|
||||||
@login_required
|
@login_required
|
||||||
def create():
|
def create():
|
||||||
|
|
@ -35,6 +37,7 @@ def create():
|
||||||
|
|
||||||
return render_template('blog/create.html')
|
return render_template('blog/create.html')
|
||||||
|
|
||||||
|
|
||||||
def get_post(id, check_author=True):
|
def get_post(id, check_author=True):
|
||||||
post = Post.query.filter_by(id=id).first()
|
post = Post.query.filter_by(id=id).first()
|
||||||
|
|
||||||
|
|
@ -46,6 +49,7 @@ def get_post(id, check_author=True):
|
||||||
|
|
||||||
return post
|
return post
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
|
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
|
||||||
@login_required
|
@login_required
|
||||||
def update(id):
|
def update(id):
|
||||||
|
|
@ -70,6 +74,7 @@ def update(id):
|
||||||
|
|
||||||
return render_template('blog/update.html', post=post)
|
return render_template('blog/update.html', post=post)
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<int:id>/delete', methods=('POST',))
|
@bp.route('/<int:id>/delete', methods=('POST',))
|
||||||
@login_required
|
@login_required
|
||||||
def delete(id):
|
def delete(id):
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from flask import current_app, g
|
|
||||||
from flask.cli import with_appcontext
|
from flask.cli import with_appcontext
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|
||||||
|
|
@ -11,6 +10,7 @@ db = SQLAlchemy()
|
||||||
def init_db():
|
def init_db():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
|
|
||||||
@click.command('init-db')
|
@click.command('init-db')
|
||||||
@with_appcontext
|
@with_appcontext
|
||||||
def init_db_command():
|
def init_db_command():
|
||||||
|
|
@ -18,10 +18,12 @@ def init_db_command():
|
||||||
init_db()
|
init_db()
|
||||||
click.echo('Initialized the database.')
|
click.echo('Initialized the database.')
|
||||||
|
|
||||||
|
|
||||||
def init_app(app):
|
def init_app(app):
|
||||||
db.init_app(app) # Initialize the sql alchemy database
|
db.init_app(app) # Initialize the sql alchemy database
|
||||||
app.cli.add_command(init_db_command)
|
app.cli.add_command(init_db_command)
|
||||||
|
|
||||||
|
|
||||||
# Model definitions
|
# Model definitions
|
||||||
class User(db.Model):
|
class User(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
|
@ -32,6 +34,7 @@ class User(db.Model):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<User %r>' % self.username
|
return '<User %r>' % self.username
|
||||||
|
|
||||||
|
|
||||||
class Post(db.Model):
|
class Post(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue