web.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. from flask import Flask,abort,jsonify,request
  3. from db import db
  4. import json
  5. app = Flask(__name__)
  6. d = db()
  7. BASEPATH="/pymnts"
  8. @app.route(BASEPATH+'/')
  9. def index():
  10. return "Hello, World!"
  11. @app.route(BASEPATH+'/articles/', methods=['POST'])
  12. @app.route(BASEPATH+'/articles', methods=['POST'])
  13. def filter_articles():
  14. return jsonify({'articles': d.filter_articles(request.get_json(),limit=80)}) #limit
  15. @app.route(BASEPATH+'/articles/', methods=['GET'])
  16. @app.route(BASEPATH+'/articles', methods=['GET'])
  17. def get_articles():
  18. return jsonify({'articles': d.articles(limit=80)}) #limit
  19. @app.route(BASEPATH+'/article/<string:art_id>', methods=['GET'])
  20. def get_article(art_id):
  21. art=d.articles(id=art_id)
  22. if art is None:
  23. abort(401)
  24. return jsonify({'articles': art})
  25. @app.route(BASEPATH+'/sources/', methods=['GET'])
  26. @app.route(BASEPATH+'/sources', methods=['GET'])
  27. def get_sources():
  28. return jsonify({'sources': d.sources()})
  29. @app.route(BASEPATH+'/sources/<string:source_id>', methods=['GET'])
  30. def get_source(source_id):
  31. source=d.sources(source_id)
  32. if source is None:
  33. abort(401)
  34. return jsonify({'source': source})
  35. @app.route(BASEPATH+'/add_source', methods=['POST'])
  36. def create_task():
  37. if not request.json or not 'title' in request.json:
  38. abort(400)
  39. task = {
  40. 'id': tasks[-1]['id'] + 1,
  41. 'title': request.json['title'],
  42. 'description': request.json.get('description', ""),
  43. 'done': False
  44. }
  45. tasks.append(task)
  46. return jsonify({'task': task}), 201
  47. if __name__ == '__main__':
  48. app.run(debug=True)