Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added as example app

  • Loading branch information...
commit 803d20968fea1e25a298aa3b4e670d5670995e0d 0 parents
@mitsuhiko authored
7 .gitignore
@@ -0,0 +1,7 @@
+*.pyc
+*.pyo
+*.egg-info
+*.db
+build
+dist
+.DS_Store
27 README
@@ -0,0 +1,27 @@
+
+ // Flask-Pastebin //
+
+ This application implements an example pastebin with Flask,
+ Flask-OAuth, Flask-SQLAlchemy, Flask-Script, as well as
+ Juggernaut to show off some realtime things.
+
+ What can it do?
+
+ * Facebook Connect
+ * Realtime push notifications with Juggernaut
+ * Shows simple pagination
+
+ How to use it?
+
+ 1. make a virtualenv
+ 2. ``pip install -r requirements.rst``
+ 3. ``python manage.py initdb``
+ 4. ``python manage.py runserver``
+
+ Also make sure to run juggernaut.
+
+ How to install juggernaut?
+
+ $ npm install -g juggernaut
+
+
4 config.cfg
@@ -0,0 +1,4 @@
+DEBUG=False
+SQLALCHEMY_DATABASE_URI='sqlite:///pastebin.db'
+SECRET_KEY='development-key'
+JUGGERNAUT_DRIVER='http://localhost:8080/application.js'
21 manage.py
@@ -0,0 +1,21 @@
+from flask.ext.script import Manager
+from pastebin import app, db
+
+
+manager = Manager(app)
+
+
+@manager.command
+def initdb():
+ """Creates all database tables."""
+ db.create_all()
+
+
+@manager.command
+def dropdb():
+ """Drops all database tables."""
+ db.drop_all()
+
+
+if __name__ == '__main__':
+ manager.run()
164 pastebin.py
@@ -0,0 +1,164 @@
+from datetime import datetime
+from flask import Flask, request, url_for, redirect, g, session, flash, \
+ abort, render_template
+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):
+ args = request.view_args.copy()
+ args['page'] = page
+ return url_for(request.endpoint, **args)
+app.jinja_env.globals['url_for_other_page'] = url_for_other_page
+
+
+class Paste(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+ code = db.Column(db.Text)
+ pub_date = db.Column(db.DateTime)
+ user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
+ parent_id = db.Column(db.Integer, db.ForeignKey('paste.id'))
+ parent = db.relationship('Paste', lazy=True, backref='children',
+ uselist=False, remote_side=[id])
+
+ def __init__(self, user, code, parent=None):
+ self.user = user
+ self.code = code
+ self.pub_date = datetime.utcnow()
+ self.parent = parent
+
+
+class User(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+ display_name = db.Column(db.String(120))
+ fb_id = db.Column(db.String(30), unique=True)
+ 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
+ reply_to = request.args.get('reply_to', type=int)
+ if reply_to is not None:
+ parent = Paste.query.get(reply_to)
+ if request.method == 'POST' and request.form['code']:
+ 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)
+
+
+@app.route('/<int:paste_id>')
+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')
4 requirements.txt
@@ -0,0 +1,4 @@
+Flask
+Flask-OAuth
+Flask-SQLAlchemy
+juggernaut
9,266 static/jquery.js
9,266 additions, 0 deletions not shown
61 static/pastebin.js
@@ -0,0 +1,61 @@
+(function() {
+ var global = this;
+
+ var jug = new Juggernaut();
+
+ var lib = global.pastebin = {
+ urlRoot : '/',
+ jug : jug,
+
+ autoHideFlashes : function() {
+ var flashes = $('p.flash:visible').hide();
+ if (flashes.length) {
+ flashes.slideDown('fast');
+ window.setTimeout(function() {
+ flashes.slideUp('slow');
+ }, 5000);
+ }
+ },
+
+ flash : function(message) {
+ $('<p class=flash></p>')
+ .append(message)
+ .hide()
+ .insertAfter('ul.nav')
+ .slideDown('fast');
+ },
+
+ onNewReply : function(reply, type) {
+ var pasteDescription = '';
+ if (type == 'user') {
+ pasteDescription = 'your paste <a href="' +
+ pastebin.urlRoot + reply.paste_id + '">#' + reply.paste_id + '</a>';
+ } else {
+ pasteDescription = 'this paste';
+ }
+ var msg = $('<span>New reply to ' + pasteDescription + ': <a href="' +
+ pastebin.urlRoot + reply.reply_id + '">#' + reply.reply_id + '</a></span>');
+ if (reply.author)
+ msg.append($('<span></span>').text(' ' + reply.author))
+ lib.flash(msg);
+ },
+
+ subscribePaste : function(pasteID) {
+ jug.subscribe('paste-replies:' + pasteID, function(data) {
+ lib.onNewReply(data, 'paste');
+ });
+ },
+
+ subscribeUser : function(userID) {
+ jug.subscribe('user-replies:' + userID, function(data) {
+ lib.onNewReply(data, 'user');
+ });
+ }
+ };
+
+
+ $(function() {
+ /* animate the server side flashes a bit */
+ lib.autoHideFlashes();
+ });
+})();
21 static/style.css
@@ -0,0 +1,21 @@
+body { margin: 0; padding: 0; }
+body, input { font-size: 16px; font-family: 'Helvetica Neue', sans-serif; }
+.page { margin: 50px auto; width: 740px; }
+h1 { margin: 0; font-weight: normal; color: #c00; }
+a { color: black; }
+a:hover { color: #c00; }
+.nav { margin: 0 0 20px 0; list-style: none; padding: 0; }
+.nav li { display: inline; }
+.nav li + li:before { content: " // "; }
+h2 { font-weight: normal; margin: 0; }
+dl { overflow: auto; font-size: 14px; }
+dl dt { font-weight: bold; width: 90px; float: left;
+ clear: left; }
+dl dd { float: left; margin: 0; padding: 0; }
+pre, textarea { font-family: 'Consolas', monospace; font-size: 14px;
+ background: #eee; padding: 0; margin: 0; }
+textarea { border: none; width: 720px; }
+.code, .flash { background: #eee; margin: 10px -30px; padding: 10px 30px; }
+.pagination strong,
+.pagination span.ellipsis,
+.pagination a { border: 1px solid #d00; padding: 2px 6px; text-decoration: none; }
19 templates/_pagination.html
@@ -0,0 +1,19 @@
+{% macro render_pagination(pagination) %}
+ <div class=pagination>
+ {%- for page in pagination.iter_pages() %}
+ {% if page %}
+ {% if page != pagination.page %}
+ <a href="{{ url_for_other_page(page) }}">{{ page }}</a>
+ {% else %}
+ <strong>{{ page }}</strong>
+ {% endif %}
+ {% else %}
+ <span class=ellipsis>…</span>
+ {% endif %}
+ {%- endfor %}
+ {% if pagination.has_next %}
+ <a href="{{ url_for_other_page(pagination.page + 1)
+ }}">Next &raquo;</a>
+ {% endif %}
+ </div>
+{% endmacro %}
11 templates/delete_paste.html
@@ -0,0 +1,11 @@
+{% 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 %}
28 templates/layout.html
@@ -0,0 +1,28 @@
+<!doctype html>
+<title>{% block title %}{% endblock %} | Flask Pastebin</title>
+<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
+<script type=text/javascript src="{{ config.JUGGERNAUT_DRIVER }}"></script>
+<script type=text/javascript src="{{ url_for('static', filename='jquery.js') }}"></script>
+<script type=text/javascript src="{{ url_for('static', filename='pastebin.js') }}"></script>
+<script type=text/javascript>
+ pastebin.urlRoot = {{ request.url_root|tojson|safe }};
+{%- if g.user %}
+ pastebin.subscribeUser({{ g.user.id }});
+{%- endif %}
+</script>
+<div class=page>
+ <h1>Flask Pastebin</h1>
+ <ul class=nav>
+ <li><a href="{{ url_for('new_paste') }}">New Paste</a>
+ {% if g.user %}
+ <li><a href="{{ url_for('my_pastes') }}">My Pastes</a>
+ <li><a href="{{ url_for('logout') }}">Sign out ({{ g.user.display_name }})</a>
+ {% else %}
+ <li><a href="{{ url_for('login') }}">Sign in with Facebook</a>
+ {% endif %}
+ </ul>
+ {% for message in get_flashed_messages() %}
+ <p class=flash>{{ message }}
+ {% endfor %}
+ {% block body %}{% endblock %}
+</div>
13 templates/my_pastes.html
@@ -0,0 +1,13 @@
+{% 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 %}
9 templates/new_paste.html
@@ -0,0 +1,9 @@
+{% extends "layout.html" %}
+{% block title %}New Paste{% endblock %}
+{% block body %}
+ <h2>New Paste</h2>
+ <form action="" method=post>
+ <div class=code><textarea name=code cols=60 rows=18>{{ parent.code }}</textarea></div>
+ <p><input type=submit value="New Paste">
+ </form>
+{% endblock %}
35 templates/show_paste.html
@@ -0,0 +1,35 @@
+{% extends "layout.html" %}
+{% block title %}Paste #{{ paste.id }}{% endblock %}
+{% block body %}
+ <h2>Paste #{{ paste.id }}</h2>
+ <dl>
+ {% if paste.user %}
+ <dt>Author
+ <dd>{{ paste.user.display_name }}
+ {% endif %}
+ <dt>Date
+ <dd>{{ paste.pub_date.strftime('%Y-%m-%d @ %H:%M') }}
+ <dt>Actions
+ <dd>
+ <a href="{{ url_for('new_paste', reply_to=paste.id) }}">Reply</a>
+ {%- if g.user and paste.user == g.user -%}
+ , <a href="{{ url_for('delete_paste', paste_id=paste.id) }}">Delete</a>
+ {% endif %}
+ {% if paste.parent_id %}
+ <dt>In reply to
+ <dd><a href="{{ url_for('show_paste', paste_id=paste.parent_id) }}">#{{ paste.parent_id }}</a>
+ {% endif %}
+ {% if paste.children %}
+ <dt>Replies
+ <dd>
+ {% for child in paste.children -%}
+ {%- if not loop.first %},{% endif %}
+ <a href="{{ url_for('show_paste', paste_id=child.id) }}">#{{ child.id }}</a>
+ {%- endfor %}
+ {% endif %}
+ </dl>
+ <div class=code><pre>{{ paste.code }}</pre></div>
+ <script type=text/javascript>
+ pastebin.subscribePaste({{ paste.id }});
+ </script>
+{% endblock %}
Please sign in to comment.
Something went wrong with that request. Please try again.