Python + Flask を使ったWebアプリ作成⑨(Blueprint導入編)

下記記事の続きで、Python Flask を使ったブログサービスの実装を進めていきます。








今回はFlaskの拡張機能Blueprintを使って、機能ごとに処理を分割してみます。


*環境

  • MacOS
  • Python 3.6.3
  • Flask 1.0.2


*参考



*Blueprints とは

Webアプリケーションが大規模になってくるとリクエストを受け取るroutes.pyが肥大化してきますが、Blueprintsを使うことで機能ごとに分割することができます。


*ディレクトリを作成

分割する機能ごとにディレクトリを作成します。
main, posts, usersのディレクトリを作成し、それぞれにforms.py, routes.pyを追加して下記構成にします。
├── flaskblog
│   ├── __init__.py
│   ├── main             <-- 追加
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── models.py
│   ├── posts             <-- 追加
│   │   ├── __init__.py
│   │   ├── forms.py
│   │   └── routes.py
│   ├── static
│   ├── templates
│   └── users             <-- 追加
│       ├── __init__.py
│       ├── forms.py
│       └── routes.py
├── run.py
└── site.db


*routes.pyを分割

flaskblog/routes.pyの下記部分を
flaskblog/main/routes.pyに移植します。
<main/routes.py>
@app.route('/')
@app.route('/home')
def home():
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    return render_template('home.html', posts=posts)


@app.route('/about')
def about():
    return render_template('about.html', title='About')

下記部分をflaskblog/users/routes.pyに移植します。
<users/routes.py>
@app.route('/register', methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)


@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('home'))
        else:
            flash('Login Unsuccessful. Please check username and password', 'danger')
    return render_template('login.html', title='Login', form=form)


@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('home'))


def save_picture(form_picture):
    random_hex = secrets.token_hex(8)
    _, f_ext = os.path.splitext(form_picture.filename)
    picture_fn = random_hex + f_ext
    picture_path = os.path.join(app.root_path, 'static/profile_pics', picture_fn)
    form_picture.save(picture_path)

    output_size = (125, 125)
    i = Image.open(form_picture)
    i.thumbnail(output_size)
    i.save(picture_path)
    return picture_fn


@app.route('/account', methods=['GET', 'POST'])
@login_required
def account():
    form = UpdateAccountForm()
    if form.validate_on_submit():
        if form.picture.data:
            picture_file = save_picture(form.picture.data)
            current_user.image_file = picture_file
        current_user.username = form.username.data
        current_user.email = form.email.data
        db.session.commit()
        flash('your account has been updated!', 'success')
        return redirect((url_for('account')))

    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email

    image_file = url_for('static', filename='profile_pics/' + current_user.image_file)
    return render_template('account.html', title='Account', image_file=image_file, form=form)

@app.route('/user/<string:username>')
def user_posts(username):
    page = request.args.get('page', 1, type=int)
    user = User.query.filter_by(username=username).first_or_404()
    posts = Post.query.filter_by(author=user)\
        .order_by(Post.date_posted.desc())\
        .paginate(page=page, per_page=5)
    return render_template('user_posts.html', posts=posts, user=user)

def send_reset_email(user):
    token = user.get_reset_token()
    msg = Message('Password Reset Request',
                  sender='noreply@demo.com',
                  recipients=[user.email])
    msg.body = '''To reset your password, visit the following link:{url}
If you did not make this request then simply ignore this email and no change will be made.
'''.format(url=url_for('reset_token', token=token, _external=True))
    mail.send(msg)

@app.route('/reset_password', methods=['GET', 'POST'])
def reset_request():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RequestResetForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        send_reset_email(user)
        flash('An email has been sent with instructions to reset your password', 'info')
        return redirect(url_for('login'))
    return render_template('reset_request.html', title='Reset Password', form=form)


@app.route('/reset_password/<token>', methods=['GET', 'POST'])
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user.password = hashed_password
        db.session.commit()
        flash('Your password has been updated! You are now able to log in', 'success')
        return redirect(url_for('login'))
    return render_template('reset_token.html', title='Reset Password', form=form)

