async def signin(request): if request.method == 'POST': if signinValidate(request.form): resp = response.redirect('/base') resp.cookies['username'] = request.form.get('username') return resp return render_template('template_signin', request, getHeaders) elif request.method == 'GET': if request.cookies.get('username'): return response.redirect('/base') return render_template('template_signin', request, getHeaders)
async def get_server_request(request: Request, user_info: UserInfo, server_id: int): user_info = user_info.__dict__ user_icon = f'https://cdn.discordapp.com/avatars/{user_info["id"]}/{user_info["avatar"]}.png' username = user_info['username'] _, guilds = await fetch_user_guilds( request=request, provider="discord", oauth_endpoint_path="https://discord.com/api/users/@me/guilds") auth = await check_auth(guilds, server_id) if not auth: return response.html( status=403, body=UNAUTHORIZED.format(url=f"/server/{server_id}")) guilds = await check_perms(guilds, user_info['id']) guild = await get_guild(guilds, server_id) context = { 'logged_in': True, 'user': username, 'icon': user_icon, 'guild': guild, } resp = jinja2_sanic.render_template("templates.dashboard_server", request, context) resp.headers['Access-Control-Allow-Origin'] = '*' return resp
async def dashboard_home(request: Request, user_info: UserInfo): user_info = user_info.__dict__ user_icon = f'https://cdn.discordapp.com/avatars/{user_info["id"]}/{user_info["avatar"]}.png' username = user_info['username'] _, guilds = await fetch_user_guilds( request, provider="discord", oauth_endpoint_path="https://discord.com/api/users/@me/guilds") guilds = await check_perms(guilds, user_info['id']) guild_data = [] for guild in guilds[:5]: guild_data.append({ 'id': guild['id'], 'name': guild['name'], 'url': f'https://cdn.discordapp.com/icons/{guild["id"]}/{guild["icon"]}.webp?size=256', 'href': f"'../server/{guild['id']}'" }) context = { 'logged_in': True, 'user': username, 'icon': user_icon, 'recent_guilds': guild_data[:2], 'all_guilds': guild_data, } resp = jinja2_sanic.render_template("templates.dashboard_home", request, context) resp.headers['Access-Control-Allow-Origin'] = '*' return resp
async def pic_profile(ctx, url): # Check image exists # This will be used to get meta async with NGXImage(blure.url.to_id(url)): return render_template('profile.html.j2', ctx.r, dict(url=url, tags=[]))
async def get(request): if not request['session'].get('foo'): request['session']['foo'] = 0 request['session']['foo'] += 1 return jinja2_sanic.render_template("index.html", request, { 'name': 'spam', 'foo': request['session']['foo'] })
async def pre_install(request): """This route renders the installation page with 'Add to Slack' button.""" # Since we've set the client ID and scope on our Bot object, we can change # them more easily while we're developing our app. client_id = pyBot.oauth["client_id"] scope = pyBot.oauth["scope"] # Our template is using the Jinja templating language to dynamically pass # our client id and scope return jinja2_sanic.render_template("install.html", request, dict(client_id=client_id, scope=scope))
async def home(request: Request): user_token = request.ctx.__dict__['session'].get('token') username, user_icon = "Sign in", "../static//images/Discord-Logo-White.svg" if user_token: user = await get_info(request=request) user_icon = f'https://cdn.discordapp.com/avatars/{user["id"]}/{user["avatar"]}.png' username = user['username'] context = {'user': username, 'icon': user_icon} resp = jinja2_sanic.render_template("templates.home", request, context) resp.headers['Access-Control-Allow-Origin'] = '*' return resp
async def thanks(request): """ This route is called by Slack after the user installs our app. It will exchange the temporary authorization code Slack sends for an OAuth token which we'll save on the bot object to use later. To let the user know what's happened it will also render a thank you page. """ # Let's grab that temporary authorization code Slack's sent us from # the request's parameters. code_arg = request.args.get('code') # The bot's auth method to handles exchanging the code for an OAuth token pyBot.auth(code_arg) return jinja2_sanic.render_template("thanks.html", request, {})
async def table(request, table_id): table = PokerGame[table_id] user = request.cookies.get('username') if ( user and len(table.players) < table.seats and table.players.getPlayerByAttr('name', user) is None ): account = PokerGameDatabase.accountFromUsername(user) if (account and account.money >= table.minbuyin): return render_template( 'template_table', request, {'username': user, 'table_id': table_id, **getHeaders} ) return response.redirect('/base')
async def pokertables(request): user = request.cookies.get('username') tables = [{ "id": table.id, "name": table.name, "seats": table.seats, "seats_taken": table.seats - table.seats_free, "buyin": table.buyin, "minbuyin": table.minbuyin, "small_blind": table.small_blind, "big_blind": table.big_blind, "url": app.url_for("table", table_id=table.id) } for table in PokerGame] return render_template( 'template_pokertables', request, {'username': user, 'tables': tables, **getHeaders} )
async def database(request): if request.method == 'POST': client_id = request.json.get('id') client_data = request.json.get('data') if client_id == ClientDbCode.GETTABLE: columns, rows = DatabaseBrowser.readTable( table_enum[client_data['table']] ) elif client_id == ClientDbCode.GETPLAYER: columns, rows = DatabaseBrowser.getPlayer( client_data['account_id'] ) return response.json({ 'rows' : rows, 'columns': columns }) elif request.method == 'GET': user = request.cookies.get('username') return render_template( 'template_database', request, {'username': user, **getHeaders} )
def render_template(template_name, request, **kwargs): return jinja2_sanic.render_template(template_name, request, context=kwargs)
async def endpoints(request): context = {} resp = jinja2_sanic.render_template("templates.endpoints", request, context) resp.headers['Access-Control-Allow-Origin'] = '*' return resp
def render_template(template_name, request, **kwargs): return jinja2_sanic.render_template(template_name, request, context=kwargs)
async def index(ctx): records = await ctx.pg.fetch('SELECT id FROM pics') picurls = [ctx.app.url.to_url(rec['id']) for rec in records] return render_template('index.html.j2', ctx.r, dict(picurls=picurls))
async def not_found(req, exc): return render_template('404.html.j2', req, dict())