Projekt

Obecné

Profil

Stáhnout (1.55 KB) Statistiky
| Větev: | Tag: | Revize:
1
from typing import List
2

    
3
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
4
from sqlalchemy.orm import Session
5
from sql_app import crud, models, schemas
6
from ..database import SessionLocal, engine
7
from fastapi import FastAPI, Request
8
from fastapi.responses import HTMLResponse
9
from fastapi.staticfiles import StaticFiles
10
from fastapi.templating import Jinja2Templates
11

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

    
15
teams_web = APIRouter(prefix="/api/v1")
16

    
17

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

    
26

    
27
@teams_web.get("/teams-web", response_class=HTMLResponse)
28
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
29
    teams = crud.get_teams(db, skip=skip, limit=limit)
30
    return templates.TemplateResponse("teams.html", {"request": request, "teams": teams})
31

    
32

    
33
@teams_web.get("/team-create", response_class=HTMLResponse)
34
async def team_create_web(request: Request):
35
    return templates.TemplateResponse("team_create.html", {"request": request})
36

    
37

    
38
@teams_web.post("/teams-web", response_class=HTMLResponse)
39
def create_team(request: Request, name: str = Form(...), skip: int = 0, limit: int = 100,
40
                   db: Session = Depends(get_db)):
41
    team = crud.create_team(db, name)
42
    if team is None:
43
        print("something went wrong")
44
    teams = crud.get_teams(db, skip=skip, limit=limit)
45
    return templates.TemplateResponse("teams.html", {"request": request, "teams": teams})
(9-9/11)