예제 #1
0
def sanitize_filters(filters):
    '''Sanitize the filters to prune bad data'''
    s_filters = {}
    if "min_players" in filters:
        try:
            s_filters["min_players"] = int(filters["min_players"])
        except (TypeError, ValueError, KeyError):
            pass

    if "max_players" in filters:
        max_players = int(filters["max_players"])
        if "min_players" in s_filters:
            if max_players >= s_filters["min_players"]:
                s_filters["max_players"] = max_players
        else:
            s_filters["max_players"] = max_players


    if "after" in filters:
        try:
            s_filters["after"] = datetime.datetime.strptime(
                filters["after"], "%Y-%m-%d")
        except (TypeError, ValueError, KeyError):
            pass

    if "before" in filters:
        try:
            before = datetime.datetime.strptime(filters["before"], "%Y-%m-%d")

            if "after" in s_filters:
                if s_filters["after"] <= before:
                    s_filters["before"] = before
            else:
                s_filters["before"] = before

        except (TypeError, ValueError, KeyError):
            pass

    if "gametype" in filters:
        try:
            filters = [f for f in filters["gametype"] if f in GAMETYPES]
            if len(filters) > 0:
                s_filters["gametype"] = filters

        except TypeError:
            pass

    if "format" in filters:
        try:
            filters = [f for f in filters["format"] if f in FORMATS]
            if len(filters) > 0:
                s_filters["format"] = filters

        except TypeError:
            pass


    if "tags" in filters:
        try:
            tags = [f.encode("utf-8") for f in filters["tags"]]
            if len(tags) > 0:
                s_filters["tags"] = set(tags)

        except TypeError:
            pass

    if "players" in filters:
        try:
            ids = [i["id"] for i in current.session.groupplayers]
            players = [int(p) for p in filters["players"] if int(p) in ids]
            if len(players) > 0:
                s_filters["players"] = set(players)

        except (TypeError, ValueError):
            pass

    if "decks" in filters:
        try:
            ids = [i["id"] for i in deckmgmt.list_decks(player_only=False)]
            decks = [int(d) for d in filters["decks"] if int(d) in ids]
            if len(decks) > 0:
                s_filters["decks"] = set(decks)

        except (TypeError, ValueError):
            pass

    return s_filters
예제 #2
0
def get_deck_names():
    playermgmt.ensure_session_vars(auth)
    response.generic_patterns = ['json']

    return {'decks': [deck["name"] for deck in 
                      deckmgmt.list_decks(player_only=False)]}
예제 #3
0
def stats():
    playermgmt.ensure_session_vars(auth)
    return {"decks": deckmgmt.list_decks(player_only=False)}