1
|
from typing import List
|
2
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
|
3
|
from datetime import datetime
|
4
|
from sqlalchemy.orm import Session
|
5
|
from sql_app import crud, models, schemas
|
6
|
from ..database import SessionLocal, engine
|
7
|
from fastapi import FastAPI, Request
|
8
|
from fastapi.responses import HTMLResponse
|
9
|
from fastapi_jwt_auth import AuthJWT
|
10
|
from fastapi.staticfiles import StaticFiles
|
11
|
from fastapi.templating import Jinja2Templates
|
12
|
|
13
|
models.Base.metadata.create_all(bind=engine)
|
14
|
|
15
|
# Path to html templates used in this file
|
16
|
templates = Jinja2Templates(directory="templates/usb-logs")
|
17
|
|
18
|
# prefix used for all endpoints in this file
|
19
|
usblogs_web = APIRouter(prefix="")
|
20
|
|
21
|
|
22
|
# Dependency
|
23
|
def get_db():
|
24
|
db = SessionLocal()
|
25
|
try:
|
26
|
yield db
|
27
|
finally:
|
28
|
db.close()
|
29
|
|
30
|
|
31
|
@usblogs_web.get("/logs-web", response_class=HTMLResponse)
|
32
|
async def read_logs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
|
33
|
Authorize: AuthJWT = Depends()):
|
34
|
"""
|
35
|
Returns template with all usb logs currently saved in database with its pcs, teams and licenses.
|
36
|
"""
|
37
|
Authorize.jwt_optional()
|
38
|
current_user = Authorize.get_jwt_subject()
|
39
|
logs = crud.get_logs(db, skip=skip, limit=limit)
|
40
|
pcs = []
|
41
|
for log in logs:
|
42
|
if log.pc_id not in pcs:
|
43
|
pcs.append(log.pc_id)
|
44
|
pc_obj = crud.find_pcs(db, pcs)
|
45
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
46
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
47
|
if current_user != "admin":
|
48
|
current_user = "guest"
|
49
|
return templates.TemplateResponse("logs.html", {"request": request, "logs": logs, "pcs": pc_obj, "teams": teams,
|
50
|
"licenses": licenses, "user": current_user})
|
51
|
|
52
|
|
53
|
@usblogs_web.post("/logs-web", response_class=HTMLResponse)
|
54
|
async def filter_logs(request: Request, pc: str = Form("all"), team: str = Form("all"), lic: str = Form("all"),
|
55
|
skip: int = 0, limit: int = 100,
|
56
|
db: Session = Depends(get_db)):
|
57
|
"""
|
58
|
Endpoint used for filtering usb logs by user given form inputs.
|
59
|
"""
|
60
|
log = crud.get_filtered_logs(db, pc, team, lic)
|
61
|
logs_ids = []
|
62
|
for l in log:
|
63
|
logs_ids.append(l[0])
|
64
|
logs = crud.find_filtered_logs(db, logs_ids)
|
65
|
pc_obj = crud.get_pcs(db, skip=skip, limit=limit)
|
66
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
67
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
68
|
return templates.TemplateResponse("logs.html", {"request": request, "logs": logs, "pcs": pc_obj, "teams": teams,
|
69
|
"licenses": licenses})
|
70
|
|