# Path: aurweb/l10n.py
# def get_translator_for_request(request):
#     """
#     Determine the preferred language from a FastAPI request object and build a
#     translator function for it.
# 
#     Example:
#     ```python
#     _ = get_translator_for_request(request)
#     print(_("Hello"))
#     ```
#     """
#     lang = request.cookies.get("AURLANG")
#     if lang is None:
#         lang = aurweb.config.get("options", "default_lang")
#     translator = Translator()
# 
#     def translate(message):
#         return translator.translate(message, lang)
# 
#     return translate
# 
# Path: aurweb/schema.py
# def compile_tinyint_sqlite(type_, compiler, **kw):
# def compile_bigint_sqlite(type_, compiler, **kw):


import time
import uuid
import fastapi
import aurweb.config
import aurweb.db
from urllib.parse import urlencode
from authlib.integrations.starlette_client import OAuth, OAuthError
from fastapi import Depends, HTTPException
from fastapi.responses import RedirectResponse
from sqlalchemy.sql import select
from starlette.requests import Request
from aurweb.l10n import get_translator_for_request
from aurweb.schema import Bans, Sessions, Users
    """
    Create a new user session into the database. Return its SID.
    """
    if is_account_suspended(conn, user_id):
        _ = get_translator_for_request(request)
        raise HTTPException(status_code=403, detail=_('Account suspended'))
        # TODO This is a terrible message because it could imply the attempt at
        #      logging in just caused the suspension.

    sid = uuid.uuid4().hex
    conn.execute(Sessions.insert().values(
        UsersID=user_id,
        SessionID=sid,
        LastUpdateTS=time.time(),
    ))

    # Update user’s last login information.
    conn.execute(Users.update()
                      .where(Users.c.ID == user_id)
                      .values(LastLogin=int(time.time()),
                              LastLoginIPAddress=request.client.host))

    return sid


def is_ip_banned(conn, ip):
    """
    Check if an IP is banned. `ip` is a string and may be an IPv4 as well as an
    IPv6, depending on the server’s configuration.
    """

# Path: aurweb/l10n.py
# def get_translator_for_request(request):
#     """
#     Determine the preferred language from a FastAPI request object and build a
#     translator function for it.
# 
#     Example:
#     ```python
#     _ = get_translator_for_request(request)
#     print(_("Hello"))
#     ```
#     """
#     lang = request.cookies.get("AURLANG")
#     if lang is None:
#         lang = aurweb.config.get("options", "default_lang")
#     translator = Translator()
# 
#     def translate(message):
#         return translator.translate(message, lang)
# 
#     return translate
# 
# Path: aurweb/schema.py
# def compile_tinyint_sqlite(type_, compiler, **kw):
# def compile_bigint_sqlite(type_, compiler, **kw):


# Path: aurweb/routers/sso.py
import time
import uuid
import fastapi
import aurweb.config
import aurweb.db
from urllib.parse import urlencode
from authlib.integrations.starlette_client import OAuth, OAuthError
from fastapi import Depends, HTTPException
from fastapi.responses import RedirectResponse
from sqlalchemy.sql import select
from starlette.requests import Request
from aurweb.l10n import get_translator_for_request
from aurweb.schema import Bans, Sessions, Users

    """
    Create a new user session into the database. Return its SID.
    """
    if is_account_suspended(conn, user_id):
        _ = get_translator_for_request(request)
        raise HTTPException(status_code=403, detail=_('Account suspended'))
        # TODO This is a terrible message because it could imply the attempt at
        #      logging in just caused the suspension.

    sid = uuid.uuid4().hex
    conn.execute(Sessions.insert().values(
        UsersID=user_id,
        SessionID=sid,
        LastUpdateTS=time.time(),
    ))

    # Update user’s last login information.
    conn.execute(Users.update()
                      .where(Users.c.ID == user_id)
                      .values(LastLogin=int(time.time()),
                              LastLoginIPAddress=request.client.host))

    return sid


def is_ip_banned(conn, ip):
    """
    Check if an IP is banned. `ip` is a string and may be an IPv4 as well as an
    IPv6, depending on the server’s configuration.
    """

    result = conn.execute(Bans.select().where(Bans.c.IPAddress == ip))