diff options
-rw-r--r-- | config.cfg | 1 | ||||
-rw-r--r-- | pastebin.py | 115 | ||||
-rw-r--r-- | templates/delete_paste.html | 11 | ||||
-rw-r--r-- | templates/my_pastes.html | 13 |
4 files changed, 9 insertions, 131 deletions
@@ -1,4 +1,3 @@ DEBUG=False SQLALCHEMY_DATABASE_URI='sqlite:///pastebin.db' SECRET_KEY='development-key' -JUGGERNAUT_DRIVER='http://localhost:8080/application.js' diff --git a/pastebin.py b/pastebin.py index 1a54812..03ab9b8 100644 --- a/pastebin.py +++ b/pastebin.py @@ -1,26 +1,12 @@ from datetime import datetime -from flask import Flask, request, url_for, redirect, g, session, flash, \ - abort, render_template +from flask import (Flask, request, url_for, redirect, g, + render_template, session) from flask.ext.sqlalchemy import SQLAlchemy -from flask.ext.oauth import OAuth -from juggernaut import Juggernaut app = Flask(__name__) app.config.from_pyfile('config.cfg') db = SQLAlchemy(app) -oauth = OAuth() -jug = Juggernaut() - -facebook = oauth.remote_app('facebook', - base_url='https://graph.facebook.com/', - request_token_url=None, - access_token_url='/oauth/access_token', - authorize_url='https://www.facebook.com/dialog/oauth', - consumer_key='188477911223606', - consumer_secret='621413ddea2bcc5b2e83d42fc40495de', - request_token_params={'scope': 'email'} -) def url_for_other_page(page): @@ -30,6 +16,13 @@ def url_for_other_page(page): app.jinja_env.globals['url_for_other_page'] = url_for_other_page +@app.before_request +def check_user_status(): + g.user = None + if 'user_id' in session: + g.user = User.query.get(session['user_id']) + + class Paste(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.Text) @@ -53,26 +46,6 @@ class User(db.Model): pastes = db.relationship(Paste, lazy='dynamic', backref='user') -def send_new_paste_notifications(paste, reply): - """Notifies clients about new pastes.""" - user = None - user_id = None - if paste.user: - user = paste.user.display_name - user_id = paste.user.id - data = {'paste_id': paste.id, 'reply_id': reply.id, 'user': user} - jug.publish('paste-replies:%d' % paste.id, data) - if user_id is not None: - jug.publish('user-replies:%d' % user_id, data) - - -@app.before_request -def check_user_status(): - g.user = None - if 'user_id' in session: - g.user = User.query.get(session['user_id']) - - @app.route('/', methods=['GET', 'POST']) def new_paste(): parent = None @@ -83,8 +56,6 @@ def new_paste(): paste = Paste(g.user, request.form['code'], parent=parent) db.session.add(paste) db.session.commit() - if parent is not None: - send_new_paste_notifications(parent, paste) return redirect(url_for('show_paste', paste_id=paste.id)) return render_template('new_paste.html', parent=parent) @@ -95,71 +66,3 @@ def show_paste(paste_id): paste = Paste.query.options(db.eagerload('children')).get_or_404(paste_id) return render_template('show_paste.html', paste=paste) - -@app.route('/<int:paste_id>/delete', methods=['GET', 'POST']) -def delete_paste(paste_id): - paste = Paste.query.get_or_404(paste_id) - if g.user is None or g.user != paste.user: - abort(401) - if request.method == 'POST': - if 'yes' in request.form: - db.session.delete(paste) - db.session.commit() - flash('Paste was deleted') - return redirect(url_for('new_paste')) - else: - return redirect(url_for('show_paste', paste_id=paste.id)) - return render_template('delete_paste.html', paste=paste) - - -@app.route('/my-pastes/', defaults={'page': 1}) -@app.route('/my-pastes/page/<int:page>') -def my_pastes(page): - if g.user is None: - return redirect(url_for('login', next=request.url)) - pagination = Paste.query.filter_by(user=g.user).paginate(page) - return render_template('my_pastes.html', pagination=pagination) - - -@app.route('/login') -def login(): - return facebook.authorize(callback=url_for('facebook_authorized', - next=request.args.get('next') or request.referrer or None, - _external=True)) - - -@app.route('/logout') -def logout(): - session.clear() - flash('You were logged out') - return redirect(url_for('new_paste')) - - -@app.route('/login/authorized') -@facebook.authorized_handler -def facebook_authorized(resp): - next_url = request.args.get('next') or url_for('new_paste') - if resp is None: - flash('You denied the login') - return redirect(next_url) - - session['fb_access_token'] = (resp['access_token'], '') - - me = facebook.get('/me') - user = User.query.filter_by(fb_id=me.data['id']).first() - if user is None: - user = User() - user.fb_id = me.data['id'] - db.session.add(user) - - user.display_name = me.data['name'] - db.session.commit() - session['user_id'] = user.id - - flash('You are now logged in as %s' % user.display_name) - return redirect(next_url) - - -@facebook.tokengetter -def get_facebook_oauth_token(): - return session.get('fb_access_token') diff --git a/templates/delete_paste.html b/templates/delete_paste.html deleted file mode 100644 index e258dae..0000000 --- a/templates/delete_paste.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends "layout.html" %} -{% block title %}Delete Paste #{{ paste.id }}{% endblock %} -{% block body %} - <h2>Delete Paste #{{ paste.id }}</h2> - <form action="" method=post> - <p>Are you sure you want to delete the paste? You cannot undo this. - <p> - <input type=submit name=yes value=Yes> - <input type=submit name=no value=No> - </form> -{% endblock %} diff --git a/templates/my_pastes.html b/templates/my_pastes.html deleted file mode 100644 index 64967ad..0000000 --- a/templates/my_pastes.html +++ /dev/null @@ -1,13 +0,0 @@ -{% extends "layout.html" %} -{% block title %}My Pastes{% endblock %} -{% from '_pagination.html' import render_pagination %} -{% block body %} - <h2>My Pastes</h2> - <ul> - {% for paste in pagination.items %} - <li><a href="{{ url_for('show_paste', paste_id=paste.id) }}">#{{ paste.id }}</a> - from {{ paste.pub_date.strftime('%Y-%m-%d @ %H:%M') }} - {% endfor %} - </ul> - {{ render_pagination(pagination) }} -{% endblock %} |