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