Last active
April 30, 2017 18:08
-
-
Save Speedy1991/d69cb17d77a363f4c526395cbee3f78c to your computer and use it in GitHub Desktop.
example flask-blogging with flask-login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask_blogging import SQLAStorage, BloggingEngine | |
from flask_sqlalchemy import SQLAlchemy | |
from flask_login import LoginManager, UserMixin, login_user, logout_user | |
from flask import Flask | |
from sqlalchemy.exc import IntegrityError | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' | |
app.config['SECRET_KEY'] = "12345" | |
db = SQLAlchemy(app) | |
class User(UserMixin, db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(15), unique=True) | |
email = db.Column(db.String(50), unique=True) | |
password = db.Column(db.String(80)) | |
login_manager = LoginManager(app) | |
login_manager.login_view = 'login' | |
storage = SQLAStorage(db=db) | |
blog_engine = BloggingEngine(app=app, storage=storage) | |
db.create_all() | |
@login_manager.user_loader | |
def load_user(user_id): | |
return db.session.query(User).get(user_id) | |
@blog_engine.user_loader | |
def load_user(user_id): | |
return db.session.query(User).get(user_id) | |
try: | |
user = User(username="paul", email="[email protected]", password="12345") | |
db.session.add(user) | |
db.session.commit() | |
except IntegrityError: | |
pass | |
@app.route("/login") | |
def login(): | |
a_user = db.session.query(User).get("1") | |
login_user(a_user) | |
return "OK", 200 | |
@app.route("/logout") | |
def logout(): | |
logout_user() | |
return "OK", 200 | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment