Advertisement
Guest User

Untitled

a guest
Sep 8th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. """
  2. Simple application with flask-peewee where peewee is a Django-like ORM
  3. <http://peewee.readthedocs.org/en/latest/index.html>
  4. <http://flask-peewee.readthedocs.org/en/latest/index.html>
  5. $ pip install flask-peewee
  6. $ python server.py
  7. * Running on http://127.0.0.1:5000/
  8. * Restarting with reloader
  9. """
  10. import os
  11. from flask import Flask, request, send_from_directory
  12. from flask_peewee.db import Database
  13. from peewee import TextField
  14.  
  15. app = Flask(__name__)
  16.  
  17. root = os.path.dirname(__file__)
  18.  
  19. DATABASE = {
  20. 'name': 'example.db',
  21. 'engine': 'peewee.SqliteDatabase',
  22. 'check_same_thread': False,
  23. }
  24. DEBUG = True
  25. SECRET_KEY = 'shhhh'
  26. app.config.from_object(__name__)
  27. database = Database(app)
  28.  
  29. class Note(database.Model):
  30. key = TextField(unique=True)
  31. json = TextField()
  32.  
  33.  
  34. @app.route('/', methods=["GET", "POST",])
  35. def hello_world():
  36. print "request: ", request.content_type, request.content_length, request.data, ":", request.stream.read()
  37. return request.data
  38.  
  39. @app.route('/note/<key>/', methods=['GET', 'POST',])
  40. def note(key):
  41. """
  42. $ curl -i http://localhost:5000/note/miao/ -X POST -d'{"miao": "bau"}' -H "Content-type: raw"
  43. """
  44. try:
  45. note = Note.get(key=key)
  46. except Note.DoesNotExist:
  47. note = Note.create(key=key, json="{}")
  48. note.save()
  49.  
  50. if request.method == 'GET':
  51. return note.json
  52. else:
  53. note.json = request.stream.read()
  54. note.save()
  55.  
  56. return '{}'
  57.  
  58. @app.route('/librarian/<filename>/', methods=["GET", "POST",])
  59. def librarian(filename):
  60. return send_from_directory(root, filename, as_attachment=True, attachment_filename=filename)
  61.  
  62. @app.errorhandler(404)
  63. def four_o_four(error):
  64. return '<html><body>you are fucked:' + error.message + '</body></html>', 404
  65.  
  66. if __name__ == '__main__':
  67. Note.create_table(fail_silently=True)
  68. app.run(debug=T
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement