def do_work(self, urlsafeProductKey, qnt): try: ##: Get the Product Model and Best Price productModel, best_price = bestPrice.getBestPrice( urlsafeProductKey, int(qnt), returnProductModel=True) if best_price == None: logging.error('Error, Best Price not determined. ') raise Exception('Best Price not determined.') ndb.Key(urlsafe=urlsafeProductKey).get() if productModel == None: logging.error('Error, No Product Model Found ') raise Exception('No Product Model Found using urlsafe key') ##: Run a query for existing Carts the user has. (PUBLIC Carts) cartList = shoppingModels.Cart.get_carts_for_user(self.user_key) params = {'product': productModel, 'cartList': cartList, 'requested_quantity': int(qnt), 'best_price': utils.dollar_float(float(best_price)), 'total_cost': utils.dollar_float(float(best_price)*float(qnt)),} self.bournee_template('selectCartForm.html', **params) except Exception as e: logging.error('Error, function do_work of class AddToSelectedCartFormHandler: -- %s' % str(e)) message = _('We are having difficulties displaying the form at this time. Please check your entry and try again later.') self.add_message(message, 'error') try: self.redirect(self.request.referer) except: self.redirect_to('home')
def d_hup(cls): if cls.hup: return utils.dollar_float(float(cls.hup)/100) else: pp, hup = ProductTierPrice.get_price_for_qnt(cls.pn, 1) cls.hup = hup cls.put() return utils.dollar_float(float(hup)/100)
def doWork(self, partKey): """ This is a common function for both Methods GET and POST """ productModel = None best_Price = None ##: Remeber the there is a Module called bestPrice... try: ##: Get the Product Model and Best Price ##: partKey is a urlSafe key from the request URL best_Price = bestPrice.getBestPrice( partKey, int(1)) if best_Price == None: logging.error('Error, Best Price not determined. ') raise Exception('Best Price not determined.') if not productModel or not best_Price: logging.error('Error, No Product Model Found ') raise Exception('No Product Model Found using urlsafe key and the function getBestPrice') params = {'product': productModel, 'best_price': dollar_float(float(best_Price)), } self.bournee_template('createAlertForm.html', **params) except Exception as e: logging.error('Error, in CreateAlertFormHandler: -- %s' % str(e)) message = _('We are having difficulties with the request for the Alert Form. Please try again later.') self.add_message(message, 'error') try: self.redirect(self.request.referer) except: self.redirect_to('home')
def doWork(self, partKey, quantity): """ This is a common function for both Methods GET and POST """ productModel = None best_price = None try: ##: Make Sure quantity from URL is an integer qnt = int(quantity) logging.info('Quantity suplied in CreateLimitOrderFormHandler = %s' % str(qnt)) ##: Get the Product Model and Best Price ##: partKey is a urlSafe key from the request URL productModel, best_price = bestPrice.getBestPrice( partKey, int(qnt)) if best_price == None: logging.error('Error, Best Price not determined. ') raise Exception('Best Price not determined.') if not productModel or not best_price: logging.error('Error, No Product Model Found ') raise Exception('No Product Model Found using urlsafe key and the function getBestPrice') params = {'product': productModel, 'requested_quantity': int(qnt), 'best_price': dollar_float(float(best_price)), } self.bournee_template('limitOrder.html', **params) except Exception as e: logging.error('Error, Updating Quantity for CreateLimitOrderFormHandler: -- %s' % str(e)) message = _('We are having difficulties with the request for the Limit Order Form. Please try again later.') self.add_message(message, 'error') try: self.redirect(self.request.referer) except: self.redirect_to('home')
def d_bup(cls): return utils.dollar_float(float(cls.bup)/100)
def d_gt(cls): ## d_gt = Dollar Grand-Total return utils.dollar_float(float(cls.gt)/100)
def d_mu(cls): ## d_mu = Dollar Mark-up return utils.dollar_float(float(cls.mu)/100)
def d_sh_fees(cls): ## d_sh = Dollar Shipping sh_fees = int(cls.sh) + int(cls.mu) return utils.dollar_float(float(sh_fees)/100)
def d_tx(cls): ## d_tx = Dollar Taxes return utils.dollar_float(float(cls.tx)/100)
def d_st(cls): ## d_st = Dollar Sub-Total return utils.dollar_float(float(cls.st)/100)
def d_sup(cls): ## dlp = Dollar Limit Price return utils.dollar_float(float(cls.sup)/100)
def d_cp(cls): return utils.dollar_float(float(cls.cp)/100)
def doWork(self, productModel): """ This is a common function for both Methods GET and POST """ best_price = None qnt = self.request.get('q', 1) try: quantity = int(qnt) except: logging.error('Error, QNT supplied in request URI was not an integer: QNT = %s' % str(qnt)) raise Exception('Error, QNT supplied in request URI was not an integer: QNT = %s' % str(qnt)) try: ######################################################################## ################# ######## Send to bestPrice script ################# ######################################################################## try: best_price = bestPrice.getBestPrice(productModel.key.urlsafe(), int(quantity)) except Exception as e: logging.error('Error finding Product and/or Price in function getProduct of ProductRequestHandler : %s' % e) message = _('We could not find a Price result for your Product Request. Please try again later.') self.add_message(message, 'error') try: self.redirect(self.request.referer) except: self.redirect_to('home') ######################################################################## ################# ######## Check Results for None's ################# ######################################################################## if best_price == None or best_price <= 0.0: raise Exception('We could not find a Price result for your Product Request. Please try again later.') ## This is an error and we Need a Price to show the User logging.error("Best Price returned as None in function getProduct of ProductRequestHandler.") message = _('We could not find a Price result for your Product Request. Please try again later.') self.add_message(message, 'error') try: self.redirect(self.request.referer) except: self.redirect_to('home') ######################################################################## ##: Add this product to the last products viewed memcache ######################################################################## try: lpv = memcache.get('%s:lastProductsViewed' % str(self.request.remote_addr)) if lpv == None: lpv = [] if productModel in lpv: lpv.remove(productModel) if len(lpv)>10: lastItem = lpv.pop() lpv.insert(0,productModel) memcache.set('%s:lastProductsViewed' % str(self.request.remote_addr),lpv) except Exception as e: logging.error('Error setting Memcache for lastProductsViewed in class ProductRequestHandler : %s' % e) ######################################################################## ##: This is the analytics counter for an idividual product ######################################################################## try: counter.load_and_increment_counter(name=productModel.key.urlsafe(), period_types=[counter.PeriodType.ALL,counter.PeriodType.YEAR], namespace="products") except Exception as e: logging.error('Error setting LiveCount for product in class ProductRequestHandler : %s' % e) params = { 'product': productModel, 'best_price': utils.dollar_float(float(best_price)), 'requested_quantity': int(quantity), 'total_cost': utils.dollar_float(float(best_price)*float(quantity)) } self.bournee_template('product.html', **params) except Exception as e: logging.error('Error finding Product and/or Price in function doWork of ProductRequestHandler : %s' % e) message = _('We could not find a Price result for your Product Request. Please try again later.') self.add_message(message, 'error') try: self.redirect(self.request.referer) except: self.redirect_to('home')
def post(self, pn): if not self.exchangeQnt_form.validate(): raise Exception('ExchangeQNTForm Form Not Validated, in function post of ExchangeOrderHandler') try: partNumber = str(pn).upper() qnt = int(self.exchangeQnt_form.qnt.data) meq = int(self.exchangeQnt_form.meq.data) mep = float(self.exchangeQnt_form.mep.data) percentage = int(self.exchangeQnt_form.percentage.data) urlsafeProductKey = str(self.exchangeQnt_form.partNumber.data.strip()).upper() price = None ##: Try to find the part in the Datastore. productKey = ndb.Key(urlsafe=urlsafeProductKey) productModel = productKey.get() if not productModel: raise Exception('Error fetching the Product with the productNumber from the URI, in function post of ExchangeOrderHandler') else: logging.info('Quantity Supplied is %s' % str(qnt)) priceModel, cents_price = shoppingModels.ProductTierPrice.get_price_for_qnt(str(urlsafeProductKey), int(qnt)) price = float(cents_price)/100 logging.info('Price returned is %s' % str(price)) if price is None: raise Exception('Error determining price from the productTierPrice Model, in function post of ExchangeOrderHandler') ##: This Loop will determine the Maximum Markup Percentage (mmp) a Market Maker can charge on this product. maxPercentage = int(percentage) oneHPrice = float(priceModel.oH)/100 per=1.50 while per >= 1.01: sellPrice = float(price) * float(per) if float(sellPrice) > float(oneHPrice): ##: If the determined sell price is higher than the 100 unit Price then this is a fail. It must be lower. per -= 0.01 continue else: maxPercentage = int(per*100)-100 maxSellPrice = sellPrice break if maxPercentage < percentage: percentage = maxPercentage r_ppp = float(percentage + 100)/100 if float(price) * float(r_ppp-1.00) <= 0.01: rep = float(price) + 0.01 else: rep = float(price) * float(r_ppp) pProfit = (float(rep) - float(price)) * float(qnt) ##: Potential Profit fee = float(pProfit) * float(0.05) ##: 5% of profit #: Calculate the cost with the minimums cost = float(qnt)*float(price) + float(fee) except Exception as e: logging.error('Error in function POST of ExchangeOrderHandler : %s' % e) message = _('We are having difficulties with the requested Product. Please try again later.') self.add_message(message, 'error') return self.get(str(pn)) message = _('Parameters have been updated by your request') self.add_message(message, 'success') params = {'product': productModel, 'meq': int(meq), # Needed in template during a Get request 'mep': dollar_float(mep), # Needed in template during a Get request 'rep': dollar_float(rep), 'pProfit': dollar_float(pProfit), 'percentage': int(percentage), 'maxPercentage': int(maxPercentage), 'maxSellPrice': dollar_float(maxSellPrice), 'qnt': int(qnt), # Needed in template during a POST request 'up': dollar_float(price), # Needed in template during a POST request 'cost': dollar_float(cost), 'fee': dollar_float(fee), } return self.bournee_template('exchangeOrder.html', **params)
def get(self, pk): """ Returns page containing part info and exchange order form """ ##: pn = partNumber try: ##: Try to find the part in the Datastore. productKey = ndb.Key(urlsafe=pk) productModel = productKey.get() if productModel: logging.info('Product MPN = {}'.format(productModel.pn)) ##: Get the minimum Exchange Order qnt and unit price for this part. productTierPrice = shoppingModels.ProductTierPrice.get_for_product(productModel.pn, productKey) if productTierPrice: meq = int(productTierPrice.meq) mep = float(productTierPrice.mep)/100 rep = float(productTierPrice.rep)/100 pProfit = float(productTierPrice.pp)/100 percentage = int(productTierPrice.ppp) fee = float(pProfit) * float(0.05) ##: 5% of profit #: Calculate the cost with the minimums cost = (float(meq)*float(mep)) + fee ##: This Loop will determine the Maximum Markup Percentage (mmp) a Market Maker can charge on this product. maxPercentage = percentage oneHPrice = float(productTierPrice.oH)/100 per=1.50 while per >= 1.01: sellPrice = float(mep) * float(per) if float(sellPrice) > float(oneHPrice): ##: If the determined sell price is higher than the 100 unit Price then this is a fail. It must be lower. per -= 0.01 continue else: maxPercentage = int(per*100)-100 maxSellPrice = sellPrice break if maxPercentage < percentage: percentage = maxPercentage params = {'product': productModel, 'meq': int(meq), # Needed in template during a Get request 'mep': dollar_float(mep), # Needed in template during a Get request 'rep': dollar_float(rep), 'pProfit': dollar_float(pProfit), 'percentage': int(percentage), 'maxPercentage': int(maxPercentage), 'maxSellPrice': dollar_float(mep), 'qnt': int(meq), # Needed in template during a POST request 'up': dollar_float(mep), # Needed in template during a POST request 'cost': dollar_float(cost), 'fee': dollar_float(fee), } self.bournee_template('exchangeOrder.html', **params) else: logging.error('No productTierPrice Found.') raise Exception('No productTierPrice Found.') else: logging.error('No productModel Found.') raise Exception('No productModel Found.') except Exception as e: logging.error('Error finding Product and/or Price in function get of ExchangeOrderHandler : %s' % e) message = _('We are having difficulties with the requested Product. Please try again later.') self.add_message(message, 'error') try: self.redirect(self.request.referer) except: self.redirect_to('home')