web.py 2.3 KB

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