Projekt

Obecné

Profil

Stáhnout (2.12 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, RedirectResponse
9
from fastapi_jwt_auth import AuthJWT
10
from fastapi.staticfiles import StaticFiles
11
from fastapi.templating import Jinja2Templates
12

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

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

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

    
21

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

    
30

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

    
46

    
47
@teams_web.get("/team-create", response_class=HTMLResponse)
48
async def team_create_web(request: Request):
49
    """
50
    Returns template with form for creating new team
51
    """
52
    return templates.TemplateResponse("team_create.html", {"request": request})
53

    
54

    
55
@teams_web.post("/teams-web-con")
56
def create_team(name: str = Form(...), db: Session = Depends(get_db)):
57
    """
58
    Endpoint called from within form for creating new team. Creates new team and returns all teams in database
59
    """
60
    team = crud.create_team(db, name)
61
    if team is None:
62
        print("something went wrong")
63
    return RedirectResponse(url=f"/teams-web", status_code=303)
(12-12/14)