Projekt

Obecné

Profil

Stáhnout (2.22 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

    
45

    
46
@pcs_web.get("/pc-team/{pc_id}", response_class=HTMLResponse)
47
async def connect_pc_team(request: Request, pc_id: int, db: Session = Depends(get_db)):
48
    """
49
    Returns template with Form for connecting pc with team
50
    """
51
    pc = crud.get_pc(db, pc_id)
52
    teams = crud.get_teams(db, 0, 100)
53
    return templates.TemplateResponse("pcteam.html",
54
                                      {"request": request, "pc": pc, "teams": teams})
55

    
56

    
57
@pcs_web.post("/pcs-web/{pc_id}")
58
async def connect_post(pc_id: int, team: str = Form(...), db: Session = Depends(get_db)):
59
    """
60
    Endpoint called from within form for connecting pc with team. Updates certain pc with new team.
61
    """
62
    old_pc = crud.update_pc(db, pc_id, team)
63
    return RedirectResponse(url=f"/pcs-web", status_code=303)
(10-10/14)