microblog/app/views.py

119 lines
3.6 KiB
Python
Raw Normal View History

2012-12-16 08:30:41 +00:00
from flask import render_template, flash, redirect, session, url_for, request, g
from flask.ext.login import login_user, logout_user, current_user, login_required
from app import app, db, lm, oid
2012-12-16 08:31:21 +00:00
from forms import LoginForm, EditForm
2012-12-16 08:30:41 +00:00
from models import User, ROLE_USER, ROLE_ADMIN
2012-12-16 08:31:21 +00:00
from datetime import datetime
2012-12-16 08:24:07 +00:00
2012-12-16 08:30:41 +00:00
@lm.user_loader
def load_user(id):
return User.query.get(int(id))
@app.before_request
def before_request():
g.user = current_user
2012-12-16 08:31:21 +00:00
if g.user.is_authenticated():
g.user.last_seen = datetime.utcnow()
db.session.add(g.user)
db.session.commit()
2012-12-16 08:32:38 +00:00
@app.errorhandler(404)
def internal_error(error):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_error(error):
db.session.rollback()
return render_template('500.html'), 500
2012-12-16 08:24:07 +00:00
@app.route('/')
@app.route('/index')
2012-12-16 08:30:41 +00:00
@login_required
2012-12-16 08:24:07 +00:00
def index():
2012-12-16 08:30:41 +00:00
user = g.user
2012-12-16 08:26:19 +00:00
posts = [
{
'author': { 'nickname': 'John' },
'body': 'Beautiful day in Portland!'
},
{
'author': { 'nickname': 'Susan' },
'body': 'The Avengers movie was so cool!'
}
]
2012-12-16 08:28:52 +00:00
return render_template('index.html',
2012-12-16 08:26:19 +00:00
title = 'Home',
user = user,
posts = posts)
2012-12-16 08:28:52 +00:00
@app.route('/login', methods = ['GET', 'POST'])
2012-12-16 08:30:41 +00:00
@oid.loginhandler
2012-12-16 08:28:52 +00:00
def login():
2012-12-16 08:30:41 +00:00
if g.user is not None and g.user.is_authenticated():
return redirect(url_for('index'))
2012-12-16 08:28:52 +00:00
form = LoginForm()
if form.validate_on_submit():
2012-12-16 08:30:41 +00:00
session['remember_me'] = form.remember_me.data
return oid.try_login(form.openid.data, ask_for = ['nickname', 'email'])
2012-12-16 08:28:52 +00:00
return render_template('login.html',
title = 'Sign In',
form = form,
providers = app.config['OPENID_PROVIDERS'])
2012-12-16 08:30:41 +00:00
@oid.after_login
def after_login(resp):
if resp.email is None or resp.email == "":
flash('Invalid login. Please try again.')
redirect(url_for('login'))
user = User.query.filter_by(email = resp.email).first()
if user is None:
nickname = resp.nickname
if nickname is None or nickname == "":
nickname = resp.email.split('@')[0]
user = User(nickname = nickname, email = resp.email, role = ROLE_USER)
db.session.add(user)
db.session.commit()
remember_me = False
if 'remember_me' in session:
remember_me = session['remember_me']
session.pop('remember_me', None)
login_user(user, remember = remember_me)
return redirect(request.args.get('next') or url_for('index'))
@app.route('/logout')
def logout():
logout_user()
2012-12-16 08:31:21 +00:00
return redirect(url_for('index'))
@app.route('/user/<nickname>')
@login_required
def user(nickname):
user = User.query.filter_by(nickname = nickname).first()
if user == None:
flash('User ' + nickname + ' not found.')
return redirect(url_for('index'))
posts = [
{ 'author': user, 'body': 'Test post #1' },
{ 'author': user, 'body': 'Test post #2' }
]
return render_template('user.html',
user = user,
posts = posts)
@app.route('/edit', methods = ['GET', 'POST'])
@login_required
def edit():
2012-12-16 08:32:38 +00:00
form = EditForm(g.user.nickname)
2012-12-16 08:31:21 +00:00
if form.validate_on_submit():
g.user.nickname = form.nickname.data
g.user.about_me = form.about_me.data
db.session.add(g.user)
db.session.commit()
flash('Your changes have been saved.')
return redirect(url_for('edit'))
2012-12-16 08:32:38 +00:00
elif request.method != "POST":
2012-12-16 08:31:21 +00:00
form.nickname.data = g.user.nickname
form.about_me.data = g.user.about_me
return render_template('edit.html',
form = form)