mysql -uroot -pmyweb myweb -e "drop table AdminUser;" mysql -uroot -pmyweb myweb -e "create table user (id int primary key auto_increment,first_name varchar(100),last_name varchar(100),login varchar(80),email varchar(120),password varchar(64));" mysql -uroot -pmyweb myweb -e "alter table user add unique index idx_login (login);"
from flask import Flask from flask_admin import Admin from flask_admin import BaseView from flask_admin import expose from flask_babelex import Babel from flask_sqlalchemy import SQLAlchemy from flask_admin.contrib.sqla import ModelView # 07_新增登陆界面:加载模块 from flask import url_for, redirect, render_template, request from wtforms import form, fields, validators import flask_admin as admin import flask_login as login from flask_admin.contrib import sqla from flask_admin import helpers from werkzeug.security import generate_password_hash, check_password_hash
db = SQLAlchemy() app = Flask(__name__) babel = Babel(app)
#set optional bootswatch theme #app.config['FLASK_ADMIN_SWATCH'] = 'cerulean' app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:myweb@localhost:3306/myweb?charset=utf8' app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # 07_新增登陆界面:配置key才能使用session # Create dummy secrey key so we can use sessions app.config['SECRET_KEY'] = '123456790'
# Flask-Login integration # NOTE: is_authenticated, is_active, and is_anonymous # are methods in Flask-Login < 0.3.0 @property defis_authenticated(self): returnTrue
@property defis_active(self): returnTrue
@property defis_anonymous(self): returnFalse
defget_id(self): return self.id
# Required for administrative interface def__unicode__(self): return self.username
# Define login and registration forms (for flask-login) classLoginForm(form.Form): login = fields.StringField(validators=[validators.required()]) password = fields.PasswordField(validators=[validators.required()]) #password = fields.StringField(validators=[validators.required()])
defvalidate_login(self, field): user = self.get_user()
if user isNone: raise validators.ValidationError('Invalid user')
# we're comparing the plaintext pw with the the hash from the db #if not check_password_hash(user.password, self.password.data): # to compare plain text passwords use if user.password != self.password.data: raise validators.ValidationError('Invalid password')
@expose('/login/', methods=('GET', 'POST')) deflogin_view(self): # handle user login form = LoginForm(request.form) if helpers.validate_form_on_submit(form): user = form.get_user() login.login_user(user)
if login.current_user.is_authenticated: return redirect(url_for('.index')) link = '<p>Don\'t have an account? <a href="' + url_for('.register_view') + '">Click here to register.</a></p>' self._template_args['form'] = form self._template_args['link'] = link returnsuper(MyAdminIndexView, self).index()
@expose('/register/', methods=('GET', 'POST')) defregister_view(self): form = RegistrationForm(request.form) if helpers.validate_form_on_submit(form): user = User()
form.populate_obj(user) # we hash the users password to avoid saving it as plaintext in the db, # remove to use plain text: #user.password = generate_password_hash(form.password.data)
db.session.add(user) db.session.commit()
login.login_user(user) return redirect(url_for('.index')) link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>' self._template_args['form'] = form self._template_args['link'] = link returnsuper(MyAdminIndexView, self).index()
if __name__ == "__main__": app.run(debug=True,host='0.0.0.0',port=5000)
3.7 运行程序
[myweb@am_01 PonyTail]$ python manage.py * Serving Flask app "manage" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 476-676-292