Added snippet admin and profile editing
This commit is contained in:
parent
aa79c72ae0
commit
d87cbff134
14 changed files with 228 additions and 40 deletions
|
|
@ -1,6 +1,8 @@
|
|||
from flask import Module, render_template, session, redirect, url_for, \
|
||||
request, flash, g, Response
|
||||
from flaskext.openid import COMMON_PROVIDERS
|
||||
from flask_website import oid, twitter
|
||||
from flask_website.utils import requires_login
|
||||
from flask_website.database import db_session, User
|
||||
|
||||
general = Module(__name__)
|
||||
|
|
@ -30,28 +32,20 @@ def logout():
|
|||
def login():
|
||||
if g.user is not None:
|
||||
return redirect(url_for('general.index'))
|
||||
if request.method == 'POST':
|
||||
openid = request.values.get('openid')
|
||||
if openid:
|
||||
return oid.try_login(openid, ask_for=['fullname', 'nickname'])
|
||||
if reassign and 'cancel' in request.form:
|
||||
flash(u'Cancelled. The OpenID was not changed.')
|
||||
return redirect(oid.get_next_url())
|
||||
openid = request.values.get('openid')
|
||||
if not openid:
|
||||
openid = COMMON_PROVIDERS.get(request.args.get('provider'))
|
||||
if openid:
|
||||
return oid.try_login(openid, ask_for=['fullname', 'nickname'])
|
||||
error = oid.fetch_error()
|
||||
if error:
|
||||
flash(u'Error: ' + error)
|
||||
return render_template('general/login.html', next=oid.get_next_url())
|
||||
|
||||
|
||||
@oid.after_login
|
||||
def create_or_login(resp):
|
||||
session['openid'] = resp.identity_url
|
||||
user = User.query.filter_by(openid=resp.identity_url).first()
|
||||
if user is not None:
|
||||
flash(u'Successfully signed in')
|
||||
g.user = user
|
||||
return redirect(oid.get_next_url())
|
||||
return redirect(url_for('first_login', next=oid.get_next_url(),
|
||||
name=resp.fullname or resp.nickname))
|
||||
|
||||
|
||||
@general.route('/first-login/', methods=['GET', 'POST'])
|
||||
def first_login():
|
||||
if g.user is not None or 'openid' not in session:
|
||||
|
|
@ -68,3 +62,55 @@ def first_login():
|
|||
return render_template('general/first_login.html',
|
||||
next=oid.get_next_url(),
|
||||
openid=session['openid'])
|
||||
|
||||
|
||||
@general.route('/profile/', methods=['GET', 'POST'])
|
||||
@requires_login
|
||||
def profile():
|
||||
name = g.user.name
|
||||
if request.method == 'POST':
|
||||
name = request.form['name'].strip()
|
||||
if not name:
|
||||
flash(u'Error: a name is required')
|
||||
else:
|
||||
g.user.name = name
|
||||
db_session.commit()
|
||||
flash(u'User profile updated')
|
||||
return redirect(url_for('index'))
|
||||
return render_template('general/profile.html', name=name)
|
||||
|
||||
|
||||
@general.route('/profile/change-openid/', methods=['GET', 'POST'])
|
||||
@requires_login
|
||||
@oid.loginhandler
|
||||
def change_openid():
|
||||
if request.method == 'POST':
|
||||
if 'cancel' in request.form:
|
||||
flash(u'Cancelled. The OpenID was not changed.')
|
||||
return redirect(oid.get_next_url())
|
||||
openid = request.values.get('openid')
|
||||
if not openid:
|
||||
openid = COMMON_PROVIDERS.get(request.args.get('provider'))
|
||||
if openid:
|
||||
return oid.try_login(openid)
|
||||
error = oid.fetch_error()
|
||||
if error:
|
||||
flash(u'Error: ' + error)
|
||||
return render_template('general/change_openid.html',
|
||||
next=oid.get_next_url())
|
||||
|
||||
|
||||
@oid.after_login
|
||||
def create_or_login(resp):
|
||||
session['openid'] = resp.identity_url
|
||||
user = g.user or User.query.filter_by(openid=resp.identity_url).first()
|
||||
if user is None:
|
||||
return redirect(url_for('first_login', next=oid.get_next_url(),
|
||||
name=resp.fullname or resp.nickname))
|
||||
if user.openid != resp.identity_url:
|
||||
user.openid = resp.identity_url
|
||||
db_session.commit()
|
||||
flash(u'OpenID identity changed')
|
||||
else:
|
||||
flash(u'Successfully signed in')
|
||||
return redirect(oid.get_next_url())
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ def new():
|
|||
else:
|
||||
title = request.form['title']
|
||||
body = request.form['body']
|
||||
if not body:
|
||||
if body:
|
||||
flash(u'Error: you have to enter a snippet')
|
||||
else:
|
||||
category = Category.query.get(category_id)
|
||||
|
|
@ -55,9 +55,7 @@ def show(id):
|
|||
if request.method == 'POST':
|
||||
title = request.form['title']
|
||||
text = request.form['text']
|
||||
if not text:
|
||||
flash(u'Error: the text is required')
|
||||
else:
|
||||
if text:
|
||||
db_session.add(Comment(snippet, g.user, title, text))
|
||||
db_session.commit()
|
||||
flash(u'Your comment was added')
|
||||
|
|
@ -65,6 +63,35 @@ def show(id):
|
|||
return render_template('snippets/show.html', snippet=snippet)
|
||||
|
||||
|
||||
@snippets.route('/comments/<int:id>/', methods=['GET', 'POST'])
|
||||
@requires_admin
|
||||
def edit_comment(id):
|
||||
comment = Comment.query.get(id)
|
||||
if comment is None:
|
||||
abort(404)
|
||||
form = dict(title=comment.title, text=comment.text)
|
||||
if request.method == 'POST':
|
||||
if 'delete' in request.form:
|
||||
db_session.delete(comment)
|
||||
db_session.commit()
|
||||
flash(u'Comment was deleted.')
|
||||
return redirect(comment.snippet.url)
|
||||
elif 'cancel' in request.form:
|
||||
return redirect(comment.snippet.url)
|
||||
form['title'] = request.form['title']
|
||||
form['text'] = request.form['text']
|
||||
if not form['text']:
|
||||
flash(u'Error: comment text is required.')
|
||||
else:
|
||||
comment.title = form['title']
|
||||
comment.text = form['text']
|
||||
db_session.commit()
|
||||
flash(u'Comment was updated.')
|
||||
return redirect(comment.snippet.url)
|
||||
return render_template('snippets/edit_comment.html', form=form,
|
||||
comment=comment)
|
||||
|
||||
|
||||
@snippets.route('/edit/<int:id>/', methods=['GET', 'POST'])
|
||||
@requires_login
|
||||
def edit(id):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue