Skip to content

Commit 94a3172

Browse files
committed
Merge pull request Show-Me-the-Code#106 from neo1218/neo1218
0023 yes
2 parents a53cc1f + 922ff6d commit 94a3172

31 files changed

Lines changed: 753 additions & 0 deletions

neo1218/0023/web/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
web 留言簿
2+
======
3+
4+
python flask框架编写的简单的留言簿<br/>
5+
功能: 用户登录留言。
6+

neo1218/0023/web/app/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: UTF-8 -*-
2+
# !/usr/bin/python
3+
4+
# app文件
5+
6+
from flask import Flask
7+
from flask.ext.bootstrap import Bootstrap
8+
from flask.ext.mail import Mail
9+
from flask.ext.moment import Moment
10+
from flask.ext.sqlalchemy import SQLAlchemy
11+
from flask.ext.login import LoginManager
12+
from config import config
13+
14+
bootstrap = Bootstrap()
15+
mail = Mail()
16+
moment = Moment()
17+
db = SQLAlchemy()
18+
19+
login_manager = LoginManager()
20+
login_manager.session_protection = 'strong'
21+
login_manager.login_view = 'auth.login'
22+
23+
24+
def create_app(config_name):
25+
# app 工厂函数(注册app蓝图)
26+
app = Flask(__name__)
27+
app.config.from_object(config[config_name])
28+
config[config_name].init_app(app)
29+
30+
bootstrap.init_app(app)
31+
mail.init_app(app)
32+
moment.init_app(app)
33+
db.init_app(app)
34+
login_manager.init_app(app)
35+
36+
from .main import main as main_blueprint
37+
app.register_blueprint(main_blueprint)
38+
39+
from .auth import auth as auth_blueprint
40+
app.register_blueprint(auth_blueprint, url_prefix='/auth')
41+
42+
return app
43+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from flask import Blueprint
2+
3+
auth = Blueprint('auth', __name__)
4+
5+
from . import views

neo1218/0023/web/app/auth/forms.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: UTF-8 -*-
2+
# !/usr/bin/python
3+
from flask.ext.wtf import Form
4+
from wtforms import StringField, PasswordField, BooleanField, SubmitField
5+
from wtforms.validators import Required, Length, Email, Regexp, EqualTo
6+
from wtforms import ValidationError
7+
from ..models import User
8+
9+
10+
class LoginForm(Form):
11+
email = StringField('Email', validators=[Required(), Length(1, 64),
12+
Email()])
13+
password = PasswordField('Password', validators=[Required()])
14+
remember_me = BooleanField('记住我')
15+
submit = SubmitField('登陆')
16+
17+
18+
class RegistrationForm(Form):
19+
email = StringField('Email', validators=[Required(), Length(1, 64),
20+
Email()])
21+
username = StringField('Username', validators=[
22+
Required(), Length(1, 64)])
23+
24+
password = PasswordField('Password', validators=[
25+
Required(), EqualTo('password2', message='Passwords must match.')])
26+
27+
password2 = PasswordField('Confirm password', validators=[Required()])
28+
29+
submit = SubmitField('注册')
30+
31+
def validate_email(self, field):
32+
if User.query.filter_by(email=field.data).first():
33+
raise ValidationError('邮箱已经注册!')
34+
35+
def validate_username(self, field):
36+
if User.query.filter_by(username=field.data).first():
37+
raise ValidationError('用户名已经存在!')

