-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun.py
185 lines (141 loc) · 5.6 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from datetime import datetime
from flask import (Flask, abort, flash, redirect, render_template, session,
url_for)
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import check_password_hash, generate_password_hash
from form import *
app = Flask(__name__)
app.config['SECRET_KEY'] = 'xNVg}f_m:UmiOB{9bC`SvB9j5N<-3I./' # CSRFトークン
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{app.root_path + "/schoo.sqlite"}' # DBへのパス
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
def hash_password(original_pass):
return generate_password_hash(original_pass)
def verify_password(hash_pass, original_pass):
return check_password_hash(hash_pass, original_pass)
def get_model_dict(model):
return dict((column.name, getattr(model, column.name))
for column in model.__table__.columns)
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, nullable=False)
email = db.Column(db.String, nullable=False)
password = db.Column(db.Text, nullable=False)
created = db.Column(db.DateTime, nullable=False, default=datetime.now)
modified = db.Column(db.DateTime, nullable=False, default=datetime.now)
@staticmethod
def login(email, password):
'''
ログイン実行
'''
u = User.query.filter_by(email=email).first()
if u and verify_password(u.password, password):
return u
return None
class Post(db.Model):
__tablename__ = 'post'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.ForeignKey('user.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False, index=True)
title = db.Column(db.String,nullable=True)
publish_date = db.Column(db.DateTime,nullable=True, default=datetime.now)
content = db.Column(db.Text,nullable=False)
created = db.Column(db.DateTime, nullable=False, default=datetime.now)
modified = db.Column(db.DateTime, nullable=False, default=datetime.now)
user = db.relationship('User')
@app.route("/")
def index():
posts = [] # 非ログイン時
if 'auth.user' in session:
# ログインしているユーザーの記事だけを
# 一覧で表示するようにしています
posts = Post.query.filter(Post.user_id == session['auth.user']['id']).order_by(Post.publish_date.desc()).all()
return render_template('index.html',posts=posts)
@app.route("/Hello")
def hello():
return "Hello,World."
@app.route("/schoo")
def schoo():
message = 'プログラミングを楽しんで下さい'
return render_template('schoo.html',message=message)
@app.route("/signup",methods=['GET','POST'])
def signup():
form = SignupForm()
if form.validate_on_submit():
u = User.query.filter_by(email=form.email.data).first()
if u:
flash('そのメールアドレスは既に利用されています。')
return redirect(url_for('.signup'))
user = User()
form.populate_obj(user)
user.password = hash_password(form.password.data)
db.session.add(user)
db.session.commit()
flash('ユーザー登録が完了しました。ログインして下さい')
return redirect(url_for(".signup"))
return render_template('signup.html',form=form)
@app.route("/login",methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
u = User.login(form.email.data, form.password.data)
if u is None:
flash('ユーザー名とパスワードの組み合わせが違います。')
return redirect(url_for('.login'))
session['auth.user'] = get_model_dict(u)
return redirect(url_for('.index'))
return render_template(
'login.html', form=form)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('.login'))
@app.route("/add_post",methods=['GET','POST'])
def add_post():
form = PostForm()
if form.validate_on_submit():
post = Post()
form.populate_obj(post)
post.id = None
post.user_id = session['auth.user']['id']
db.session.add(post)
db.session.commit()
flash('記事を公開しました!')
return redirect(url_for(".index"))
return render_template('add_post.html', form=form)
@app.route('/post/<int:post_id>')
def post(post_id):
post = Post.query.filter(Post.id == post_id).first()
if not post:
abort(404)
return render_template('post.html',post=post)
@app.route('/show_post/<int:post_id>')
def show_post(post_id):
post = Post.query.filter(Post.id == post_id).first()
form = PostForm()
if not post:
abort(404)
form.title.data = post.title
form.content.data = post.content
form.id.data = post.id
return render_template('update_post.html',post=post,form=form)
@app.route('/update_post/',methods=['POST'])
def update_post():
form = PostForm()
if form.validate_on_submit():
post = Post.query.filter(Post.id == form.id.data).first()
if form.update.data:
# 更新
post.title = form.title.data
post.content = form.content.data
db.session.add(post)
flash('記事内容を更新しました')
else:
# 削除
db.session.delete(post)
flash('記事を削除しました')
db.session.commit()
return redirect(url_for('.index'))
return redirect(url_for('.show_post',post_id=form.id.data))
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0',port=5200)