web.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. import json
  3. import urllib
  4. import datetime
  5. from db import db
  6. from flask import Flask, jsonify, request
  7. app = Flask(__name__)
  8. PORT = 8981
  9. BASE_PATH="/back" #changes based on webserver
  10. d = db()
  11. @app.route(BASE_PATH + '/cliente/<_hash>')
  12. def client(_hash):
  13. out=d.client(_hash)
  14. if out is None:
  15. return "{}", 400
  16. out["pedidos"] = d.pedidos(int(out["id"]))
  17. return json.dumps(out)
  18. @app.route(BASE_PATH + '/pedido/<_hash>/<pedidoid>')
  19. def get_pedido(_hash, pedidoid):
  20. c = d.client(_hash)
  21. out = d.get_pedido(c["id"], pedidoid)
  22. if out is None:
  23. return "{}", 400
  24. return json.dumps(out)
  25. @app.route(BASE_PATH + '/productos/<_hash>')
  26. def products(_hash):
  27. c=d.client(_hash)
  28. if c is None:
  29. return "{}", 400
  30. out=d.products()
  31. return json.dumps(out)
  32. @app.route(BASE_PATH + '/pedido', methods=["POST"])
  33. def pedido():
  34. t = datetime.datetime.now()
  35. r = request.get_json()
  36. productos = r["pedido"]["productos"]
  37. cliente = r["pedido"]["cliente"]
  38. d.create_pedido(productos,cliente)
  39. return "{}"
  40. if __name__ == '__main__':
  41. app.run(host="0.0.0.0",port=PORT, debug=True)