#!/usr/bin/env python3 import json import urllib import datetime import os 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>/') 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>/', 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) or True: 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') if __name__ == '__main__': app.run(host="0.0.0.0",port=PORT, debug=True)