Beispiel #1
0
def route_api_amiibo_series():
    args = request.args

    if 'key' in args:
        try:
            key = AmiiboSeriesHex(args['key'].strip())
        except ValueError:
            abort(400)
        result = amiibo_manager.amiibo_series.get(key)
    else:
        filters = {}
        if 'name' in args:
            filters['name'] = args['name'].strip()

        result = amiibo_manager.amiibo_series.filter(**filters)
        if 'sort' in args:
            values = {
                'key': 'id',
                'name': 'name',
            }
            result = result.sort(*[
                values[value] for value in args['sort'].split(',')
                if value in values
            ])

    if not result:
        abort(404)

    respond = jsonify({'amiibo': result})
    return respond
Beispiel #2
0
def route_api_amiibo():
    args = request.args

    if 'id' in args:
        try:
            id_ = AmiiboHex(args['id'].strip())
        except ValueError:
            abort(400)
        result = amiibo_manager.amiibos.get(id_)
    else:
        filters = {}
        if 'head' in args:
            try:
                filters['head'] = Hex(args['head'].strip())
            except ValueError:
                abort(400)

        if 'tail' in args:
            try:
                filters['tail'] = Hex(args['tail'].strip())
            except ValueError:
                abort(400)

        if 'name' in args:
            filters['name'] = args['name'].strip()

        if 'gameseries' in args:
            game_series = args['gameseries'].strip()
            if game_series.startswith('0x'):
                try:
                    filters['game_series_id'] = GameSeriesHex(game_series)
                except ValueError:
                    abort(400)
            else:
                filters['game_series_name'] = game_series

        if 'character' in args:
            character = args['character'].strip()
            if character.startswith('0x'):
                try:
                    filters['character_id'] = CharacterHex(character)
                except ValueError:
                    abort(400)
            else:
                filters['character_name'] = character

        if 'variant' in args:
            try:
                filters['variant_id'] = VariantHex(args['variant'].strip())
            except ValueError:
                abort(400)

        if 'type' in args:
            amiibo_type = args['type'].strip()
            if amiibo_type.startswith('0x'):
                try:
                    filters['amiibo_type_id'] = AmiiboTypeHex(amiibo_type)
                except ValueError:
                    abort(400)
            else:
                filters['amiibo_type_name'] = amiibo_type

        if 'amiibo_model' in args:
            filters['amiibo_model_id'] = AmiiboModelHex(
                args['amiibo_model'].strip())

        if 'amiiboSeries' in args:
            amiibo_series = args['amiiboSeries'].strip()
            if amiibo_series.startswith('0x'):
                try:
                    filters['amiibo_series_id'] = AmiiboSeriesHex(
                        amiibo_series)
                except ValueError:
                    abort(400)
            else:
                filters['amiibo_series_name'] = amiibo_series

        result = amiibo_manager.amiibos.filter(**filters)
        if 'sort' in args:
            values = {
                'id': 'id',
                'head': 'head',
                'tail': 'tail',
                'name': 'name',
                'gameseries': 'gameseries',
                'gameseries_id': 'game_series_id',
                'gameseries_name': 'game_series_name',
                'character': 'character_name',
                'character_id': 'character_id',
                'character_name': 'character_name',
                'variant': 'variant_id',
                'variant_id': 'variant_id',
                'type': 'amiibo_type_id',
                'type_id': 'amiibo_type_id',
                'type_name': 'amiibo_type_name',
                'amiibo_model': 'amiibo_model_id',
                'amiibo_model_id': 'amiibo_model_id',
                'series': 'amiibo_series_name',
                'series_id': 'amiibo_series_id',
                'series_name': 'amiibo_series_name',
                'release_na': 'release_na',
                'release_jp': 'release_jp',
                'release_eu': 'release_eu',
                'release_au': 'release_au',
            }
            result = result.sort(*[
                values[value] for value in args['sort'].split(',')
                if value in values
            ])

    if not result:
        abort(404)

    respond = jsonify({'amiibo': result})
    return respond