1
|
from typing import List
|
2
|
from typing import Optional
|
3
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
|
4
|
from sqlalchemy.orm import Session
|
5
|
from datetime import date
|
6
|
from sql_app import crud, models, schemas
|
7
|
from ..database import SessionLocal, engine
|
8
|
from fastapi import FastAPI, Request
|
9
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
10
|
from fastapi_jwt_auth import AuthJWT
|
11
|
from fastapi.staticfiles import StaticFiles
|
12
|
from fastapi.templating import Jinja2Templates
|
13
|
|
14
|
models.Base.metadata.create_all(bind=engine)
|
15
|
|
16
|
# Path to html templates used in this file
|
17
|
templates = Jinja2Templates(directory="templates/licenses")
|
18
|
device_templates = Jinja2Templates(directory="templates/devices")
|
19
|
|
20
|
# prefix used for all endpoints in this file
|
21
|
licenses_web = APIRouter(prefix="")
|
22
|
|
23
|
|
24
|
# Dependency
|
25
|
def get_db():
|
26
|
db = SessionLocal()
|
27
|
try:
|
28
|
yield db
|
29
|
finally:
|
30
|
db.close()
|
31
|
|
32
|
|
33
|
@licenses_web.get("/license-create", response_class=HTMLResponse)
|
34
|
async def licenses_create_web(request: Request, Authorize: AuthJWT = Depends()):
|
35
|
"""
|
36
|
Returns template with Form for creating new license.
|
37
|
"""
|
38
|
Authorize.jwt_optional()
|
39
|
current_user = Authorize.get_jwt_subject()
|
40
|
if current_user != "admin":
|
41
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
42
|
return templates.TemplateResponse("license_create.html", {"request": request, "minimum_date": date.today()})
|
43
|
|
44
|
|
45
|
@licenses_web.get("/licenses-web", response_class=HTMLResponse)
|
46
|
async def read_licenses_web(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
|
47
|
Authorize: AuthJWT = Depends()):
|
48
|
"""
|
49
|
Returns template with all licenses currently saved in database
|
50
|
"""
|
51
|
Authorize.jwt_optional()
|
52
|
current_user = Authorize.get_jwt_subject()
|
53
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
54
|
if current_user == "admin":
|
55
|
return templates.TemplateResponse("licenses.html", {"request": request, "licenses": licenses,
|
56
|
"user": current_user})
|
57
|
else:
|
58
|
current_user = "guest"
|
59
|
return templates.TemplateResponse("licenses_normal.html", {"request": request, "licenses": licenses,
|
60
|
"user": current_user})
|
61
|
|
62
|
@licenses_web.post("/licenses-web")
|
63
|
def create_license(name: Optional[str] = Form(""), lic_id: str = Form(...), expdate: Optional[date] = Form(None),
|
64
|
db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
|
65
|
"""
|
66
|
Endpoint called from create license form. Creates new license and redirects to devices-web endpoint
|
67
|
"""
|
68
|
Authorize.jwt_optional()
|
69
|
current_user = Authorize.get_jwt_subject()
|
70
|
if current_user != "admin":
|
71
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
72
|
licenses = crud.get_licenses(db, 0, 100)
|
73
|
licenses_ids = []
|
74
|
for l in licenses:
|
75
|
licenses_ids.append(l.license_id)
|
76
|
if lic_id not in licenses_ids:
|
77
|
db_license = crud.create_license(db, name, lic_id, expdate)
|
78
|
if db_license is None:
|
79
|
print("something went wrong")
|
80
|
return RedirectResponse(url=f"/licenses-web", status_code=303)
|