From 24e7778fa585b31b7aa01747a82ed88ed9495188 Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Wed, 27 Mar 2024 16:22:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E2=80=94=20Use=20a=20POST=20req?= =?UTF-8?q?uest=20for=20automatic=20refresh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- argos/server/routes/views.py | 27 ++++++++++++++++++++++----- argos/server/templates/base.html | 4 ++-- argos/server/templates/index.html | 12 ++++++------ pyproject.toml | 1 + 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/argos/server/routes/views.py b/argos/server/routes/views.py index 88343ea..fd5fc32 100644 --- a/argos/server/routes/views.py +++ b/argos/server/routes/views.py @@ -1,9 +1,11 @@ """Web interface for humans""" from collections import defaultdict from functools import cmp_to_key +from typing import Annotated from urllib.parse import urlparse -from fastapi import APIRouter, Depends, Request +from fastapi import APIRouter, Cookie, Depends, Form, Request, status +from fastapi.responses import RedirectResponse from fastapi.templating import Jinja2Templates from sqlalchemy import func from sqlalchemy.orm import Session @@ -23,8 +25,8 @@ SEVERITY_LEVELS = {"ok": 1, "warning": 2, "critical": 3, "unknown": 4} async def get_severity_counts_view( request: Request, db: Session = Depends(get_db), - refresh: bool = False, - delay: int = 15, + auto_refresh_enabled: Annotated[bool, Cookie()] = False, + auto_refresh_seconds: Annotated[int, Cookie()] = 15, ): """Shows the number of results per severity""" counts_dict = await queries.get_severity_counts(db) @@ -37,8 +39,8 @@ async def get_severity_counts_view( "request": request, "counts_dict": counts_dict, "agents": agents, - "refresh": refresh, - "delay": delay, + "auto_refresh_enabled": auto_refresh_enabled, + "auto_refresh_seconds": auto_refresh_seconds, }, ) @@ -151,3 +153,18 @@ async def get_agents_view(request: Request, db: Session = Depends(get_db)): return templates.TemplateResponse( "agents.html", {"request": request, "last_seen": last_seen} ) + + +@route.post("/refresh") +async def set_refresh_cookies_view( + request: Request, + auto_refresh_enabled: Annotated[bool, Form()] = False, + auto_refresh_seconds: Annotated[int, Form()] = 15, +): + response = RedirectResponse( + request.url_for("get_severity_counts_view"), + status_code=status.HTTP_303_SEE_OTHER, + ) + response.set_cookie(key="auto_refresh_enabled", value=auto_refresh_enabled) + response.set_cookie(key="auto_refresh_seconds", value=int(auto_refresh_seconds)) + return response diff --git a/argos/server/templates/base.html b/argos/server/templates/base.html index 7d01ad8..a623714 100644 --- a/argos/server/templates/base.html +++ b/argos/server/templates/base.html @@ -9,9 +9,9 @@ content="width=device-width, initial-scale=1.0"> - {% if refresh %} + {% if auto_refresh_enabled %} + content="{{ auto_refresh_seconds }}"> {% endif %} diff --git a/argos/server/templates/index.html b/argos/server/templates/index.html index 9a31d76..35b2fe0 100644 --- a/argos/server/templates/index.html +++ b/argos/server/templates/index.html @@ -13,27 +13,27 @@