Removed the useless apishowcase example
This commit is contained in:
parent
0b8e01b445
commit
03168a5d53
7 changed files with 30 additions and 89 deletions
|
|
@ -1,30 +0,0 @@
|
||||||
from flask import Flask, abort, redirect, request, session, \
|
|
||||||
render_template, url_for
|
|
||||||
|
|
||||||
#: create a new flask applications. We pass it the name of our module
|
|
||||||
#: so that flask knows where to look for templates and static files.
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
|
||||||
def index():
|
|
||||||
"""Show an overview page"""
|
|
||||||
return render_template('index.html')
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/hello/', methods=['GET', 'POST'])
|
|
||||||
def hello_user():
|
|
||||||
"""Ask the user for a name and redirect to :func:`hello`"""
|
|
||||||
if request.method == 'POST':
|
|
||||||
return redirect(url_for('hello', name=request.form['name']))
|
|
||||||
return render_template('hello.html', name=None)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/hello/<name>', methods=['GET'])
|
|
||||||
def hello(name):
|
|
||||||
"""Greet name friendly"""
|
|
||||||
return render_template('hello.html', name=name)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(debug=True)
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
body {
|
|
||||||
font-family: 'Trebuchet MS', sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
|
||||||
color: #44AD80;
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
{% extends "layout.html" %}
|
|
||||||
{% block body %}
|
|
||||||
<p>
|
|
||||||
This is an example application that shows how
|
|
||||||
the Werkzeug powered Flask microframework works.
|
|
||||||
<p>
|
|
||||||
The various parts of the example application:
|
|
||||||
<ul>
|
|
||||||
<li><a href="{{ url_for('hello_user') }}">Hello World</a>
|
|
||||||
<li><a href="{{ url_for('counter') }}">Counter</a>
|
|
||||||
</ul>
|
|
||||||
{% endblock %}
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
{% extends "layout.html" %}
|
|
||||||
{% block body %}
|
|
||||||
{% if name %}
|
|
||||||
<h2>Hello {{ name }}!</h2>
|
|
||||||
{% else %}
|
|
||||||
<h3>Hello Stranger …</h3>
|
|
||||||
<form action="{{ url_for('hello_user') }}" method="post">
|
|
||||||
<p>… What's your name?
|
|
||||||
<p><input type=text name=name size=30>
|
|
||||||
<input type=submit value="That's me">
|
|
||||||
</form>
|
|
||||||
{% endif %}
|
|
||||||
{% endblock %}
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
{% extends "layout.html" %}
|
|
||||||
{% block body %}
|
|
||||||
<p>
|
|
||||||
This is an example application that shows how
|
|
||||||
the Werkzeug powered Flask microframework works.
|
|
||||||
<p>
|
|
||||||
The various parts of the example application:
|
|
||||||
<ul>
|
|
||||||
<li><a href="{{ url_for('hello_user') }}">Hello World</a>
|
|
||||||
</ul>
|
|
||||||
{% endblock %}
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
<!doctype html>
|
|
||||||
<title>Flask API Showcase</title>
|
|
||||||
<link rel=stylesheet href="{{ url_for('static', filename='style.css') }}" type=text/css>
|
|
||||||
<h1>Flask API Showcase</h1>
|
|
||||||
{% if request.endpoint != 'index' %}
|
|
||||||
<div class=backlink><a href="{{ url_for('index') }}">« back to index</a></div>
|
|
||||||
{% endif %}
|
|
||||||
{% block body %}{% endblock %}
|
|
||||||
|
|
@ -1,4 +1,13 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
MiniTwit
|
||||||
|
~~~~~~~~
|
||||||
|
|
||||||
|
A microblogging application written with Flask and sqlite3.
|
||||||
|
|
||||||
|
:copyright: (c) 2010 by Armin Ronacher.
|
||||||
|
:license: BSD, see LICENSE for more details.
|
||||||
|
"""
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
@ -66,21 +75,26 @@ def before_request():
|
||||||
up the current user so that we know he's there.
|
up the current user so that we know he's there.
|
||||||
"""
|
"""
|
||||||
g.db = connect_db()
|
g.db = connect_db()
|
||||||
|
g.user = None
|
||||||
if 'user_id' in session:
|
if 'user_id' in session:
|
||||||
g.user = query_db('select * from user where user_id = ?',
|
g.user = query_db('select * from user where user_id = ?',
|
||||||
[session['user_id']], one=True)
|
[session['user_id']], one=True)
|
||||||
|
|
||||||
|
|
||||||
@app.request_shutdown
|
@app.request_shutdown
|
||||||
def after_request(request):
|
def after_request(response):
|
||||||
"""Closes the database again at the end of the request."""
|
"""Closes the database again at the end of the request."""
|
||||||
g.db.close()
|
g.db.close()
|
||||||
return request
|
return response
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def timeline():
|
def timeline():
|
||||||
if not 'user_id' in session:
|
"""Shows a users timeline or if no user is logged in it will
|
||||||
|
redirect to the public timeline. This timeline shows the user's
|
||||||
|
messages as well as all the messages of followed users.
|
||||||
|
"""
|
||||||
|
if not g.user:
|
||||||
return redirect(url_for('public_timeline'))
|
return redirect(url_for('public_timeline'))
|
||||||
offset = request.args.get('offset', type=int)
|
offset = request.args.get('offset', type=int)
|
||||||
return render_template('timeline.html', messages=query_db('''
|
return render_template('timeline.html', messages=query_db('''
|
||||||
|
|
@ -95,6 +109,7 @@ def timeline():
|
||||||
|
|
||||||
@app.route('/public')
|
@app.route('/public')
|
||||||
def public_timeline():
|
def public_timeline():
|
||||||
|
"""Displays the latest messages of all users."""
|
||||||
return render_template('timeline.html', messages=query_db('''
|
return render_template('timeline.html', messages=query_db('''
|
||||||
select message.*, user.* from message, user
|
select message.*, user.* from message, user
|
||||||
where message.author_id = user.user_id
|
where message.author_id = user.user_id
|
||||||
|
|
@ -103,12 +118,13 @@ def public_timeline():
|
||||||
|
|
||||||
@app.route('/<username>')
|
@app.route('/<username>')
|
||||||
def user_timeline(username):
|
def user_timeline(username):
|
||||||
|
"""Display's a users tweets."""
|
||||||
profile_user = query_db('select * from user where username = ?',
|
profile_user = query_db('select * from user where username = ?',
|
||||||
[username], one=True)
|
[username], one=True)
|
||||||
if profile_user is None:
|
if profile_user is None:
|
||||||
abort(404)
|
abort(404)
|
||||||
followd = False
|
followd = False
|
||||||
if 'user_id' in session:
|
if g.user:
|
||||||
followed = query_db('''select 1 from follower where
|
followed = query_db('''select 1 from follower where
|
||||||
follower.who_id = ? and follower.whom_id = ?''',
|
follower.who_id = ? and follower.whom_id = ?''',
|
||||||
[session['user_id'], profile_user['user_id']], one=True) is not None
|
[session['user_id'], profile_user['user_id']], one=True) is not None
|
||||||
|
|
@ -122,7 +138,8 @@ def user_timeline(username):
|
||||||
|
|
||||||
@app.route('/<username>/follow')
|
@app.route('/<username>/follow')
|
||||||
def follow_user(username):
|
def follow_user(username):
|
||||||
if not 'user_id' in session:
|
"""Adds the current user as follower of the given user"""
|
||||||
|
if not g.user:
|
||||||
abort(401)
|
abort(401)
|
||||||
whom_id = get_user_id(username)
|
whom_id = get_user_id(username)
|
||||||
if whom_id is None:
|
if whom_id is None:
|
||||||
|
|
@ -136,7 +153,8 @@ def follow_user(username):
|
||||||
|
|
||||||
@app.route('/<username>/unfollow')
|
@app.route('/<username>/unfollow')
|
||||||
def unfollow_user(username):
|
def unfollow_user(username):
|
||||||
if not 'user_id' in session:
|
"""Removes the current user as follower of the given user"""
|
||||||
|
if not g.user:
|
||||||
abort(401)
|
abort(401)
|
||||||
whom_id = get_user_id(username)
|
whom_id = get_user_id(username)
|
||||||
if whom_id is None:
|
if whom_id is None:
|
||||||
|
|
@ -150,6 +168,7 @@ def unfollow_user(username):
|
||||||
|
|
||||||
@app.route('/add_message', methods=['POST'])
|
@app.route('/add_message', methods=['POST'])
|
||||||
def add_message():
|
def add_message():
|
||||||
|
"""Registers a new message for the user"""
|
||||||
if 'user_id' not in session:
|
if 'user_id' not in session:
|
||||||
abort(401)
|
abort(401)
|
||||||
if request.form['text']:
|
if request.form['text']:
|
||||||
|
|
@ -163,7 +182,8 @@ def add_message():
|
||||||
|
|
||||||
@app.route('/login', methods=['GET', 'POST'])
|
@app.route('/login', methods=['GET', 'POST'])
|
||||||
def login():
|
def login():
|
||||||
if 'user_id' in session:
|
"""Logs the user in"""
|
||||||
|
if g.user:
|
||||||
return redirect(url_for('timeline'))
|
return redirect(url_for('timeline'))
|
||||||
error = None
|
error = None
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
|
@ -183,7 +203,8 @@ def login():
|
||||||
|
|
||||||
@app.route('/register', methods=['GET', 'POST'])
|
@app.route('/register', methods=['GET', 'POST'])
|
||||||
def register():
|
def register():
|
||||||
if 'user_id' in session:
|
"""Registers the user"""
|
||||||
|
if g.user:
|
||||||
return redirect(url_for('timeline'))
|
return redirect(url_for('timeline'))
|
||||||
error = None
|
error = None
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
|
@ -211,6 +232,7 @@ def register():
|
||||||
|
|
||||||
@app.route('/logout')
|
@app.route('/logout')
|
||||||
def logout():
|
def logout():
|
||||||
|
"""Logs the user out"""
|
||||||
flash('You were logged out')
|
flash('You were logged out')
|
||||||
session.pop('user_id', None)
|
session.pop('user_id', None)
|
||||||
return redirect(url_for('public_timeline'))
|
return redirect(url_for('public_timeline'))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue