from flask import Blueprint, request, jsonify, abort from api.validators import validate_territory, validate_number, validate_exists from api.models.product import Material from api.models.contract_customer import ContractCustomer from api.models import market as market_model from api.lib.prices import wallpaper_price, old_canvas_price, old_canvas_diy_frame_price, m2_price from api.lib.limits import calculate_canvas_limits mod = Blueprint('prices', __name__, url_prefix='/prices') def get_reseller(): dealerurl = request.args.get('dealerurl') territory = request.args.get('territory') if dealerurl and territory: return ContractCustomer.query.reseller_store(dealerurl, territory) return None @mod.route("/wallpaper", methods=['GET']) @validate_exists('width', 'height', 'territory') @validate_number('width', 'height') @validate_territory('territory') def wallpaper(): width = request.args.get('width', type=int) height = request.args.get('height', type=int) market = market_model.from_territory(request.args.get('territory')) reseller = get_reseller() materials = Material.query.wallpapers().all() data = [] for material in materials: data.append({ 'material': material.name, 'material_id': material.id, 'material_price': m2_price(material, market, reseller=reseller, inc_vat=True), 'price': wallpaper_price(width, height, material, market, reseller=reseller), }) return jsonify(data=data) @mod.route('/canvas', methods=['GET']) @validate_exists('width', 'height', 'territory') @validate_number('width', 'height') @validate_territory('territory') def canvas(): market = market_model.from_territory(request.args.get('territory')) reseller = get_reseller() material = Material.query.canvas().first() data = [] for framed in [True, False]: width, height = calculate_canvas_limits( request.args.get('width', type=int), request.args.get('height', type=int), framed ) data.append({ 'framed': framed, 'width': width, 'height': height, 'price': old_canvas_price(width, height, material, market, reseller=reseller, framed=framed) }) return jsonify(data=data) @mod.route('/diy-frame', methods=['GET']) @validate_exists('width', 'height', 'territory') @validate_number('width', 'height') @validate_territory('territory') def diy_frame(): market = market_model.from_territory(request.args.get('territory')) reseller = get_reseller() width, height = calculate_canvas_limits( request.args.get('width', type=int), request.args.get('height', type=int), framed=True ) data = [{ 'width': width, 'height': height, 'price': old_canvas_diy_frame_price(width, height, market, reseller=reseller, rounded=False), 'price_excl_vat': old_canvas_diy_frame_price(width, height, market, reseller=reseller, rounded=False, inc_vat=False) }] return jsonify(data=data)