def add_voucher(): json_dict = request.json if 'name' not in json_dict: return Responses.OPERATION_FAILED(Messages.VOUCHER_NAME_EMPTY) existing_item = Voucher.get_voucher(json_dict['name']) if existing_item: return Responses.OBJECT_EXIST(Messages.VOUCHER_EXISTS) if (('discount_percent_off' in json_dict and float(json_dict['discount_percent_off']) > 0) and ('discount_fixed_amount' in json_dict and float(json_dict['discount_fixed_amount']) > 0)): return Responses.OPERATION_FAILED(Messages.VOUCHER_DETAILS_WRONG) item = Voucher(json_dict['name']) error = item.insert_as_new_item(json_dict, ['name', 'redeem_by']) if len(error) > 0: return Responses.OPERATION_FAILED(error) return res(item.as_dict())
def update_vouchers(name): """ update_voucher updates voucher by using name Args: name (string): Returns: (string,int): update succesful, otherwise response no need to update """ item = Voucher.get_voucher(name) if not item: return Responses.NOT_EXIST() json_dict = request.json if (('discount_percent_off' in json_dict and float(json_dict['discount_percent_off']) > 0) and ('discount_fixed_amount' in json_dict and float(json_dict['discount_fixed_amount']) > 0)): return Responses.OPERATION_FAILED(Messages.VOUCHER_DETAILS_WRONG) if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()