users/utils.pyを新規作成し、users/routes.pyの下記メソッドをutils.pyに切り出し共通化します。
<users/utils.py>
def save_picture(form_picture):  
    random_hex = secrets.token_hex(8)  
    _, f_ext = os.path.splitext(form_picture.filename)  
    picture_fn = random_hex + f_ext  
    picture_path = os.path.join(current_app.root_path, 'static/profile_pics', picture_fn)  
    form_picture.save(picture_path)  
  
    output_size = (125, 125)  
    i = Image.open(form_picture)  
    i.thumbnail(output_size)  
    i.save(picture_path)  
    return picture_fn  
  
  
def send_reset_email(user):  
    token = user.get_reset_token()  
    msg = Message('Password Reset Request',  
                  sender='noreply@demo.com',  
                  recipients=[user.email])  
    msg.body = '''To reset your password, visit the following link:{url}  
If you did not make this request then simply ignore this email and no change will be made.  
'''.format(url=url_for('reset_token', token=token, _external=True))  
    mail.send(msg)

下記部分をflaskblog/posts/routes.pyに移植します。
<posts/routes.py>
@app.route('/post/new', methods=['GET', 'POST'])
@login_required
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data, content=form.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created!', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html', title='New Post',
                           form=form, legend='New Post')


@app.route('/post/<int:post_id>')
def post(post_id):
    post = Post.query.get_or_404(post_id)
    return render_template('post.html', title=post.title, post=post)


