Viewing file: __init__.py (5.2 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
from flask import Flask from flask.ext.api import status
from json import dumps from json import loads from flask import request from flask import Response import database_functions import requests
app = Flask(__name__)
flask_debug = True
mirror_api = False
mirror_api_url = "http://104.131.18.172:8080/almazen_test/"
@app.route('/test', methods=['GET']) def test(): return "test"
# API REST @app.route('/product', methods=['GET']) def get_product_list(): # return foreach product {id, name, description, price, qr} # always return local database if (mirror_api is True): response = requests.get(mirror_api_url + '/product') return dumps(response.json()) return dumps(database_functions.get_all_products())
@app.route('/product', methods=['POST']) def add_product(): # data: ex. { 'id' : null, 'name': 'product', 'description': 'a product named product', 'price': 0.5} json_request = loads(request.data) pending = True if (mirror_api is True): response = requests.post(mirror_api_url + '/product', data=dumps(json_request)) if response.status_code == 200: pending = False #TODO: use pending as a state por product creation name = json_request['name'] description = json_request['description'] price = json_request['price'] # at the moment we dont use qr codes for products # qr_code = database_functions.create_qr_for_new_product(name) database_functions.insert_product(name, description, price) return Response("Producto creado", status=200)
@app.route('/product/<string:product_id>', methods=['GET']) def get_product_detail(product_id): return dumps(database_functions.get_product_by_name(product_id))
@app.route('/product/<string:product_id>/qr', methods=['GET']) def get_qrcode(product_id): from flask import send_file filename = database_functions.get_qr_filename_from_product(product_id) # TODO: Verify filename is not None return send_file(filename, mimetype='image/png')
@app.route('/order', methods=['POST']) def create_order(): ''' { "id": null, "seller_id": "seller 1", "client": "client_id", "client_name": "name", "date": <timestamp>, "products": [ { "name": "product1", "count": 5 }, ... ] } ''' json_request = loads(request.data) print 'pedido %s' % str(json_request) pending = True if (mirror_api is True): response = requests.post(mirror_api_url + '/order', data=dumps(json_request)) if response.status_code == 200: pending = False # use pending as a valid state for orders seller_id = json_request['seller_id'] products = json_request['products'] import datetime date = datetime.datetime.now() if 'date' in json_request: date = json_request['date'] if 'client_name' in json_request: client_name = json_request['client_name'] database_functions.insert_order_client_name(seller_id, client_name, products, date) else: client_id = json_request['client_id'] database_functions.insert_order(seller_id, client_id, products, date) return "Orden creada", 200
@app.route('/client', methods=['GET']) def get_all_clients(): if (mirror_api is True): response = requests.get(mirror_api_url + '/client') if response.status_code == 200: return dumps(response.json()) # return foreach client {id, name, business_name , address, cuit, city, iva, mail } clients = database_functions.get_all_clients() # Avoid to convert unicode string. Clients has characters no ascii return dumps(clients)
@app.route('/client', methods=['POST']) def add_new_client(): # data: ex. { 'name': 'client', 'bussiness_name': 'client SRL', 'address': 'client addres 1234', # 'city' : 'client city', 'phone': '0800000', 'cuit': '20-client-3', 'mail': 'client@client.com', 'iva': 'responsable inscripto', # 'observation': '', 'expresso': ''} json_request = loads(request.data) pending = True if (mirror_api is True): response = requests.post(mirror_api_url + '/client', data=dumps(json_request)) if response.status_code == 200: pending = False # use pending as a valid state for orders name = json_request['name'] business_name = json_request['business_name'] address = json_request['address'] city = json_request['city'] phone = json_request['phone'] cuit = json_request['cuit'] mail = json_request['mail'] expresso = json_request['expresso'] province = json_request['province'] client_return = database_functions.insert_client(name, business_name, address, city, phone, cuit, mail, expresso, province) return Response(dumps(client_return), status=200, mimetype='application/json')
@app.route('/client/bussines_name/<string:client_bussines_name>', methods=['GET']) def get_client_by_bussines_name(client_bussines_name): return dumps(database_functions.get_client_by_business_name(client_bussines_name))
@app.route('/client/<string:client_cuit>', methods=['GET']) def get_client_by_cuit(client_cuit): return dumps(database_functions.get_client_by_cuit(client_cuit))
if __name__ == '__main__': app.run('0.0.0.0', debug=flask_debug)
|