コード例 #1
0
def formatProductTypeForResponse(productType):
    # convert the price to a dollar format
    return {
        "id": productType.id,
        "sku": productType.sku,
        "inventory": productType.inventory,
        "price": helpers.convertIntToFormattedPrice(productType.price),
    }
コード例 #2
0
def createProductType():
    # parse json
    requestJson = request.get_json(force=True)

    # validate params
    sku = requestJson.get("sku")
    inventory = requestJson.get("inventory")
    price = requestJson.get("price")

    if (not sku) or (not price):
        return jsonify(error="One or more required data is not set"), 400
    if not inventory:
        inventory = 0
        # convert dollar formatted price to number of cents
    price = helpers.parsePrice(price)

    productType = models.ProductType(sku, inventory, price)

    db.session.add(productType)

    try:
        db.session.commit()
        # check for errors, uniquness constratint
    except exc.IntegrityError as err:
        return jsonify(error="Duplicate ProductType name not allowed"), 400
    except exc.SQLAlchemyError as err:
        return jsonify(error="Failed to create ProductType"), 400

        # publish update
    helpers.publishUpdate(
        "CREATE_PRODUCT_TYPE",
        dict(
            id=productType.id,
            inventory=productType.inventory,
            price=helpers.convertIntToFormattedPrice(productType.price),
            sku=productType.sku,
        ),
    )

    # return newly created productType
    return jsonify(formatProductTypeForResponse(productType)), 201
コード例 #3
0
ファイル: order_api.py プロジェクト: neogeo/Order_Flask_App
def formatOrderForGetResponse(order):
	shippingAddress = {
		'street' : order.shipping_street,
		'city' : order.shipping_state,
		'state' : order.shipping_city,
		'zip' : order.shipping_zipcode
	}
	billingAddress = {
		'street' : order.billing_street,
		'city' : order.billing_state,
		'state' : order.billing_city,
		'zip' : order.billing_zipcode
	}
	lines = map(formatProductForResponse, order.products)
	return { 
		'id':order.id, 
		'total':helpers.convertIntToFormattedPrice(order.total),
		'shippingAddress':shippingAddress,
		'billingAddress':billingAddress,
		'lines': lines
	}
コード例 #4
0
ファイル: order_api.py プロジェクト: neogeo/Order_Flask_App
def formatProductForResponse(product):
	productType = models.ProductType.query.get(product.product_type_id)
	
	price = helpers.convertIntToFormattedPrice( productType.price )
	return { 'quantity':product.quantity, 'price':price, 'sku':productType.sku}
コード例 #5
0
ファイル: order_api.py プロジェクト: neogeo/Order_Flask_App
def createOrder():
	#parse json
	requestJson = request.get_json(force=True)

	#validate params
	shippingAddress = requestJson.get('shippingAddress')
	billingAddress = requestJson.get('billingAddress')
	lines = requestJson.get('lines')

	if (not shippingAddress) or (not billingAddress) or (not lines):
		return jsonify(error="One or more required data is not set"), 400
	if len(lines)<1:
		return jsonify(error="Required data not set, specify at least 1 product sku"), 400

	#create an order
	order = models.Order()
	#check for 'same' billing address
	sameAddress = billingAddress.get('same') if billingAddress.get('same') else "false"

	order.setShippingAndBillingAddress(shippingAddress, billingAddress, sameAddress)

	#for each line item
	publishList = []
	for line in lines:
		#validate line params
		sku = line.get('sku')
		quantity = line.get('quantity')
		if (not sku) or (not quantity):
			return jsonify(error="Required data on line not set, specify both sku and quantity"), 400
		quantity = int(quantity)

		#check if product sku exists
		productType = models.ProductType.query.filter_by(sku=sku).first()
		#check and update inventory
		if not productType:
			return jsonify(error="Product sku not found for sku: "+sku), 400
		elif not productType.verifyAndUpdateInventory(quantity):
			return jsonify(error="Not enough inventory for sku: "+sku), 400

		#add up total
		lineTotal = productType.price * quantity
		order.increaseTotal(lineTotal)

		product = models.Product(quantity, productType, order)
		#only need to add the product to the session. SQLAlchemy is smart enough to also add the DB relationships during commit
		db.session.add(product)
		
		#add price for response
		line['price'] = helpers.convertIntToFormattedPrice( lineTotal )
		#append to publish list
		publishList.append(dict(id=productType.id, inventory=productType.inventory))

	try:
		db.session.commit()
	#check for errors
	except exc.SQLAlchemyError as err:
		return jsonify(error="Failed to create Order"), 400

	#publish creation
	helpers.publishCreateOrder("UPDATE_INVENTORY", publishList)

	return jsonify(formatOrderForResponse(order, lines)), 201