| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #!/usr/bin/env python3
- import json
- import urllib
- import datetime
- import os
- import mail
- from db import db
- from flask import Flask, jsonify, request, send_file
- from wand.image import Image
- from wand.drawing import Drawing
- from wand.color import Color
- app = Flask(__name__)
- PORT = 8981
- BASE_PATH="/back" #changes based on webserver
- d = db()
- @app.route(BASE_PATH + '/cliente/<_hash>')
- def client(_hash):
- out=d.client(_hash)
- if out is None:
- return "{}", 400
- out["pedidos"] = d.pedidos(int(out["id"]))
- return json.dumps(out)
- @app.route(BASE_PATH + '/pedido/<_hash>/<pedidoid>')
- def get_pedido(_hash, pedidoid):
- c = d.client(_hash)
- out = d.get_pedido(c["id"], pedidoid)
- if out is None:
- return "{}", 400
- return json.dumps(out)
- @app.route(BASE_PATH + '/productos/<_hash>')
- def products(_hash):
- c=d.client(_hash)
- if c is None:
- return "{}", 400
- out=d.products()
- return json.dumps(out)
- @app.route(BASE_PATH + '/pedido', methods=["POST"])
- def pedido():
- t = datetime.datetime.now()
- r = request.get_json()
- productos = r["pedido"]["productos"]
- cliente = r["pedido"]["cliente"]
- obs = r["pedido"]["observaciones"]
- if "id" in r["pedido"]:
- d.update_pedido(productos, cliente, obs, r["pedido"]["id"])
- else:
- d.create_pedido(productos, cliente, obs)
- return "{}"
- @app.route(BASE_PATH + '/confirm/<_hash>/<pedidoid>', methods=["POST"])
- def confirm(_hash, pedidoid):
- d.confirm(_hash, pedidoid)
- return "{}"
- @app.route(BASE_PATH + '/image/<_hash>')
- def image(_hash):
- filename = '/var/www/pedido/back/images/%s.png' % _hash
- if not os.path.isfile(filename):
- c=d.client(_hash)
- draw = Drawing()
- with Image(width=300,height=150,background=Color('#2A3A49')) as img:
- with Image(filename="/var/www/pedido/back/images/logo.png") as logo:
- img.composite(logo, left=int(img.width/2-logo.width/2), top=int(img.height/2-logo.height/2))
- draw.fill_color = Color('white')
- draw.gravity = "north"
- draw.text(0,10, "Pedido")
- draw.gravity = "south"
- draw.text(0,10, c["nombre"] + " " + c["apellido"])
- draw(img)
- img.save(filename=filename)
- return send_file(filename, mimetype='image/png')
- @app.route(BASE_PATH + '/reset/<_hash>')
- def reset(_hash):
- r = mail.reset(_hash)
- if r:
- return "{}"
- else:
- return "{}", 400
- if __name__ == '__main__':
- app.run(host="0.0.0.0",port=PORT, debug=True)
|