Exemplo n.º 1
0
async def user_settings(request: Request):
    """Handles the users' individual settings pages"""

    # See if they're logged in
    session = await aiohttp_session.get_session(request)
    if not session.get('user_id'):
        return HTTPFound(location='/')

    # Get the colours they're using
    db = await request.app['database'].get_connection()
    if len(request.query) > 0:
        colours_raw = {
            'edge': request.query.get('edge'),
            'node': request.query.get('node'),
            'font': request.query.get('font'),
            'highlighted_font': request.query.get('highlighted_font'),
            'highlighted_node': request.query.get('highlighted_node'),
            'background': request.query.get('background'),
            'direction': request.query.get('direction', 'TB'),
        }
        colours = {}
        for i, o in colours_raw.items():
            if o == None:
                o = 'transparent'
            colours[i] = o
    else:
        data = await db('SELECT * FROM customisation WHERE user_id=$1',
                        session['user_id'])
        try:
            colours = utils.CustomisedTreeUser(**data[0]).unquoted_hex
        except (IndexError, TypeError):
            colours = utils.CustomisedTreeUser.get_default_unquoted_hex()

    # Make a URL for the preview
    tree_preview_url = '/tree_preview?' + '&'.join([
        f'{i}={o.strip("#")}' if i != 'direction' else f'{i}={o}'
        for i, o in colours.items()
    ])

    # Get their blocked users
    blocked_users_db = await db(
        "SELECT blocked_user_id FROM blocked_user WHERE user_id=$1",
        session['user_id'])
    blocked_users = {
        i['blocked_user_id']: await
        request.app['bot'].get_name(i['blocked_user_id'])
        for i in blocked_users_db
    }

    # Give all the data to the page
    await db.disconnect()
    return {
        'hex_strings': colours,
        'tree_preview_url': tree_preview_url,
        'blocked_users': blocked_users,
    }
Exemplo n.º 2
0
    async def randomisetreecolours(self, ctx: utils.Context,
                                   user: utils.converters.UserID):
        """Adds a redirect to the database"""

        ctu = utils.CustomisedTreeUser(
            user,
            edge=random.randint(0, 0xffffff),
            node=random.randint(0, 0xffffff),
            font=random.randint(0, 0xffffff),
            highlighted_font=random.randint(0, 0xffffff),
            highlighted_node=random.randint(0, 0xffffff),
            background=random.randint(0, 0xffffff),
        )
        async with self.bot.database() as db:
            await ctu.save(db)
        await ctx.send("Done.")
Exemplo n.º 3
0
async def user_settings(request: Request):
    """
    Handles the individual settings page for the user - shows them their
    tree settings as well as blocked users.
    """

    # See if they're logged in
    session = await aiohttp_session.get_session(request)

    # Get the colours they're using
    db = await request.app['database'].get_connection()
    data = await db(
        """SELECT * FROM customisation WHERE user_id=$1""",
        session['user_id'],
    )
    try:
        colours = botutils.CustomisedTreeUser(**data[0]).unquoted_hex
    except (IndexError, TypeError):
        colours = botutils.CustomisedTreeUser.get_default_unquoted_hex()

    # Make a URL for the preview
    colours = {i: o.strip("#") for i, o in colours.items()}
    tree_preview_url = f"/tree_preview?{urlencode(colours)}"

    # Get their blocked users
    blocked_users_db = await db(
        """SELECT blocked_user_id FROM blocked_user WHERE user_id=$1""",
        session['user_id'],
    )
    bot = request.app['bots']['bot']
    blocked_users = {
        i['blocked_user_id']: await
        botutils.DiscordNameManager.fetch_name_by_id(bot, i['blocked_user_id'])
        for i in blocked_users_db
    }

    # Give all the data to the page
    await db.disconnect()
    return {
        "hex_strings": colours,
        "tree_preview_url": tree_preview_url,
        "blocked_users": blocked_users,
    }