Basic validation for a board
This commit is contained in:
		
							parent
							
								
									b5a3415cd9
								
							
						
					
					
						commit
						1c1d6a17ef
					
				
					 1 changed files with 18 additions and 2 deletions
				
			
		
							
								
								
									
										20
									
								
								tictactoe.py
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								tictactoe.py
									
										
									
									
									
								
							| 
						 | 
					@ -1,7 +1,23 @@
 | 
				
			||||||
from flask import Flask
 | 
					from flask import Flask, abort, request
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app = Flask(__name__)
 | 
					app = Flask(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@app.route('/')
 | 
					@app.route('/')
 | 
				
			||||||
def hello():
 | 
					def hello():
 | 
				
			||||||
    return 'Hello!'
 | 
					    board = _get_board_or_abort()
 | 
				
			||||||
 | 
					    return board
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _get_board_or_abort():
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    Get the board from the request, or abort the request with a 400 error.
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    board = request.args.get('board', None)
 | 
				
			||||||
 | 
					    is_valid = all(
 | 
				
			||||||
 | 
					        board is not None,  # 'board' var must exist.
 | 
				
			||||||
 | 
					        len(board) >= 9,    # Board should be at least 9 characters long.
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if not is_valid:
 | 
				
			||||||
 | 
					        abort(400)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return board
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue