Projekt

Obecné

Profil

Stáhnout (2.15 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})
41
    else:
42
        return templates.TemplateResponse("pcs_normal.html", {"request": request, "pcs": pcs})
43

    
44

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

    
55

    
56
@pcs_web.post("/pcs-web/{pc_id}", response_class=HTMLResponse)
57
async def connect_post(pc_id: int, team: str = Form(...), db: Session = Depends(get_db)):
58
    """
59
    Endpoint called from within form for connecting pc with team. Updates certain pc with new team.
60
    """
61
    old_pc = crud.update_pc(db, pc_id, team)
62
    RedirectResponse("/pcs-web")
(9-9/13)