render whole pages on refresh and partial on hx rq
This commit is contained in:
		
							parent
							
								
									464683e9ce
								
							
						
					
					
						commit
						eff7bfa843
					
				
					 1 changed files with 34 additions and 4 deletions
				
			
		|  | @ -1,12 +1,16 @@ | ||||||
|  | import functools | ||||||
| import urllib.parse | import urllib.parse | ||||||
|  | 
 | ||||||
| import transmission_rpc | import transmission_rpc | ||||||
| from fastapi import Depends, FastAPI, Request | from fastapi import Depends, FastAPI, Request | ||||||
| from fastapi.responses import HTMLResponse | from fastapi.responses import HTMLResponse, RedirectResponse | ||||||
|  | from fastapi.staticfiles import StaticFiles | ||||||
| from fastapi.templating import Jinja2Templates | from fastapi.templating import Jinja2Templates | ||||||
| 
 | 
 | ||||||
| from torrent_downloader.client import TorrentDownloader | from torrent_downloader.client import TorrentDownloader | ||||||
| 
 | 
 | ||||||
| app = FastAPI() | app = FastAPI() | ||||||
|  | app.mount("/static", StaticFiles(directory="static"), name="static") | ||||||
| templates = Jinja2Templates(directory="templates") | templates = Jinja2Templates(directory="templates") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -18,12 +22,36 @@ def get_active_torrents(downloader=get_downloader()) -> list[transmission_rpc.To | ||||||
|     return [t for t in downloader.get_active_torrents() if t.format_eta() != "not available"] |     return [t for t in downloader.get_active_torrents() if t.format_eta() != "not available"] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @app.get("/", response_class=HTMLResponse) | def hx(func): | ||||||
| async def index(request: Request): |     @functools.wraps(func) | ||||||
|     return templates.TemplateResponse(request=request, name="index.html") |     async def hx_or_http(request: Request, *args, **kwargs): | ||||||
|  |         response = await func(request, *args, **kwargs) | ||||||
|  | 
 | ||||||
|  |         if "HX-Request" not in request.headers: | ||||||
|  |             response = templates.TemplateResponse( | ||||||
|  |                 request, | ||||||
|  |                 name="index.html", | ||||||
|  |                 context={"include_template": response.template.name}, | ||||||
|  |                 status_code=200, | ||||||
|  |             ) | ||||||
|  |         return response | ||||||
|  | 
 | ||||||
|  |     return hx_or_http | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @app.get("/") | ||||||
|  | async def index(_: Request): | ||||||
|  |     return RedirectResponse("/search") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @app.get("/search", response_class=HTMLResponse) | ||||||
|  | @hx | ||||||
|  | async def search_for_torrents(request: Request): | ||||||
|  |     return templates.TemplateResponse(request=request, name="search.html") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @app.get("/list", response_class=HTMLResponse) | @app.get("/list", response_class=HTMLResponse) | ||||||
|  | @hx | ||||||
| async def list_torrents(request: Request, query: str, downloader=Depends(get_downloader)): | async def list_torrents(request: Request, query: str, downloader=Depends(get_downloader)): | ||||||
|     torrents = downloader.search(query) |     torrents = downloader.search(query) | ||||||
|     return templates.TemplateResponse( |     return templates.TemplateResponse( | ||||||
|  | @ -32,6 +60,7 @@ async def list_torrents(request: Request, query: str, downloader=Depends(get_dow | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @app.get("/download", response_class=HTMLResponse) | @app.get("/download", response_class=HTMLResponse) | ||||||
|  | @hx | ||||||
| async def download(request: Request, downloader=Depends(get_downloader)): | async def download(request: Request, downloader=Depends(get_downloader)): | ||||||
|     magnet = urllib.parse.unquote(str(request.query_params)) |     magnet = urllib.parse.unquote(str(request.query_params)) | ||||||
|     downloader.download_magnet(magnet) |     downloader.download_magnet(magnet) | ||||||
|  | @ -42,6 +71,7 @@ async def download(request: Request, downloader=Depends(get_downloader)): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @app.get("/active", response_class=HTMLResponse) | @app.get("/active", response_class=HTMLResponse) | ||||||
|  | @hx | ||||||
| async def active(request: Request, active=Depends(get_active_torrents)): | async def active(request: Request, active=Depends(get_active_torrents)): | ||||||
|     return templates.TemplateResponse( |     return templates.TemplateResponse( | ||||||
|         request=request, name="active_torrents.html", context={"torrents": active} |         request=request, name="active_torrents.html", context={"torrents": active} | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue