This repository has been archived by the owner on Dec 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 81a9b62
Showing
8 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.swp | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#Flask and REST example | ||
|
||
A simple RESTful Flask CRUD, based on [snippet #38](http://flask.pocoo.org/snippets/38/). | ||
|
||
##Dependencies | ||
|
||
The only dependency is [Flask](http://flask.pocoo.org), that can be easily installed: | ||
|
||
$ [sudo] pip install flask | ||
|
||
##Running it | ||
|
||
Just run on terminal: | ||
|
||
$ python library.py | ||
|
||
And happy browsing! :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from werkzeug import url_decode | ||
from flask import Flask, render_template, request, redirect, url_for, flash | ||
|
||
class MethodRewriteMiddleware(object): | ||
"""Middleware for HTTP method rewriting. | ||
Snippet: http://flask.pocoo.org/snippets/38/ | ||
""" | ||
|
||
def __init__(self, app): | ||
self.app = app | ||
|
||
def __call__(self, environ, start_response): | ||
if 'METHOD_OVERRIDE' in environ.get('QUERY_STRING', ''): | ||
args = url_decode(environ['QUERY_STRING']) | ||
method = args.get('__METHOD_OVERRIDE__') | ||
if method: | ||
method = method.encode('ascii', 'replace') | ||
environ['REQUEST_METHOD'] = method | ||
return self.app(environ, start_response) | ||
|
||
class Book(object): | ||
"""A Fake model""" | ||
|
||
def __init__(self, id = None, name = None): | ||
self.id = id | ||
self.name = name | ||
|
||
|
||
app = Flask(__name__) | ||
app.config['DEBUG'] = True | ||
app.config['SECRET_KEY'] = 'secret' | ||
app.wsgi_app = MethodRewriteMiddleware(app.wsgi_app) | ||
|
||
@app.route('/books') | ||
def list_books(): | ||
"""GET /books | ||
Lists all books""" | ||
books = [ Book(id=1, name=u'Lord of the rings'), Book(id=2, name=u'Dive into Python') ] # Your query here ;) | ||
return render_template('list_books.html', books=books) | ||
|
||
@app.route('/books/<id>') | ||
def show_book(id): | ||
"""GET /books/<id> | ||
Get a book by its id""" | ||
book = Book(id=id, name=u'My great book') # Your query here ;) | ||
return render_template('show_book.html', book=book) | ||
|
||
@app.route('/books/new') | ||
def new_book(): | ||
"""GET /books/new | ||
The form for a new book""" | ||
return render_template('new_book.html') | ||
|
||
@app.route('/books', methods=['POST',]) | ||
def create_book(): | ||
"""POST /books | ||
Receives a book data and saves it""" | ||
name = request.form['name'] | ||
book = Book(id=2, name=name) # Save it | ||
flash('Book %s sucessful saved!' % book.name) | ||
return redirect(url_for('show_book', id=2)) | ||
|
||
@app.route('/books/<id>/edit') | ||
def edit_book(id): | ||
"""GET /books/<id>/edit | ||
Form for editing a book""" | ||
book = Book(id=id, name=u'Something crazy') # Your query | ||
return render_template('edit_book.html', book=book) | ||
|
||
@app.route('/books/<id>', methods=['PUT']) | ||
def update_book(id): | ||
"""PUT /books/<id> | ||
Updates a book""" | ||
book = Book(id=id, name=u"I don't know") # Your query | ||
book.name = request.form['name'] # Save it | ||
flash('Book %s updated!' % book.name) | ||
return redirect(url_for('show_book', id=book.id)) | ||
|
||
@app.route('/books/<id>', methods=['DELETE']) | ||
def delete_book(id): | ||
"""DELETE /books/<id> | ||
Deletes a books""" | ||
book = Book(id=id, name=u"My book to be deleted") # Your query | ||
flash('Book %s deleted!' % book.name) | ||
return redirect(url_for('list_books')) | ||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 | ||
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
|
||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<title>{% block title %}{% endblock %}</title> | ||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> | ||
</head> | ||
|
||
<body> | ||
{% block content %} | ||
{% endblock %} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} | ||
Edit book {{ book.name }} | ||
{% endblock %} | ||
|
||
{% block content%} | ||
<form action="{{ url_for('update_book', id=book.id, __METHOD_OVERRIDE__='PUT') }}" method="post" accept-charset="utf-8"> | ||
<p> | ||
<label for="name">Name:</label> | ||
<input type="text" name="name" value="{{ book.name }}" id="name"/> | ||
</p> | ||
<p><input type="submit" value="Save!"/></p> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} | ||
Books list | ||
{% endblock %} | ||
|
||
{% block content %} | ||
|
||
{% for msg in get_flashed_messages() %} | ||
<p>{{ msg }}</p> | ||
{% endfor %} | ||
|
||
<ul> | ||
{% for book in books %} | ||
<li>{{ book.id }} - {{ book.name }} (<a href="{{ url_for('delete_book', id=book.id, __METHOD_OVERRIDE__='DELETE') }}">Delete</a>)</li> | ||
{% endfor %} | ||
</ul> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} | ||
New book form | ||
{% endblock %} | ||
|
||
{% block content%} | ||
<form action="{{ url_for('create_book') }}" method="post" accept-charset="utf-8"> | ||
<p> | ||
<label for="name">Name:</label> | ||
<input type="text" name="name" value="{{ book_name }}" id="name"/> | ||
</p> | ||
<p><input type="submit" value="Save!"/></p> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} | ||
Show book {{ book.name }} | ||
{% endblock %} | ||
|
||
{% block content %} | ||
|
||
{% for msg in get_flashed_messages() %} | ||
<p>{{ msg }}</p> | ||
{% endfor %} | ||
|
||
<h1 id="">Book</h1> | ||
|
||
<p> | ||
<strong>Id:</strong> {{ book.id }}<br /> | ||
<strong>Name:</strong> {{ book.name }} | ||
</p> | ||
{% endblock %} |