このファイルを分割してみる。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
from flask import Flask, render_template,url_for,flash, redirect, request
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Length
from flask_migrate import Migrate
app = Flask(__name__)
project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format(os.path.join(project_dir, "msg.db"))
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] ='you-will-never-know'
db = SQLAlchemy(app)
db.init_app(app)
migrate = Migrate(app, db)
#DB Model
class Tw_msg (db.Model):
__tablename__ = 'tw_msg'
id = db.Column(db.Integer, unique=True, primary_key=True)
time = db.Column(db.String(6))
message = db.Column(db.String(140))
hashtag =db.Column(db.String(140))
url = db.Column(db.String(140))
length = db.Column(db.String(10))
conditions = db.Column(db.String(5))
#WTF Model(入力フィールド定義)
class Message_Add(FlaskForm):
id = StringField('ID',validators=[DataRequired(), Length(min=1, max=3) ])
time = StringField('Time',
validators=[DataRequired(), Length(min=2, max=60) ])
message = StringField('Message')
hashtag = StringField('Hashtag')
url = StringField('URL')
length = StringField('Length')
conditions = StringField('Conditions')
submit = SubmitField('Submit')
#ルーティングと処理
@app.route('/',methods=['GET', 'POST'])
def message():
db.create_all()
form = Message_Add()
#新規データ受付時処理
try:
if form.validate_on_submit():
Char_len = len(form.message.data+form.hashtag.data)
message = Tw_msg( time = form.time.data, message = form.message.data ,
hashtag = form.hashtag.data, url = form.url.data,
length=Char_len, conditions=form.conditions.data)
db.session.add(message)
db.session.commit()
flash('New message has been created!', 'success')
return redirect(url_for('message'))
except:
flash('Failed at somewhere!', 'Unsuccess')
#登録データ一覧表示
messages = Tw_msg.query.all()
return render_template("message.html",form=form, messages=messages)
@app.route("/<int:message_id>/edit", methods=['GET', "POST"])
def edit_message(message_id):
msg = Tw_msg.query.get(message_id)
form = Message_Add()
if form.validate_on_submit():
Char_len = len(form.message.data+form.hashtag.data)
print(Char_len)
msg.id = form.id.data
msg.time = form.time.data
msg.message = form.message.data
msg.hashtag = form.hashtag.data
msg.url = form.url.data
msg.length = Char_len
msg.conditions = form.conditions.data
db.session.commit()
flash('Your post has been updated!', 'success')
return redirect(url_for('message'))
elif request.method == 'GET':
form.id.data = msg.id
form.time.data = msg.time
form.message.data = msg.message
form.hashtag.data = msg.hashtag
form.url.data = msg.url
form.url.data = msg.length
form.conditions.data = msg.conditions
return render_template("edit_message.html",form=form)
@app.route("/<int:message_id>/delete", methods=['GET', "POST"])
def delete_message(message_id):
msg = Tw_msg.query.get_or_404(message_id)
db.session.delete(msg)
db.session.commit()
flash('Your post has been deleted!', 'success')
return redirect(url_for('message'))
if __name__ == "__main__":
app.run()
1. Step1
admin.pyを作って、その配下にadminフォルダにを作って、__init__.pyを作って、上のアプリを__init__.pyに全部コピー。
admin.py
from admin import app
/admin/__init_.py
__init__.py
上のアプリを全コピー。
すると、DBファイルがこのディレクトリに作られる。
Step2. DB関連をmodel.pyに分割する。
model.py
from admin import db
#DB Model
class Tw_msg (db.Model):
__tablename__ = 'tw_msg'
id = db.Column(db.Integer, unique=True, primary_key=True)
time = db.Column(db.String(6))
message = db.Column(db.String(140))
hashtag =db.Column(db.String(140))
url = db.Column(db.String(140))
length = db.Column(db.String(10))
conditions = db.Column(db.String(5))
これでも動く。
DBの変更は、FLASK-Migrateで。
0 件のコメント:
コメントを投稿