web.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. import json
  3. import urllib
  4. import datetime
  5. import os
  6. from db import db
  7. from flask import Flask, jsonify, request, send_file
  8. from wand.image import Image
  9. from wand.drawing import Drawing
  10. from wand.color import Color
  11. app = Flask(__name__)
  12. PORT = 8981
  13. BASE_PATH="/back" #changes based on webserver
  14. d = db()
  15. @app.route(BASE_PATH + '/cliente/<_hash>')
  16. def client(_hash):
  17. out=d.client(_hash)
  18. if out is None:
  19. return "{}", 400
  20. out["pedidos"] = d.pedidos(int(out["id"]))
  21. return json.dumps(out)
  22. @app.route(BASE_PATH + '/pedido/<_hash>/<pedidoid>')
  23. def get_pedido(_hash, pedidoid):
  24. c = d.client(_hash)
  25. out = d.get_pedido(c["id"], pedidoid)
  26. if out is None:
  27. return "{}", 400
  28. return json.dumps(out)
  29. @app.route(BASE_PATH + '/productos/<_hash>')
  30. def products(_hash):
  31. c=d.client(_hash)
  32. if c is None:
  33. return "{}", 400
  34. out=d.products()
  35. return json.dumps(out)
  36. @app.route(BASE_PATH + '/pedido', methods=["POST"])
  37. def pedido():
  38. t = datetime.datetime.now()
  39. r = request.get_json()
  40. productos = r["pedido"]["productos"]
  41. cliente = r["pedido"]["cliente"]
  42. obs = r["pedido"]["observaciones"]
  43. if "id" in r["pedido"]:
  44. d.update_pedido(productos, cliente, obs, r["pedido"]["id"])
  45. else:
  46. d.create_pedido(productos, cliente, obs)
  47. return "{}"
  48. @app.route(BASE_PATH + '/confirm/<_hash>/<pedidoid>', methods=["POST"])
  49. def confirm(_hash, pedidoid):
  50. d.confirm(_hash, pedidoid)
  51. return "{}"
  52. @app.route(BASE_PATH + '/image/<_hash>')
  53. def image(_hash):
  54. filename = '/var/www/pedido/back/images/%s.png' % _hash
  55. if not os.path.isfile(filename) or True:
  56. c=d.client(_hash)
  57. draw = Drawing()
  58. with Image(width=300,height=150,background=Color('#2A3A49')) as img:
  59. with Image(filename="/var/www/pedido/back/images/logo.png") as logo:
  60. img.composite(logo, left=int(img.width/2-logo.width/2), top=int(img.height/2-logo.height/2))
  61. draw.fill_color = Color('white')
  62. draw.gravity = "north"
  63. draw.text(0,10, "Pedido")
  64. draw.gravity = "south"
  65. draw.text(0,10, c["nombre"] + " " + c["apellido"])
  66. draw(img)
  67. img.save(filename=filename)
  68. return send_file(filename, mimetype='image/png')
  69. if __name__ == '__main__':
  70. app.run(host="0.0.0.0",port=PORT, debug=True)