Projekt

Obecné

Profil

Stáhnout (2.04 KB) Statistiky
| Větev: | Tag: | Revize:
1 b6f0e019 Matej Zeman
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 2fe004b1 Matej Zeman
from fastapi.responses import HTMLResponse, RedirectResponse
9
from fastapi_jwt_auth import AuthJWT
10 b6f0e019 Matej Zeman
from fastapi.staticfiles import StaticFiles
11
from fastapi.templating import Jinja2Templates
12
13
models.Base.metadata.create_all(bind=engine)
14
15 4911f0ea Matej Zeman
# Path to html templates used in this file
16 4babde6c Matej Zeman
templates = Jinja2Templates(directory="templates/teams")
17 4911f0ea Matej Zeman
18
# prefix used for all endpoints in this file
19 af6f5886 Matěj Zeman
teams_web = APIRouter(prefix="")
20 b6f0e019 Matej Zeman
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 2fe004b1 Matej Zeman
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
33
                       Authorize: AuthJWT = Depends()):
34 4911f0ea Matej Zeman
    """
35
    Returns template with all teams currently saved in database
36
    """
37 2fe004b1 Matej Zeman
    Authorize.jwt_optional()
38
    current_user = Authorize.get_jwt_subject()
39 b6f0e019 Matej Zeman
    teams = crud.get_teams(db, skip=skip, limit=limit)
40 2fe004b1 Matej Zeman
    if current_user == "admin":
41
        return templates.TemplateResponse("teams.html", {"request": request, "teams": teams})
42
    else:
43
        return templates.TemplateResponse("teams_normal.html", {"request": request, "teams": teams})
44 eaf8ace4 Matej Zeman
45
46
@teams_web.get("/team-create", response_class=HTMLResponse)
47
async def team_create_web(request: Request):
48 4911f0ea Matej Zeman
    """
49
    Returns template with form for creating new team
50
    """
51 eaf8ace4 Matej Zeman
    return templates.TemplateResponse("team_create.html", {"request": request})
52
53
54
@teams_web.post("/teams-web", response_class=HTMLResponse)
55 2fe004b1 Matej Zeman
def create_team(name: str = Form(...), db: Session = Depends(get_db)):
56 4911f0ea Matej Zeman
    """
57
    Endpoint called from within form for creating new team. Creates new team and returns all teams in database
58
    """
59 eaf8ace4 Matej Zeman
    team = crud.create_team(db, name)
60
    if team is None:
61
        print("something went wrong")
62 2fe004b1 Matej Zeman
    RedirectResponse("/teams-web")