def post(self): #log.debug("Add VIP") params = {} try: params = json_util.decodeDict(json.loads(self.request.body)) except ValueError: self.error(400) self.renderJson( {'error': "The information provided was incomplete."}) if self.checkParamsErrors(params): params[ 'error'] = "The information provided was incomplete or incorrect." self.error(400) self.renderJson(params) else: self.name = params['name'] self.dob = self.checkDOB(params['dob']) self.phone = params['phone'] #make sure the user doesn't already exist if Customer.findCustomer(self.name, self.dob, self.phone): msg = 'That user already exists.' error_dict = {'error': msg} self.error(400) self.renderJson(error_dict) else: customer = Customer.register(self.name, self.dob, self.phone) self.response.set_status(201) self.renderJson(customer.asDict())
def post(self): have_error = False params = {} try: params = json_util.decodeDict(json.loads(self.request.body)) pprint(params) except ValueError: self.error(400) self.renderJson( {'error': "The information provided was incomplete."}) if not 'price' in params.keys(): params['error_price'] = "No price was provided." have_error = True if not 'type' in params.keys(): params['error_type'] = "No type was provided." have_error = True if not 'best_seller' in params.keys(): params['error_best_seller'] = "best_seller was not provided." have_error = True if not 'is_drink' in params.keys(): params['is_drink'] = False item_data = [ params['price'], params['type'], params['best_seller'], params['is_drink'] ] added_item = Item.newItem(*item_data) self.write("Done")
def sendRequest(test, method, uid=None, params=None, returnCode=200): uri = '/vip/' if uid: uri = uri + str(uid) request = webapp2.Request.blank(uri) request.method = method if params: request.body = json.dumps(params) response = request.get_response(main.app) test.assertEqual(response.status_int, returnCode) return json_util.decodeDict(json.loads(response.body))
def put(self, uid=None): #log.debug("Edit VIP") have_error = False params = {} try: params = json_util.decodeDict(json.loads(self.request.body)) except ValueError: self.error(400) self.renderJson( {'error': "The information provided was incomplete."}) if not uid: params['error_card_number'] = "No vip card number provided." have_error = True if self.checkParamsErrors(params) or have_error: params['error'] = "The information provided was incomplete." self.error(400) self.renderJson(params) else: #make sure the user doesn't already exist self.uid = int(uid) customer = Customer.byID(self.uid) if customer: self.name = params['name'] self.dob = self.checkDOB(params['dob']) self.phone = params['phone'] new_info_cust = Customer.findCustomer(self.name, self.dob, self.phone) if not new_info_cust: if customer.updateInfo(self.uid, name=self.name, dob=self.dob, phone=self.phone): return_val = { "complete": "The vip account was successfully updated." } self.renderJson(return_val) else: msg = 'The info provided matches that of an existing customer.' error_dict = {'error': msg} self.error(400) self.renderJson(error_dict) else: msg = 'The vip card number provided does not exist.' error_dict = {'error': msg} self.error(404) self.renderJson(error_dict)
def post(self): #log.debug("PreOrderHandler") have_error = False params = {} try: params = json_util.decodeDict(json.loads(self.request.body)) except ValueError: self.error(400) self.renderJson( {'error': "The information provided was incomplete."}) if not 'card_number' in params.keys(): params['error_card_number'] = "No vip card number provided." have_error = True else: customer = Customer.byID(params["card_number"]) if not customer: params[ 'error_card_number'] = "The vip card number provided does not exist." have_error = True if not 'item_list' in params.keys() or len(params['item_list']) == 0: params['error_items'] = "No list of items preordered was provided." have_error = True else: error_items = "Invalid item ID provided:" have_item_error = False for itemID in params['item_list']: if not Item.byID(itemID): error_items = error_items + " " + str(itemID) + "," have_item_error = True if have_item_error: params['error_items'] = rstrip(error_items, ',') have_error = True if not 'cart_number' in params.keys(): params[ 'error_cart_num'] = "The cart number where the preorder was placed was not provided." have_error = True if not 'preorder_date' in params.keys(): params[ 'error_preorder_date'] = "The fulfillment date for the preorder was not provided." have_error = True else: self.pre_order_date = self.checkPreorderDate( params["preorder_date"]) if not self.pre_order_date: params[ 'error_preorder_date'] = "The fulfillment date provided for the preorder was not valid." have_error = True if have_error: params['error'] = "The information provided was incomplete." self.error(400) self.renderJson(params) else: result = Preorder.newPreorder(params['card_number'], self.pre_order_date, params['item_list'], params["cart_number"]) if isinstance(result, bool): result = {"complete": "Preorder successfully added."} self.renderJson(result) else: params[ 'error'] = "There was an error while completing the preorder." params['error_items'] = result self.error(400) self.renderJson(params)
def post(self): #log.debug("PurchaseHandler") have_error = False params = {} try: params = json_util.decodeDict(json.loads(self.request.body)) except ValueError: self.error(400) self.renderJson( {'error': "The information provided was incomplete."}) if not 'card_number' in params.keys(): params['error_card_number'] = "No vip card number provided." have_error = True else: self.customer = Customer.byID(params["card_number"]) if not self.customer: params[ 'error_card_number'] = "The vip card number provided does not exist." have_error = True self.order_list = [] if not 'item_list' in params.keys() or len(params['item_list']) == 0: params['error_items'] = "No list of items was provided." have_error = True else: error_items = "Invalid item ID provided:" have_item_error = False for itemID in params['item_list']: item = Item.byID(itemID) if not item: error_items = error_items + " " + str(itemID) + "," have_item_error = True else: self.order_list.append(item) if have_item_error: params['error_items'] = rstrip(error_items, ',') have_error = True if not 'cart_number' in params.keys(): params[ 'error_cart_num'] = "The cart number where this order was placed was not provided." have_error = True if have_error: params['error'] = "The information provided was incomplete." self.error(400) self.renderJson(params) else: purcahse = Purchase.newPurchase(self.customer, self.order_list, params["cart_number"]) if purcahse: result = { "complete": "Order successfully added.", "purchase": purcahse.asDict() } self.renderJson(result) else: params[ 'error'] = "There was an error while completing the order." self.error(400) self.renderJson(params)