Projekt

Obecné

Profil

Stáhnout (1.45 KB) Statistiky
| Větev: | Tag: | Revize:
1
from typing import List
2
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
3
from sqlalchemy.orm import Session
4
from sql_app import crud, models, schemas
5
from ..database import SessionLocal, engine
6
from fastapi import FastAPI, Request
7
from fastapi.responses import HTMLResponse, RedirectResponse
8
from fastapi_jwt_auth import AuthJWT
9
from fastapi.staticfiles import StaticFiles
10
from fastapi.templating import Jinja2Templates
11

    
12
models.Base.metadata.create_all(bind=engine)
13

    
14
# Path to html templates used in this file
15
templates = Jinja2Templates(directory="templates/pcs")
16

    
17
# prefix used for all endpoints in this file
18
pcs_web = APIRouter(prefix="")
19

    
20

    
21
# Dependency
22
def get_db():
23
    db = SessionLocal()
24
    try:
25
        yield db
26
    finally:
27
        db.close()
28

    
29

    
30
@pcs_web.get("/pcs-web", response_class=HTMLResponse)
31
async def read_pcs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
32
                   Authorize: AuthJWT = Depends()):
33
    """
34
    Returns template with all pcs currently saved in database
35
    """
36
    Authorize.jwt_optional()
37
    current_user = Authorize.get_jwt_subject()
38
    pcs = crud.get_pcs(db, skip=skip, limit=limit)
39
    if current_user == "admin":
40
        return templates.TemplateResponse("pcs.html", {"request": request, "pcs": pcs, "user": current_user})
41
    else:
42
        current_user = "guest"
43
        return templates.TemplateResponse("pcs_normal.html", {"request": request, "pcs": pcs, "user": current_user})
44

    
(10-10/15)