|
| 1 | +from os import path |
| 2 | +from json import dumps |
| 3 | + |
| 4 | +from flask import Flask, redirect, request |
| 5 | +from flask.ext.autodoc import Autodoc |
| 6 | + |
| 7 | + |
| 8 | +app = Flask(__name__) |
| 9 | +app.debug = True |
| 10 | +auto = Autodoc(app) |
| 11 | + |
| 12 | +users = [] |
| 13 | +posts = [] |
| 14 | + |
| 15 | + |
| 16 | +class User(object): |
| 17 | + |
| 18 | + def __init__(self, username): |
| 19 | + self.username = username |
| 20 | + users.append(self) |
| 21 | + self.id = users.index(self) |
| 22 | + |
| 23 | + def __repr__(self): |
| 24 | + return dumps(self.__dict__) |
| 25 | + |
| 26 | + |
| 27 | +class Post(object): |
| 28 | + |
| 29 | + def __init__(self, title, content, author): |
| 30 | + self.title = title |
| 31 | + self.content = content |
| 32 | + posts.append(self) |
| 33 | + self.id = posts.index(self) |
| 34 | + |
| 35 | + def __repr__(self): |
| 36 | + return dumps(self.__dict__) |
| 37 | + |
| 38 | + |
| 39 | +u = User('acoomans') |
| 40 | +Post('First post', 'This is the first awesome post', u) |
| 41 | +Post('Second post', 'This is another even more awesome post', u) |
| 42 | + |
| 43 | + |
| 44 | +@app.route('/') |
| 45 | +@app.route('/posts') |
| 46 | +@auto.doc(groups=['posts', 'public', 'private']) |
| 47 | +def get_posts(): |
| 48 | + """Return all posts.""" |
| 49 | + return '%s' % posts |
| 50 | + |
| 51 | + |
| 52 | +@app.route('/post/<int:id>') |
| 53 | +@auto.doc(groups=['posts', 'public', 'private']) |
| 54 | +def get_post(id): |
| 55 | + """Return the post for the given id.""" |
| 56 | + return '%s' % posts[id] |
| 57 | + |
| 58 | + |
| 59 | +@app.route('/post', methods=["POST"]) |
| 60 | +@auto.doc(groups=['posts', 'private']) |
| 61 | +def post_post(): |
| 62 | + """Create a new post. |
| 63 | + Form Data: title, content, authorid. |
| 64 | + """ |
| 65 | + authorid = request.form.get('authorid', None) |
| 66 | + Post(request.form['title'], |
| 67 | + request.form['content'], |
| 68 | + users[authorid]) |
| 69 | + return redirect("/posts") |
| 70 | + |
| 71 | + |
| 72 | +@app.route('/users') |
| 73 | +@auto.doc(groups=['users', 'public', 'private']) |
| 74 | +def get_users(): |
| 75 | + """Return all users.""" |
| 76 | + return '%s' % users |
| 77 | + |
| 78 | + |
| 79 | +@app.route('/user/<int:id>') |
| 80 | +@auto.doc(groups=['users', 'public', 'private']) |
| 81 | +def get_user(id): |
| 82 | + """Return the user for the given id.""" |
| 83 | + return '%s' % users[id] |
| 84 | + |
| 85 | + |
| 86 | +@app.route('/users', methods=['POST']) |
| 87 | +@auto.doc(groups=['users', 'private']) |
| 88 | +def post_user(id): |
| 89 | + """Creates a new user. |
| 90 | + Form Data: username. |
| 91 | + """ |
| 92 | + User(request.form['username']) |
| 93 | + redirect('/users') |
| 94 | + |
| 95 | + |
| 96 | +@app.route('/admin', methods=['GET']) |
| 97 | +@auto.doc(groups=['private']) |
| 98 | +def admin(): |
| 99 | + """Admin interface.""" |
| 100 | + return 'Admin interface' |
| 101 | + |
| 102 | + |
| 103 | +@app.route('/doc/') |
| 104 | +@app.route('/doc/public') |
| 105 | +def public_doc(): |
| 106 | + return auto.html(groups=['public'], title='Blog Documentation with Custom template', template="autodoc_custom.html") |
| 107 | + |
| 108 | + |
| 109 | +@app.route('/doc/private') |
| 110 | +def private_doc(): |
| 111 | + return auto.html(groups=['private'], title='Private Documentation with Custom template', template="autodoc_custom.html") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + app.run() |
0 commit comments