neo1218/0023/web/app/auth/views.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: UTF-8 -*-
2+
# !/usr/bin/python
3+
from flask import render_template, redirect, request, url_for, flash
4+
from flask.ext.login import login_user, logout_user, login_required, \
5+
current_user
6+
from . import auth
7+
from .. import db
8+
from ..models import User
9+
from .forms import LoginForm, RegistrationForm
10+
11+
12+
@auth.route('/login', methods=['GET', 'POST'])
13+
def login():
14+
form = LoginForm()
15+
if form.validate_on_submit():
16+
user = User.query.filter_by(email=form.email.data).first()
17+
if user is not None and user.verify_password(form.password.data):
18+
login_user(user, form.remember_me.data)
19+
return redirect(request.args.get('next') or url_for('main.index'))
20+
flash('用户名或密码不正确!')
21+
return render_template('auth/login.html', form=form)
22+
23+
24+
@auth.route('/logout')
25+
@login_required
26+
def logout():
27+
logout_user()
28+
flash('你已经退出!.')
29+
return redirect(url_for('main.index'))
30+
31+
32+
@auth.route('/register', methods=['GET', 'POST'])
33+
def register():
34+
form = RegistrationForm()
35+
if form.validate_on_submit():
36+
user = User(email=form.email.data,
37+
username=form.username.data,
38+
password=form.password.data)
39+
db.session.add(user)
40+
flash('你现在可以登录了!')
41+
return redirect(url_for('auth.login'))
42+
return render_template('auth/register.html', form=form)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from flask import Blueprint
2+
3+
main = Blueprint('main', __name__)
4+
5+
from . import views, errors
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from flask import render_template
2+
from . import main
3+
4+
5+
@main.app_errorhandler(404)
6+
def page_not_found(e):
7+
return render_template('404.html'), 404
8+
9+
10+
@main.app_errorhandler(500)
11+
def internal_server_error(e):
12+
return render_template('500.html'), 500

neo1218/0023/web/app/main/forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding: UTF-8 -*-
2+
# !/usr/bin/python
3+
from flask.ext.wtf import Form
4+
from wtforms import StringField, TextAreaField, BooleanField, SelectField,SubmitField
5+
from wtforms.validators import Required
6+
7+
class PostForm(Form):
8+
body = TextAreaField("说点什么吧!",validators=[Required()])
9+
submit = SubmitField('提交')
10+
11+

neo1218/0023/web/app/main/views.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: UTF-8 -*-
2+
#!/usr/bin/python
3+
from flask import render_template,url_for,redirect
4+
from flask.ext.login import current_user
5+
from .forms import PostForm
6+
from ..models import Post
7+
from .. import db
8+
from . import main
9+
10+
11+
@main.route('/',methods=['GET','POST'])
12+
def index():
13+
form = PostForm()
14+
if form.validate_on_submit():
15+
post = Post(body=form.body.data,
16+
author=current_user._get_current_object())
17+
db.session.add(post)
18+
return redirect(url_for('.index'))
19+
posts = Post.query.order_by(Post.timestamp.desc()).all()
20+
return render_template('index.html',form=form,posts=posts)
21+
22+

neo1218/0023/web/app/models.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: UTF-8 -*-
2+
# !/usr/bin/python
3+
from werkzeug.security import generate_password_hash, check_password_hash
4+
from flask.ext.login import UserMixin
5+
from datetime import datetime
6+
from . import db, login_manager
7+
8+
class User(UserMixin, db.Model):
9+
# 用户数据库表
10+
__tablename__ = 'users'
11+
id = db.Column(db.Integer, primary_key=True)
12+
email = db.Column(db.String(64), unique=True, index=True)
13+
username = db.Column(db.String(64), unique=True, index=True)
14+
password_hash = db.Column(db.String(128))
15+
posts = db.relationship('Post',backref="author",lazy='dynamic')
16+
17+
@property
18+
def password(self):
19+
raise AttributeError('password is not a readable attribute')
20+
21+
@password.setter
22+
def password(self, password):
23+
self.password_hash = generate_password_hash(password)
24+
25+
def verify_password(self, password):
26+
return check_password_hash(self.password_hash, password)
27+
28+
def __repr__(self):
29+
return '<User %r>' % self.username
30+
31+
@login_manager.user_loader
32+
def load_user(user_id):
33+
return User.query.get(int(user_id))
34+
35+
class Post(db.Model):
36+
__tablename__= 'posts'
37+
id = db.Column(db.Integer,primary_key=True)
38+
body = db.Column(db.Text)
39+
timestamp = db.Column(db.DateTime,index=True,default=datetime.utcnow)
40+
author_id = db.Column(db.Integer,db.ForeignKey('users.id'))

0 commit comments

Comments
 (0)