Projekt

Obecné

Profil

Stáhnout (1.66 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
8
from fastapi.staticfiles import StaticFiles
9
from fastapi.templating import Jinja2Templates
10

    
11
models.Base.metadata.create_all(bind=engine)
12
templates = Jinja2Templates(directory="templates/pcs")
13

    
14
pcs_web = APIRouter(prefix="/api/v1")
15

    
16

    
17
# Dependency
18
def get_db():
19
    db = SessionLocal()
20
    try:
21
        yield db
22
    finally:
23
        db.close()
24

    
25

    
26
@pcs_web.get("/pcs-web", response_class=HTMLResponse)
27
async def read_pcs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
28
    pcs = crud.get_pcs(db, skip=skip, limit=limit)
29
    return templates.TemplateResponse("pcs.html", {"request": request, "pcs": pcs})
30

    
31

    
32
@pcs_web.get("/pc-team/{pc_id}", response_class=HTMLResponse)
33
async def connect_pc_team(request: Request, pc_id: int, db: Session = Depends(get_db)):
34
    pc = crud.get_pc(db, pc_id)
35
    teams = crud.get_teams(db, 0, 100)
36
    return templates.TemplateResponse("pcteam.html",
37
                                      {"request": request, "pc": pc, "teams": teams})
38

    
39

    
40
@pcs_web.post("/pcs-web/{pc_id}", response_class=HTMLResponse)
41
async def connect_post(request: Request, pc_id: int, team: str = Form(...), skip: int = 0, limit: int = 100,
42
                       db: Session = Depends(get_db)):
43
    old_pc = crud.update_pc(db, pc_id, team)
44
    pcs = crud.get_pcs(db, skip=skip, limit=limit)
45
    return templates.TemplateResponse("pcs.html", {"request": request, "pcs": pcs})
(7-7/11)