def add_mod(request: Request, game: dict[str, Any], version: dict[str, Any]): if request.POST: if not request.POST.get('id', True): DB.execute('INSERT INTO Mods (game, name, description, created, image) VALUES (?, ?, ?, ?, ?)', (game['id'], request.POST['name'], request.POST['description'], request.POST['created'], list(request.FILES['icon'].values())[0].read())) request.POST['id'] = DB.execute('SELECT last_insert_rowid() as row').fetchone()['row'] DB.execute('INSERT INTO ModVersions (mod, version, game_version, released) VALUES (?, ?, ?, ?)', (request.POST['id'], request.POST['version'], version['id'], request.POST['released'])) DB.commit() return '', 307, {'Location': f'/{game["name"]}/mods/{request.POST["name"]}/{request.POST["version"]}/'} return render(request, 'html/add_mod.html', {'game': game, 'version': version, 'mods': DB.execute('SELECT name, created, id FROM Mods WHERE game=? AND id NOT IN (SELECT mod FROM ModVersions WHERE game_version=?) ORDER BY name', (game['id'], version['id'])).fetchall()})
def add_item(request: Request, game: dict[str, Any], version: dict[str, Any]): if request.POST: if not request.POST.get('id', True): DB.execute('INSERT INTO GameItems (name, image) VALUES (?, ?)', (request.POST['name'], list(request.FILES['icon'].values())[0].read())) request.POST['id'] = DB.execute('SELECT last_insert_rowid() as row').fetchone()['row'] DB.execute('INSERT INTO GameVersionItemMap VALUES (?, ?)', (version['id'], request.POST['id'])) DB.commit() return '', 307, {'Location': f'/{game["name"]}/{version["version"]}/items/{request.POST["name"]}/'} return render(request, 'html/add_item.html', {'game': game, 'version': version, 'items': DB.execute('SELECT name, GameItems.id FROM GameItems INNER JOIN GameVersionItemMap GVIM ON GameItems.id = GVIM.item INNER JOIN GameVersions GV ON GV.id = GVIM.version WHERE game=? AND GameItems.id NOT IN (SELECT item FROM GameVersionItemMap WHERE version=?) ORDER BY name', (game['id'], version['id'])).fetchall()})
def add_game(request: Request): if request.POST: DB.execute( 'INSERT INTO Games (name, released, image) VALUES (?, ?, ?)', (request.POST['name'], request.POST['released'], list(request.FILES['icon'].values())[0].read())) DB.commit() return '', 307, {'Location': f'/{request.POST["name"]}/'} return render(request, 'html/add_game.html')
def add_version(request: Request, game: dict[str, Any]): if request.POST: DB.execute( 'INSERT INTO GameVersions (game, version, released) VALUES (?, ?, ?)', (game['id'], request.POST['version'], request.POST['released'])) DB.commit() return '', 307, { 'Location': f'/{game["name"]}/{request.POST["version"]}/' } return render(request, 'html/add_version.html', {'game': game})
def versions(request: Request, game: dict[str, Any]): return render( request, 'html/game.html', { 'game': game, 'versions': DB.execute( 'SELECT version, released FROM GameVersions WHERE game=? ORDER BY released DESC', (game['id'], )).fetchall(), 'mods': DB.execute( 'SELECT Mods.* FROM Mods INNER JOIN ModVersions ON Mods.id=ModVersions.mod WHERE game_version IN (SELECT id FROM GameVersions WHERE game=?)', (game['id'], )).fetchall() })
def game_version(request: Request, game: dict[str, Any], version: dict[str, Any]): return render(request, 'html/game_version.html', {'game': game, 'version': version, 'items': DB.execute('SELECT GameItems.* FROM GameItems INNER JOIN GameVersionItemMap ON GameItems.id=GameVersionItemMap.item WHERE version=? ORDER BY name', (version['id'],)).fetchall(), 'mods': DB.execute('SELECT Mods.* FROM Mods INNER JOIN ModVersions ON Mods.id=ModVersions.mod WHERE game_version=? ORDER BY name', (version['id'],)).fetchall()})
def render_example(request: Request): return render( request, '../README.md', {'test': request.GET.get('test', '')} ) # replace ~~test~~ in the readme file with what is in the get request for the variable test
from http import HTTPStatus from litespeed import add_websocket, App, register_error_page, render, route, serve, start_with_args, WebServer from litespeed.error import ResponseError from litespeed.utils import Request """Any function with a route decorator must follow one of the following return patterns: render(filename, dict) static(filename) str or bytes (body) str or bytes (body), int (status code) str or bytes (body), int (status code), dict (headers)""" @route() # uses method name to generate url: /test/ def test(request): return 'Testing' # return text only with default 200 status @route('example2') # specify url directly: /example2/ def other(request: Request): return 'Other', None, { 'Testing': 'Header' } # return text and header values with default 200 status @route( 'other/txt', methods=['post'] ) # specify url and lock to certain methods: anything but post will return 405 def another(request: Request): return 'Txt', 204 # return text and status 204 (no content)
def index(request: Request): return render(request, 'html/index.html', {'games': DB.execute('SELECT name, released, id FROM Games ORDER BY name').fetchall()})