示例#1
0
async def email_subscribe(request):
    try:
        email = request.form['email'][0]
    except KeyError:
        return add_message(request, 'error', 'Enter an email in the field.', '/schoolweek')

    async with open_db_connection(request.app) as conn:
        existing = await conn.fetchrow('SELECT * FROM mailing_list WHERE email = $1', email)
        if existing:
            return add_message(request, 'error', 'Email already subscribed.', '/schoolweek')

    msg = EmailMessage()
    msg['Subject'] = 'Thank you for subscribing to GCHS Daily Updates!'
    msg['From'] = request.app.config.CUSTOM_EMAIL
    msg['To'] = email
    body = MIMEText(
        f"If this wasn't you, click <a href=\"http{'s' if not request.app.config.DEV else ''}://{request.app.config.DOMAIN}"
        f"/schoolweek/unsubscribe/{email}\">here</a> to unsubscribe.", 'html')
    msg.set_content(body)

    with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
        smtp.login(request.app.config.NOREPLY_EMAIL, request.app.config.EMAIL_APP_PASSWORD)

        smtp.send_message(msg)

    async with open_db_connection(request.app) as conn:
        await conn.execute('INSERT INTO mailing_list(email) VALUES ($1)', email)

    return add_message(request, 'success', 'Your email has been added to the mailing list.', '/schoolweek')
示例#2
0
async def existing_pastebin(request, code):
    async with open_db_connection(request.app) as conn:
        res = await conn.fetchrow('SELECT * FROM pastebin WHERE code = $1', code)
    if not res:
        abort(404, message=f'Requested URL {request.path} not found')
    text = disable_xss(res['text'])
    return await render_template('saved_pastebin', request, title="Pastebin - Saved", description="Saved Pastebin", code=text)
示例#3
0
async def callback(request):
    app = request.app
    code = request.args.get('code')
    access_token = await app.oauth.get_access_token(code)
    user = await app.oauth.get_user_json(access_token)
    if user.get('message'):
        return await render_template('unauthorized',
                                     request,
                                     description='Discord Oauth Unauthorized.')

    if user.get('avatar'):
        avatar = 'https://cdn.discordapp.com/avatars/{}/{}.png'.format(
            user['id'], user['avatar'])
    else:  # in case of default avatar users
        avatar = 'https://cdn.discordapp.com/embed/avatars/{}.png'.format(
            user['discriminator'] % 5)

    async with open_db_connection(request.app) as conn:
        await conn.executemany(
            '''INSERT INTO users(id, name, discrim, avatar) VALUES ($1, $2, $3, $4)
            ON CONFLICT (id) DO UPDATE SET id=$1, name=$2, discrim=$3, avatar=$4''',
            [(user['id'], user['username'], user['discriminator'], avatar),
             (user['id'], user['username'], user['discriminator'], avatar)])

    request.ctx.session['logged_in'] = True
    request.ctx.session['id'] = user['id']

    return response.redirect('/dashboard')
示例#4
0
async def create_url(request):
    chars = string.ascii_letters + string.digits
    code = ''.join(random.choice(chars) for i in range(8))
    try:
        url = request.form['url'][0]
    except KeyError:
        return add_message(request, 'error', 'Enter a URL to redirect to.',
                           '/urlshortener')
    account = request.ctx.session.get('id', 'no_account')

    async with open_db_connection(request.app) as conn:
        if request.form.get('code'):
            code = request.form['code'][0]
            existing = await conn.fetchrow(
                'SELECT * FROM urls WHERE code = $1', code)
            if existing:
                return add_message(
                    request, 'error',
                    'That code is already taken. Try another one.',
                    '/urlshortener')
        await conn.execute(
            'INSERT INTO urls(user_id, code, url) VALUES ($1, $2, $3)',
            account, code, url)
    return add_message(
        request, 'success',
        f"Shortened URL created at <a href=\"http{'s' if not request.app.config.DEV else ''}://{request.app.config.DOMAIN}/{code}\">"
        f"http{'s' if not request.app.config.DEV else ''}://{request.app.config.DOMAIN}/{code}</a>",
        '/urlshortener')
示例#5
0
async def create_pastebin(request):
    chars = string.ascii_letters + string.digits
    code = ''.join(random.choice(chars) for i in range(8))
    try:
        text = request.form['text'][0]
    except KeyError:
        return add_message(request, 'error', 'Paste some code in to save.', '/pastebin')
    account = request.ctx.session.get('id', 'no_account')
    async with open_db_connection(request.app) as conn:
        await conn.execute('INSERT INTO pastebin(user_id, code, text) VALUES ($1, $2, $3)', account, code, text)
    return response.redirect(f'/pastebin/{code}')
示例#6
0
async def dashboard_home(request):
    async with open_db_connection(request.app) as conn:
        urls = await conn.fetch('SELECT * FROM urls WHERE user_id = $1', request.ctx.session['id'])
        pastes = await conn.fetch('SELECT * FROM pastebin WHERE user_id = $1', request.ctx.session['id'])
    return await render_template(
        template='dashboard',
        request=request,
        title="Dashboard",
        description='Dashboard for your account.',
        urls=urls,
        pastes=pastes
    )
示例#7
0
async def email_unsubscribe(request, email):
    async with open_db_connection(request.app) as conn:
        await conn.execute('DELETE FROM mailing_list WHERE email = $1', email)
    return add_message(request, 'success', 'Your email has been removed from mailing list.', '/schoolweek')
示例#8
0
async def existing_code(request, code):
    async with open_db_connection(request.app) as conn:
        res = await conn.fetchrow('SELECT * FROM urls WHERE code = $1', code)
    if not res:
        abort(404, message=f'Requested URL {request.path} not found')
    return response.redirect(res['url'])