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})
|
42
|
else:
|
43
|
return templates.TemplateResponse("teams_normal.html", {"request": request, "teams": teams})
|
44
|
|
45
|
|
46
|
@teams_web.get("/team-create", response_class=HTMLResponse)
|
47
|
async def team_create_web(request: Request):
|
48
|
"""
|
49
|
Returns template with form for creating new team
|
50
|
"""
|
51
|
return templates.TemplateResponse("team_create.html", {"request": request})
|
52
|
|
53
|
|
54
|
@teams_web.post("/teams-web", response_class=HTMLResponse)
|
55
|
def create_team(name: str = Form(...), db: Session = Depends(get_db)):
|
56
|
"""
|
57
|
Endpoint called from within form for creating new team. Creates new team and returns all teams in database
|
58
|
"""
|
59
|
team = crud.create_team(db, name)
|
60
|
if team is None:
|
61
|
print("something went wrong")
|
62
|
RedirectResponse("/teams-web")
|