@app.route('/post/<int:post_id>/update', methods=['GET', 'POST'])
@login_required
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been update!', 'success')
        return redirect(url_for('post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('create_post.html', title='Update Post',
                           form=form, legend='Update Post')


@app.route('/post/<int:post_id>/delete', methods=['GET', 'POST'])
@login_required
def delete_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    db.session.delete(post)
    db.session.commit()
    flash('Your post has been deleted!', 'success')
    return redirect(url_for('home'))


*Blueprintを追加

分割した各ファイルにBlueprintを追加します。
main/routes.pyの最初の行に下記を追加し、デコレーターの@app.route()@main.routeに修正します。
<main/routes.py>
from flask import render_template, request, Blueprint
from flaskblog.models import Post

main = Blueprint('main', __name__)


@main.route('/')
@main.route('/home')
def home():
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    return render_template('home.html', posts=posts)


@main.route('/about')
def about():
    return render_template('about.html', title='About')

同様にusers/routes.pyにもBlueprintを追加します。
<users/routes.py>
from flask import render_template, url_for, flash, redirect, request, Blueprint
from flask_login import login_user, current_user, logout_user, login_required
from flaskblog import db, bcrypt
from flaskblog.models import User, Post
from flaskblog.users.forms import (RegistrationForm, LoginForm, UpdateAccountForm,
                                   RequestResetForm, ResetPasswordForm)
from flaskblog.users.utils import save_picture, send_reset_email

users = Blueprint('users', __name__)


@users.route('/register', methods=['GET', 'POST'])
def register():
...


同様にposts/routes.pyにもBlueprintを追加します。
<posts/routes.py>
from flask import (render_template, url_for, flash,
                   redirect, request, abort, Blueprint)
from flask_login import current_user, login_required
from flaskblog import db
from flaskblog.models import Post
from flaskblog.posts.forms import PostForm

posts = Blueprint('posts', __name__)


@posts.route('/post/new', methods=['GET', 'POST'])
@login_required
def new_post():
...


ディレクトリ構成が変わったので、pythonファイル/htmlファイルで`url_for()`でメソッドを呼び出している部分を修正します。 そのメソッドがあるディレクトリ名を追加します。
  • url_for('home') --> url_for('main.home')
  • url_for('login') --> url_for('users.login')
  • url_for('reset_request') --> url_for('users.reset_request')
  • url_for('account') --> url_for('users.account')
  • url_for('reset_token') --> url_for('users.reset_token')
  • url_for('logout') --> url_for('users.logout')
  • url_for('user_posts' --> url_for('users.user_posts'
  • url_for('new_post') --> url_for('posts.new_post')
  • url_for(post) --> url_for(posts.post)


*forms.pyを分割

flaskblog/forms.pyの下記部分をflaskblog/posts/forms.pyに移植します。
<flaskblog/posts/forms.py>
from flask_wtf import FlaskForm  
from wtforms import StringField, SubmitField, TextAreaField  
from wtforms.validators import DataRequired  
  
  
class PostForm(FlaskForm):  
    title = StringField('Title', validators=[DataRequired()])  
    content = TextAreaField('Content', validators=[DataRequired()])  
    submit = SubmitField('Post')

flaskblog/forms.pyの残りの部分をflaskblog/users/forms.pyに移植します。
from flask_wtf import FlaskForm  
from flask_wtf.file import FileField, FileAllowed  
from wtforms import StringField, PasswordField, SubmitField, BooleanField  
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError  
from flask_login import current_user  
from flaskblog.models import User


class RegistrationForm(FlaskForm):  
    username = StringField('Username',  
                           validators=[DataRequired(), Length(min=2, max=20)])  
    email = StringField('Email',  
                        validators=[DataRequired(), Email()])  
    password = PasswordField('Password', validators=[DataRequired()])  
    confirm_password = PasswordField('Confirm Password',  
                                     validators=[DataRequired(), EqualTo('password')])  
    submit = SubmitField('Sign Up')  
  
    def validate_username(self, username):  
        user = User.query.filter_by(username=username.data).first()  
        if user:  
            raise ValidationError('That username is taken. Please choose a different one.')  
  
    def validate_email(self, email):  
        email = User.query.filter_by(email=email.data).first()  
        if email:  
            raise ValidationError('That email is taken. Please choose a different one.')  
  
  
class LoginForm(FlaskForm):  
    email = StringField('Email',  
                        validators=[DataRequired(), Email()])  
    password = PasswordField('Password', validators=[DataRequired()])  
    remember = BooleanField('Remember me')  
    submit = SubmitField('Login')  
  
  
class UpdateAccountForm(FlaskForm):  
    username = StringField('Username',  
                           validators=[DataRequired(), Length(min=2, max=20)])  
    email = StringField('Email',  
                        validators=[DataRequired(), Email()])  
    picture = FileField('Update Profile Picture', validators=[FileAllowed(['jpg', 'png'])])  
    submit = SubmitField('Update')  
  
    def validate_username(self, username):  
        if username.data != current_user.username:  
            user = User.query.filter_by(username=username.data).first()  
            if user:  
                raise ValidationError('That username is taken. Please choose a different one.')  
  
    def validate_email(self, email):  
        if email.data != current_user.email:  
            email = User.query.filter_by(email=email.data).first()  
            if email:  
                raise ValidationError('That email is taken. Please choose a different one.')  
  
  
class RequestResetForm(FlaskForm):  
    email = StringField('Email', validators=[DataRequired(), Email()])  
    submit = SubmitField('Request Password Reset')  
  
    def validate_email(self, email):  
        email = User.query.filter_by(email=email.data).first()  
        if email is None:  
            raise ValidationError('There is no account with that email. You must register first.')  
  
  
class ResetPasswordForm(FlaskForm):  
    password = PasswordField('Password', validators=[DataRequired()])  
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])  
    submit = SubmitField('Reset Password')


*Blueprintをアプリケーションに登録

分割した機能をregister_blueprint()でアプリケーションに登録します。
今回はflaskblog/__init__.pycreate_app()というメソッドを作成して、その中でFlaskのインスタンスを生成しています。
<flaskblog/__init__.py>
import os  
from flask import Flask  
from flask_sqlalchemy import SQLAlchemy  
from flask_bcrypt import Bcrypt  
from flask_login import LoginManager  
from flask_mail import Mail  
from flaskblog.config import Config  
  
  
db = SQLAlchemy()  
bcrypt = Bcrypt()  
login_manager = LoginManager()  
login_manager.login_view = 'users.login'  
login_manager.login_message_category = 'info'  
mail = Mail()  
  
  
def create_app(config_class=Config):  
    app = Flask(__name__)  
    app.config.from_object(Config)  
  
    db.init_app(app)  
    bcrypt.init_app(app)  
    login_manager.init_app(app)  
    mail.init_app(app)  
  
    from flaskblog.users.routes import users  
    from flaskblog.posts.routes import posts  
    from flaskblog.main.routes import main  
  
    # Blueprintに登録
    app.register_blueprint(users)  
    app.register_blueprint(posts)  
    app.register_blueprint(main)  
  
    return app

flaskblog/__init__.pyに元々書かれていたシークレットキーとDBの設定を環境変数に登録します。
ターミナルから.bash_prifileを編集します。
$ vim ~/.bash_prifile

下記を追加します。
# Flask-blog-app
export SECRET_KEY='cfb33786023cc152019e747a051f73c6'
export SQLALCHEMY_DATABASE_URI='sqlite:///site.db'

環境変数の読み込み、メール設定などを別ファイルに切り出します。
config.pyを新規作成し、下記を書き込みます。
<flaskblog/config.py>
import os  
  
  
class Config:  
    SECRET_KEY = os.environ.get('SECRET_KEY')  
    SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI')  
    MAIL_SERVER = 'smtp.gmail.com'  
  MAIL_PORT = 587  
  MAIL_USE_TLS = True  
  MAIL_USERNAME = os.environ.get('EMAIL_USER')  
    MAIL_PASSWORD = os.environ.get('EMAIL_PASS')


run.pyappをインポートしていましたが、create_appに修正します。
from flaskblog import create_app  
  
app = create_app()  
  
if __name__ == '__main__':  
    app.run(debug=True)


*models.pyの修正

app = Flask(__name__)Blueprintに変更したので、変更前のFlaskのインスタンスappにアクセスできなくなってしまいます。
このことを回避するために、Flaskのcurrent_appを使います。
<models.py>
from datetime import datetime  
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer  
from flask import current_app  
from flaskblog import db, login_manager  
from flask_login import UserMixin  
  
  
@login_manager.user_loader  
def load_user(user_id):  
    return User.query.get(int(user_id))  
  
  
class User(db.Model, UserMixin):  
    id = db.Column(db.Integer, primary_key=True)  
    username = db.Column(db.String(20), unique=True, nullable=False)  
    email = db.Column(db.String(120), unique=True, nullable=False)  
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')  
    password = db.Column(db.String(60), nullable=False)  
    posts = db.relationship('Post', backref='author', lazy=True)  
  
    def get_reset_token(self, expires_sec=1800):  
        s = Serializer(current_app.config['SECRET_KEY'], expires_sec)  
        return s.dumps({'user_id': self.id}).decode('utf-8')  
  
    @staticmethod  
  def verify_reset_token(token):  
        s = Serializer(current_app.config['SECRET_KEY'])  
        try:  
            user_id = s.loads(token)['user_id']  
        except:  
            return None  
 return User.query.get(user_id)  
  
    def __repr__(self):  
        return "User('{}', '{}', '{}')".format(self.username, self.email, self.image_file)  
  
  
class Post(db.Model):  
    id = db.Column(db.Integer, primary_key=True)  
    title = db.Column(db.String(100), nullable=False)  
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)  
    content = db.Column(db.Text, nullable=False)  
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)  
  
    def __repr__(self):  
        return "Post('{}', '{}')".format(self.title, self.date_posted)



*動作確認

Webアプリケーションを起動して、処理が今まで通り正常にできるか確認します。
$ python run.py













*所感

Blueprintを使って分割することで、コードで処理を追いやすくなりました。Flaskを使う場合はBlueprintを使うのが一般的なようなので、実際に業務で使う際は規模が大きくなる前に早めに導入したほうがコードの可読性が上がり後々の保守にも役立つかと思います。


Previous
Next Post »

人気の投稿