Projekt

Obecné

Profil

Stáhnout (3.07 KB) Statistiky
| Větev: | Tag: | Revize:
1 b6f0e019 Matej Zeman
from typing import List
2
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 2fe004b1 Matej Zeman
from fastapi.responses import HTMLResponse, RedirectResponse
10 5dc6d077 Matej Zeman
from fastapi_jwt_auth import AuthJWT
11 b6f0e019 Matej Zeman
from fastapi.staticfiles import StaticFiles
12
from fastapi.templating import Jinja2Templates
13
14
models.Base.metadata.create_all(bind=engine)
15
16 4911f0ea Matej Zeman
# Path to html templates used in this file
17 4babde6c Matej Zeman
templates = Jinja2Templates(directory="templates/licenses")
18
device_templates = Jinja2Templates(directory="templates/devices")
19 4911f0ea Matej Zeman
20
# prefix used for all endpoints in this file
21 af6f5886 Matěj Zeman
licenses_web = APIRouter(prefix="")
22 b6f0e019 Matej Zeman
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 5dc6d077 Matej Zeman
async def licenses_create_web(request: Request, Authorize: AuthJWT = Depends()):
35 4911f0ea Matej Zeman
    """
36
    Returns template with Form for creating new license.
37
    """
38 5dc6d077 Matej Zeman
    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 b6f0e019 Matej Zeman
44
45
@licenses_web.get("/licenses-web", response_class=HTMLResponse)
46 5dc6d077 Matej Zeman
async def read_licenses_web(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
47
                            Authorize: AuthJWT = Depends()):
48 4911f0ea Matej Zeman
    """
49
    Returns template with all licenses currently saved in database
50
    """
51 5dc6d077 Matej Zeman
    Authorize.jwt_optional()
52
    current_user = Authorize.get_jwt_subject()
53 b6f0e019 Matej Zeman
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
54 5dc6d077 Matej Zeman
    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 b6f0e019 Matej Zeman
62 7fe7be79 zemanm98@students.zcu.cz
@licenses_web.post("/licenses-web")
63 5dc6d077 Matej Zeman
def create_license(name: str = Form(...), expdate: date = Form(...), db: Session = Depends(get_db),
64
                   Authorize: AuthJWT = Depends()):
65 4911f0ea Matej Zeman
    """
66 7fe7be79 zemanm98@students.zcu.cz
    Endpoint called from create license form. Creates new license and redirects to devices-web endpoint
67 4911f0ea Matej Zeman
    """
68 5dc6d077 Matej Zeman
    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_names = []
74
    for l in licenses:
75
        licenses_names.append(l.name)
76
    if name not in licenses_names:
77
        db_license = crud.create_license(db, name, expdate)
78
        if db_license is None:
79
            print("something went wrong")
80 7fe7be79 zemanm98@students.zcu.cz
    return RedirectResponse(url=f"/devices-web", status_code=303)