def signout(): """ Request: { "auth_token": "auth_token" } Response [422] (unauthenticated): { "errors": { "UnauthorizedAccess": [ "Attempted to access data without an authenticated player" ] } } Response [422] (signout failure - save error): { "errors": { "SignoutError": [ "Unable to sign out player" ] } } Response [200] (success): { "Success": "Player signed out" } """ if get_current_user() and get_current_user().logout(): return render_view('players/show', 200, player={'Success': 'Player signed out'}) else: return render_view('422', 422, errors=UNABLE_TO_SIGNOUT_ERROR)
def show(definition_filler_id): """ Request: {} Response [422] (definition_filler with definition_filler_id doesn't exist): { "errors": { "DefinitionFillerNotFound": [ "Unable to find DefinitionFiller" ] }, "inputs": { "id": "definition_filler_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "definition_template": "current value", "filler": "current value", "is_dictionary": "current value", "is_active": "current value" } """ # Get the definition_filler definition_filler = DefinitionFillersService.get_instance().get(definition_filler_id) if definition_filler: return render_view('definition_fillers/show', 200, definition_filler=definition_filler.serialized) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': definition_filler_id})
def index(): """ Request: { "offset": "offset", "limit": "limit" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "offset": "value passed in. empty string if missing", "limit": "value passed in. empty string if missing" } } Response [200] (success): [ { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" }, { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" }, ... ] """ # Get the input validator inputs = ListInputs(get_inputs()) # Verify the list inputs if inputs.validate(): games = GamesService.get_instance().get_list(inputs.limit.data, inputs.offset.data) return render_view('games/index', 200, games={game.get_id(): game.serialized for game in games}) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def show(word_id): # Get the word word = WordsService.get_instance().get(word_id) if word: return render_view('words/show', 200, word=word.serialized) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': word_id})
def index(): """ Request: { "offset": "offset", "limit": "limit" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "offset": "value passed in. empty string if missing", "limit": "value passed in. empty string if missing" } } Response [200] (success): [ { "id": "current value", "date_created": "current value", "date_modified": "current value", "username": "******", "email": "current value", "avatar_url": "current value", "is_active": "current value" }, { "id": "current value", "date_created": "current value", "date_modified": "current value", "username": "******", "email": "current value", "avatar_url": "current value", "is_active": "current value" }, ... ] """ # Get the input validator inputs = ListInputs(get_inputs()) # Verify the list inputs if inputs.validate(): players = PlayersService.get_instance().get_list(inputs.limit.data, inputs.offset.data) return render_view('players/index', 200, players={player.get_id(): player.serialized for player in players}) return render_view('422', 422, errors=inputs.errors, inputs=inputs.obfuscated())
def delete(definition_filler_id): """ Request: {} Response [422] (definition_filler with definition_filler_id doesn't exist): { "errors": { "DefinitionFillerNotFound": [ "Unable to find DefinitionFiller" ] }, "inputs": { "id": "definition_filler_id" } } Response [422] (save failure - unable to set definition_filler as inactive): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "id": "definition_filler_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "definition_template": "current value", "filler": "current value", "is_dictionary": "current value", "is_active": "current value" } """ # Get the definition_filler definition_filler = DefinitionFillersService.get_instance().get(definition_filler_id) # Verify the definition_filler creation inputs if definition_filler: try: definition_filler.update(**{'is_active': False}) return render_view('definition_fillers/show', 200, definition_filler=definition_filler.serialized) except Exception as e: return render_view( '422', 422, errors={e.__class__.__name__: [e.message]}, inputs={'id': definition_filler_id} ) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': definition_filler_id})
def index(): # Get the input validator inputs = ListInputs(get_inputs()) # Verify the list inputs if inputs.validate(): words = WordsService.get_instance().get_list(inputs.limit.data, inputs.offset.data) return render_view('words/index', 200, words={word.get_id(): word.serialized for word in words}) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def delete(game_id): """ Request: {} Response [422] (game with game_id doesn't exist): { "errors": { "GameNotFound": [ "Unable to find Game" ] }, "inputs": { "id": "game_id" } } Response [422] (save failure - unable to set game as inactive): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "id": "game_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" } """ # Get the game game = GamesService.get_instance().get(game_id) # Verify the game creation inputs if game: try: game.update(**{'is_active': False}) return render_view('games/show', 200, game=game.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs={'id': game_id}) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': game_id})
def create(): # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the match creation inputs if inputs.validate_on_submit(): # Ensure we have a valid game game = GamesService.get_instance().get(inputs.game_id.data) if game: # If an opponent is specified, match with that opponent if inputs.opponent_id.data: # Ensure that the opponent is a valid user opponent = PlayersService.get_instance().get(inputs.opponent_id.data) if not opponent: return render_view('422', 422, errors=OPPONENT_NOT_FOUND_ERROR, inputs=inputs.serialized()) # First attempt to find a match already requested by the desired opponent match = MatchesService.get_instance().get_opponent_match( game.get_id(), get_current_user(), opponent.get_id() ) # If the match is found, add the player to it and start the match if it's full if match: match.add_player(get_current_user()) # If no match is found, create one that is assigned to the desired opponent, # but leave it in the waiting state else: match = Match(game, get_current_user()) match.add_player(opponent, should_start=False) # Otherwise, match with a random opponent else: # First, attempt to find a match that is looking for an opponent match = MatchesService.get_instance().get_random_match(game.get_id(), get_current_user()) # If the match is found, add the player to it and start the match if it's full if match: match.add_player(get_current_user()) # If no match is found, create one that is waiting for an opponent else: match = Match(game, get_current_user()) try: match.save() return render_view('matches/show', 201, match=match.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=GAME_NOT_FOUND_ERROR, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def delete(word_id): # Get the word word = WordsService.get_instance().get(word_id) # Verify the word creation inputs if word: try: word.update(**{'is_active': False}) return render_view('words/show', 200, word=word.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs={'id': word_id}) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': word_id})
def index(): # Get the input validator inputs = ListInputs(get_inputs()) # Verify the list inputs if inputs.validate(): matches = MatchesService.get_instance().get_list_by_game_for_player( inputs.game_id.data, get_current_user().get_id(), inputs.limit.data, inputs.offset.data ) return render_view('matches/index', 200, matches={match.get_id(): match.serialized for match in matches}) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def create(): # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the word creation inputs if inputs.validate_on_submit(): word = Word(inputs.lexeme_form.data, inputs.lexical_class.data) try: word.save() return render_view('words/show', 201, word=word.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def show(player_id): """ Request: { "auth_token": "auth_token" } Response [422] (unauthenticated): { "errors": { "UnauthorizedAccess": [ "Attempted to access data without an authenticated player" ] } } Response [422] (player with player_id doesn't exist): { "errors": { "PlayerNotFound": [ "Unable to find Player" ] }, "inputs": { "id": "player_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "username": "******", "email": "current value", "avatar_url": "current value", "is_active": "current value" } """ # Get the player player = PlayersService.get_instance().get(player_id) if player: return render_view('players/show', 200, player=player.serialized) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': player_id})
def show(match_id): # Get the current turn turn, errors = TurnsService.get_instance().get_player_turn(match_id, get_current_user().get_id()) if not errors: turn_player = TurnPlayersService.get_instance().get_for_turn_by_player( turn.get_id(), get_current_user().get_id() ) # Get the turn_definition_fillers for this turn turn_definition_fillers = TurnDefinitionFillersService.get_instance().get_list_by_turn(turn.get_id()) # If this player is the selector for this turn if turn_player.get_is_selector(): # TODO Test SQL for turn.get_match().get_game().get_definition_filler_count() vs turn.match.game.get_definition_filler_count() if turn.get_match().get_game().get_definition_filler_count() > len(turn_definition_fillers): pass return render_view('matches/show', 200, match=match.serialized) return render_view('422', 422, errors=errors, inputs={'id': match_id})
def show(game_id): """ Request: {} Response [422] (game with game_id doesn't exist): { "errors": { "GameNotFound": [ "Unable to find Game" ] }, "inputs": { "id": "game_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" } """ # Get the game game = GamesService.get_instance().get(game_id) if game: return render_view('games/show', 200, game=game.serialized) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': game_id})
def update(word_id): # Get the word word = WordsService.get_instance().get(word_id) # Verify the word creation inputs if word: # Get the input validator inputs = UpdateInputs(get_inputs()) combined_inputs = dict(inputs.serialized().items() + {'id': word_id}.items()) if inputs.validate_on_submit(): # If we're only marking the word as active or inactive, pass through to the update if inputs.is_active.data and \ not any([inputs.lexeme_form.data, inputs.lexical_class.data]): try: word.update(**get_mixed_dict_from_multidict(get_inputs(), inputs)) return render_view('words/show', 200, word=word.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) else: word.update(**{'is_active': False}) lexeme_form = inputs.lexeme_form.data if inputs.lexeme_form.data else word.get_lexeme_form() lexical_class = inputs.lexical_class.data if inputs.lexical_class.data else word.get_lexical_class() is_active = inputs.is_active.data if inputs.is_active.data else word.get_is_active() word = Word(lexeme_form, lexical_class) word.set_is_active(is_active) try: word.save() return render_view( 'words/show', 200, word=word.serialized ) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': word_id})
def create(): """ Request: { "username": "******", "password": "******", "confirm": "confirm", "email": "email", "avatar_url": "avatar_url" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "username": "******", "password": NULL, "confirm": NULL, "email": "value passed in. empty string if missing", "avatar_url": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed, such as username/email uniqueness" ] }, "inputs": { "username": "******", "password": NULL, "confirm": NULL, "email": "value passed in. empty string if missing", "avatar_url": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "username": "******", "email": "current value", "avatar_url": "current value", "is_active": "current value", "auth_token": "current value" } """ # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the player creation inputs if inputs.validate_on_submit(): avatar_url = inputs.avatar_url.data if inputs.avatar_url.data else None player = Player(inputs.username.data, inputs.password.data, inputs.email.data, avatar_url) try: player.save() player_data = dict(player.serialized.items() + {'auth_token': player.get_auth_token()}.items()) return render_view('players/show', 201, player=player_data) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.obfuscated()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.obfuscated())
def create(): """ Request: { "name": "name", "description": "description", "match_size": "match_size", "definition_filler_count": "definition_filler_count" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "match_size": "value passed in. empty string if missing", "definition_filler_count": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed, such as name uniqueness" ] }, "inputs": { "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "match_size": "value passed in. empty string if missing", "definition_filler_count": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" } """ # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the game creation inputs if inputs.validate_on_submit(): game = Game( inputs.name.data, inputs.description.data, inputs.match_size.data, inputs.definition_filler_count.data ) try: game.save() return render_view('games/show', 201, game=game.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def signin(): """ Request: { "username": "******", "password": "******" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "username": "******", "password": NULL } } Response [422] (login failure - username doesn't return user or password is invalid): { "errors": { "InvalidUsernamePassword": [ "Wrong username or password" ] }, "inputs": { "username": "******", "password": NULL } } Response [422] (signin failure - inactive user or save error): { "errors": { "SigninError": [ "Unable to sign in an inactive player" ] }, "inputs": { "username": "******", "password": NULL } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "username": "******", "email": "current value", "avatar_url": "current value", "is_active": "current value", "auth_token": "current value" } """ # Get the input validator inputs = SignInInputs(get_inputs()) # Verify the sign in inputs if inputs.validate_on_submit(): player = PlayersService.get_instance().get_from_username(inputs.username.data) if player and check_password_hash(player.get_password(), inputs.password.data): if player.login(): player_data = dict(player.serialized.items() + {'auth_token': player.get_auth_token()}.items()) return render_view('players/show', 200, player=player_data) else: return render_view('422', 422, errors=UNABLE_TO_SIGNIN_ERROR, inputs=inputs.obfuscated()) return render_view('422', 422, errors=INVALID_USERNAME_PASSWORD_ERROR, inputs=inputs.obfuscated()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.obfuscated())
def delete(player_id): """ Request: { "auth_token": "auth_token" } Response [422] (unauthenticated): { "errors": { "UnauthorizedAccess": [ "Attempted to access data without an authenticated player" ] } } Response [422] (player with player_id doesn't exist): { "errors": { "PlayerNotFound": [ "Unable to find Player" ] }, "inputs": { "id": "player_id" } } Response [422] (save failure - unable to set user as inactive): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "id": "player_id" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "username": "******", "email": "current value", "avatar_url": "current value", "is_active": "current value" } """ # Get the player player = PlayersService.get_instance().get(player_id) # Verify the player creation inputs if player: try: player.update(**{'is_active': False}) return render_view('players/show', 200, player=player.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs={'id': player_id}) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': player_id})
def create(): """ Request: { "word_id": "word_id", "definition": "definition", "filler_lexical_classes": "filler_lexical_classes" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing" } } Response [422] (word with word_id doesn't exist): { "errors": { "WordNotFound": [ "Unable to find the specified Word" ] }, "inputs": { "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing" } } Response [422] (save failure - invalid filler_lexical_classes length): { "errors": { "AttributeError": [ "There are {} filler lexical classes but {} fillers. These values must be the same." ] }, "inputs": { "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing" } } Response [422] (save failure - invalid filler_lexical_classes value): { "errors": { "AttributeError": [ "Cannot set the filler lexical classes to a value other than one of: {}" ] }, "inputs": { "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "word": "current value", "definition": "current value", "filler_lexical_classes": "current value", "is_active": "current value" } """ # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the definition_template creation inputs if inputs.validate_on_submit(): word = WordsService.get_instance().get(inputs.word_id.data) if word: try: definition_template = DefinitionTemplate(word, inputs.definition.data, inputs.filler_lexical_classes.data) definition_template.save() return render_view('definition_templates/show', 201, definition_template=definition_template.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=WORD_NOT_FOUND_ERROR, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def update(player_id): """ Request: { "auth_token": "auth_token", "username": "******", "password": "******", "email": "email", "avatar_url": "avatar_url", "is_active": "is_active" } Response [422] (unauthenticated): { "errors": { "UnauthorizedAccess": [ "Attempted to access data without an authenticated player" ] } } Response [422] (player with player_id doesn't exist): { "errors": { "PlayerNotFound": [ "Unable to find Player" ] }, "inputs": { "id": "player_id" } } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "id": "player_id", "username": "******", "password": NULL, "email": "value passed in. empty string if missing", "avatar_url": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed, such as username/email uniqueness" ] }, "inputs": { "id": "player_id", "username": "******", "password": NULL, "email": "value passed in. empty string if missing", "avatar_url": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "username": "******", "email": "current value", "avatar_url": "current value", "is_active": "current value" } """ # Get the player player = PlayersService.get_instance().get(player_id) # Verify the player creation inputs if player: # Get the input validator inputs = UpdateInputs(get_inputs()) combined_inputs = dict(inputs.obfuscated().items() + {'id': player_id}.items()) if inputs.validate_on_submit(): try: player.update(**get_mixed_dict_from_multidict(get_inputs(), inputs)) return render_view('players/show', 200, player=player.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': player_id})
def update(definition_template_id): """ Request: { "word_id": "word_id", "definition": "definition", "filler_lexical_classes": "filler_lexical_classes", "is_active": "is_active" } Response [422] (definition_template with definition_template_id doesn't exist): { "errors": { "DefinitionTemplateNotFound": [ "Unable to find DefinitionTemplate" ] }, "inputs": { "id": "definition_template_id" } } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "id": "definition_template_id", "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (word with word_id doesn't exist): { "errors": { "WordNotFound": [ "Unable to find the specified Word" ] }, "inputs": { "id": "definition_template_id", "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure - invalid filler_lexical_classes length): { "errors": { "AttributeError": [ "There are {} filler lexical classes but {} fillers. These values must be the same." ] }, "inputs": { "id": "definition_template_id", "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure - invalid filler_lexical_classes value): { "errors": { "AttributeError": [ "Cannot set the filler lexical classes to a value other than one of: {}" ] }, "inputs": { "id": "definition_template_id", "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "id": "definition_template_id", "word_id": "value passed in. empty string if missing", "definition": "value passed in. empty string if missing", "filler_lexical_classes": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "word": "current value", "definition": "current value", "filler_lexical_classes": "current value", "is_active": "current value" } """ # Get the definition_template definition_template = DefinitionTemplatesService.get_instance().get(definition_template_id) # Verify the definition_template creation inputs if definition_template: # Get the input validator inputs = UpdateInputs(get_inputs()) combined_inputs = dict(inputs.serialized().items() + {'id': definition_template_id}.items()) if inputs.validate_on_submit(): # If we're only marking the definition as active or inactive, pass through to the update if inputs.is_active.data and \ not any([inputs.word_id.data, inputs.definition.data, inputs.filler_lexical_classes.data]): try: definition_template.update(**get_mixed_dict_from_multidict(get_inputs(), inputs)) return render_view( 'definition_templates/show', 200, definition_template=definition_template.serialized ) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) # Otherwise, if we're trying to change the definition, mark the old # definition as inactive and create a new one with the new parameters else: definition_template.update(**{'is_active': False}) word = WordsService.get_instance().get(inputs.word_id.data) \ if inputs.word_id.data else definition_template.get_word() definition = inputs.definition.data if inputs.definition.data else definition_template.get_definition() filler_lexical_classes = inputs.filler_lexical_classes.data \ if inputs.filler_lexical_classes.data else definition_template.get_filler_lexical_classes() is_active = inputs.is_active.data if inputs.is_active.data else definition_template.get_is_active() if word: try: definition_template = DefinitionTemplate(word, definition, filler_lexical_classes) definition_template.set_is_active(is_active) definition_template.save() return render_view( 'definition_templates/show', 200, definition_template=definition_template.serialized ) except Exception as e: return render_view( '422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs ) return render_view('422', 422, errors=WORD_NOT_FOUND_ERROR, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': definition_template_id})
def create(): """ Request: { "definition_template_id": "definition_template_id", "filler": "filler", "is_dictionary": "is_dictionary" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing" } } Response [422] (definition_template with definition_template_id doesn't exist): { "errors": { "DefinitionTemplateNotFound": [ "Unable to find the specified DefinitionTemplate" ] }, "inputs": { "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing" } } Response [422] (save failure - invalid filler array length): { "errors": { "AttributeError": [ "There are {} filler but {} filler lexical classes. These values must be the same" ] }, "inputs": { "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "definition_template": "current value", "filler": "current value", "is_dictionary": "current value", "is_active": "current value" } """ # Get the input validator inputs = CreateInputs(get_inputs()) # Hack to deal with WTForms requirement that list inputs be validated against a list of choices filler = get_mixed_dict_from_multidict(get_inputs()).get('filler', []) inputs.filler.choices = [(fill, fill) for fill in filler] # Verify the definition_filler creation inputs if inputs.validate_on_submit(): definition_template = DefinitionTemplatesService.get_instance().get(inputs.definition_template_id.data) if definition_template: try: definition_filler = DefinitionFiller(definition_template, inputs.filler.data, inputs.is_dictionary.data) definition_filler.save() return render_view('definition_fillers/show', 201, definition_filler=definition_filler.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=DEFINITION_TEMPLATE_NOT_FOUND_ERROR, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def update(definition_filler_id): """ Request: { "definition_template_id": "definition_template_id", "filler": "filler", "is_dictionary": "is_dictionary", "is_active": "is_active" } Response [422] (definition_filler with definition_filler_id doesn't exist): { "errors": { "DefinitionFillerNotFound": [ "Unable to find DefinitionFiller" ] }, "inputs": { "id": "definition_filler_id" } } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "id": "definition_filler_id", "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (definition_template with definition_template_id doesn't exist): { "errors": { "DefinitionTemplateNotFound": [ "Unable to find the specified DefinitionTemplate" ] }, "inputs": { "id": "definition_filler_id", "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure - invalid filler array length): { "errors": { "AttributeError": [ "There are {} filler but {} filler lexical classes. These values must be the same" ] }, "inputs": { "id": "definition_filler_id", "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed" ] }, "inputs": { "id": "definition_filler_id", "definition_template_id": "value passed in. empty string if missing", "filler": "value passed in. empty string if missing", "is_dictionary": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "definition_template": "current value", "filler": "current value", "is_dictionary": "current value", "is_active": "current value" } """ # Get the definition_filler definition_filler = DefinitionFillersService.get_instance().get(definition_filler_id) # Verify the definition_filler creation inputs if definition_filler: # Get the input validator inputs = UpdateInputs(get_inputs()) combined_inputs = dict(inputs.serialized().items() + {'id': definition_filler_id}.items()) # Hack to deal with WTForms requirement that list inputs be validated against a list of choices filler = get_mixed_dict_from_multidict(get_inputs()).get('filler', []) inputs.filler.choices = [(fill, fill) for fill in filler] if inputs.validate_on_submit(): # If we're only marking the filler as active or inactive, pass through to the update if inputs.is_active.data and \ not any([inputs.definition_template_id.data, inputs.filler.data, inputs.is_dictionary.data]): try: definition_filler.update(**get_mixed_dict_from_multidict(get_inputs(), inputs)) return render_view( 'definition_fillers/show', 200, definition_filler=definition_filler.serialized ) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) # If we're trying to change the filler, mark the old filler # as inactive and create a new one with the new parameters else: definition_filler.update(**{'is_active': False}) definition_template = DefinitionTemplatesService.get_instance().get( inputs.definition_template_id.data ) if inputs.definition_template_id.data else definition_filler.get_definition_template() filler = inputs.filler.data if inputs.filler.data else definition_filler.get_filler() is_dictionary = inputs.is_dictionary.data \ if inputs.is_dictionary.data else definition_filler.get_is_dictionary() is_active = inputs.is_active.data if inputs.is_active.data else definition_filler.get_is_active() if definition_template: try: definition_filler = DefinitionFiller(definition_template, filler, is_dictionary) definition_filler.set_is_active(is_active) definition_filler.save() return render_view( 'definition_fillers/show', 200, definition_filler=definition_filler.serialized ) except Exception as e: return render_view( '422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs ) return render_view('422', 422, errors=DEFINITION_TEMPLATE_NOT_FOUND_ERROR, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': definition_filler_id})
def index(): """ Request: { "offset": "offset", "limit": "limit", "word_id": "word_id", "definition_template_id": "definition_template_id" } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "offset": "value passed in. empty string if missing", "limit": "value passed in. empty string if missing", "word_id": "value passed in. empty string if missing", "definition_template_id": "value passed in. empty string if missing" } } Response [200] (success): [ { "id": "current value", "date_created": "current value", "date_modified": "current value", "definition_template": "current value", "filler": "current value", "is_dictionary": "current value", "is_active": "current value" }, { "id": "current value", "date_created": "current value", "date_modified": "current value", "definition_template": "current value", "filler": "current value", "is_dictionary": "current value", "is_active": "current value" }, ... ] """ # Get the input validator inputs = ListInputs(get_inputs()) # Verify the list inputs if inputs.validate(): if inputs.word_id.data: definition_fillers = DefinitionFillersService.get_instance().get_list_by_word( inputs.word_id.data, inputs.limit.data, inputs.offset.data ) elif inputs.definition_template_id.data: definition_fillers = DefinitionFillersService.get_instance().get_list_by_definition_template( inputs.definition_template_id.data, inputs.limit.data, inputs.offset.data ) else: definition_fillers = DefinitionFillersService.get_instance().get_list(inputs.limit.data, inputs.offset.data) return render_view( 'definition_fillers/index', 200, definition_fillers={ definition_filler.get_id(): definition_filler.serialized for definition_filler in definition_fillers } ) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def update(game_id): """ Request: { "name": "name", "description": "description", "is_active": "is_active" } Response [422] (game with game_id doesn't exist): { "errors": { "GameNotFound": [ "Unable to find Game" ] }, "inputs": { "id": "game_id" } } Response [422] (invalid parameters): { "errors": { "name of parameter that failed validation": [ "Reason for validation failure" ], "name of another parameter that failed validation": [ "Reason for validation failure" ], }, "inputs": { "id": "game_id", "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (save failure): { "errors": { "IntegrityError": [ "Reason saving to the db failed, such as name uniqueness" ] }, "inputs": { "id": "game_id", "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [422] (illegal update request - protected attribute, like match_size, passed in as input): { "errors": { "AttributeError": [ "Update to Game is not supported with changes to match_size" ] }, "inputs": { "id": "game_id", "name": "value passed in. empty string if missing", "description": "value passed in. empty string if missing", "is_active": "value passed in. empty string if missing" } } Response [200] (success): { "id": "current value", "date_created": "current value", "date_modified": "current value", "name": "current value", "description": "current value", "match_size": "current value", "definition_filler_count": "current value", "is_active": "current value" } """ # Get the game game = GamesService.get_instance().get(game_id) # Verify the game creation inputs if game: # Get the input validator inputs = UpdateInputs(get_inputs()) combined_inputs = dict(inputs.serialized().items() + {'id': game_id}.items()) if inputs.validate_on_submit(): try: game.update(**get_mixed_dict_from_multidict(get_inputs(), inputs)) return render_view('games/show', 200, game=game.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': game_id})