def do_post_create_item(self): post_data = get_raw_post_data(self) item_dict = parse_json(post_data) # Pull out all of the fields we need identifier = get_value(item_dict, 'identifier') price = parse_float(get_value(item_dict, 'price', 0)) billing_method = get_value(item_dict, 'billing_method', '') special = get_value(item_dict, 'special') # Ensure that all necessary data is present msg = '' if identifier is None or identifier == '': msg += 'Must provide identifier. ' if price is None or price <= 0: msg += 'Must provide price and must be positive. ' if msg != '': set_response(self, 400, msg, 'text/text') else: # Check to see if the provided special is valid if special is not None: msg = validate_special(special, billing_method) if msg != '': set_response(self, 400, msg, 'text/text') else: # Create and store the item and tell the user everything is fine item = Item(identifier, price, billing_method, special) datastore.set('itemdetails:' + item.identifier, item) set_response(self, 200, '')
def do_post_add_item_to_order(self): post_data = get_raw_post_data(self) post_dict = parse_json(post_data) order_id = get_value(post_dict, 'order_id') item_identifier = get_value(post_dict, 'item_identifier') msg = '' if order_id is None or order_id == '': msg += 'Must provide order_id. ' if item_identifier is None: msg += 'Must provide item. ' if msg != '': set_response(self, 400, msg, 'text/text') else: order = datastore.get('orders:' + order_id) item = datastore.get('itemdetails:' + item_identifier) if order is None: set_response(self, 400, 'Order does not exist.', 'text/text') elif item is None: set_response(self, 400, 'Item does not exist.', 'text/text') else: # If the item is a UNIT type, we only want to allow integers # for the quantity since it doesn't make sense to have something # like 1.25 cans of soup quantity = parse_float(get_value(post_dict, 'quantity'), 1.0) if item.billing_method == Methods.UNIT: quantity = parse_int(quantity) order.add_item(item, quantity) set_response(self, 200, '')
def do_post_data_store(self): post_data = get_raw_post_data(self) post_dict = parse_json(post_data) key = get_value(post_dict, 'key') value = get_value(post_dict, 'value') if key is None: set_response(self, 400, 'Must provide key', 'text/text') elif value is None: set_response(self, 400, 'Must provide value', 'text/text') else: datastore.set(key, value) set_response(self, 200, '')
def do_delete_order(self): post_data = get_raw_post_data(self) post_dict = parse_json(post_data) order_id = get_value(post_dict, 'id') if order_id is None: set_response(self, 400, 'Must provide id.', 'text/text') else: if datastore.get('orders:' + order_id) is not None: datastore.delete('orders:' + order_id) set_response(self, 200, '') else: set_response(self, 400, 'Order does not exist', 'text/text')
def do_post_create_order(self): post_data = get_raw_post_data(self) post_dict = parse_json(post_data) order_id = get_value(post_dict, 'id') # Ensure the client provided an id to create if order_id is None or order_id == '': set_response(self, 400, 'Must provide id.', 'text/text') else: # If the order already exists, we don't want to overwrite it # Instead, tell the user that there was a problem if datastore.get('orders:' + order_id) is None: order = MakeOrder(order_id, datastore) datastore.set('orders:' + order_id, order) set_response(self, 200, '') else: set_response(self, 400, 'Order with that id already exists.', 'text/text')
def do_post_ping(self): # Read the post variables and return them to the client post_data = get_raw_post_data(self) post_dict = parse_json(post_data, dict()) json_resp = json.dumps(post_dict) set_response(self, 200, json_resp)
def test_parse_json_when_given_empty_string_returns_default(self): json_str = '' self.assertEqual(H.parse_json(json_str), None)
def test_parse_json_when_given_invalid_json_with_data_returns_default( self): json_str = '{"test":123' self.assertEqual(H.parse_json(json_str), None)
def test_parse_json_when_given_json_with_data_returns_json_as_dict( self): json_str = '{"test":123}' self.assertEqual(H.parse_json(json_str), {"test": 123})
def test_parse_json_when_given_json_returns_same_json_as_dict(self): json_str = "{}" self.assertEqual(H.parse_json(json_str), dict())