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, Authorize: AuthJWT = Depends()):
|
49
|
"""
|
50
|
Returns template with form for creating new team
|
51
|
"""
|
52
|
Authorize.jwt_optional()
|
53
|
current_user = Authorize.get_jwt_subject()
|
54
|
if current_user != "admin":
|
55
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
56
|
return templates.TemplateResponse("team_create.html", {"request": request})
|
57
|
|
58
|
|
59
|
@teams_web.post("/teams-web-con")
|
60
|
def create_team(name: str = Form(...), db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
|
61
|
"""
|
62
|
Endpoint called from within form for creating new team. Creates new team and redirects to view with all teams
|
63
|
"""
|
64
|
Authorize.jwt_optional()
|
65
|
current_user = Authorize.get_jwt_subject()
|
66
|
if current_user != "admin":
|
67
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
68
|
teams = crud.get_teams(db, 0, 100)
|
69
|
teams_names = []
|
70
|
for t in teams:
|
71
|
teams_names.append(t.name)
|
72
|
if name not in teams_names:
|
73
|
team = crud.create_team(db, name)
|
74
|
if team is None:
|
75
|
print("something went wrong")
|
76
|
return RedirectResponse(url=f"/teams-web", status_code=303)
|
77
|
|
78
|
|
79
|
@teams_web.get("/team-change/{team_id}", response_class=HTMLResponse)
|
80
|
async def team_change_web(request: Request, team_id: int, db: Session = Depends(get_db),
|
81
|
Authorize: AuthJWT = Depends()):
|
82
|
"""
|
83
|
Returns template with form for changing teams name
|
84
|
"""
|
85
|
Authorize.jwt_optional()
|
86
|
current_user = Authorize.get_jwt_subject()
|
87
|
if current_user != "admin":
|
88
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
89
|
team = crud.get_team(db, team_id)
|
90
|
return templates.TemplateResponse("team_change.html", {"request": request, "team": team})
|
91
|
|
92
|
@teams_web.post("/teams-change-process/{team_id}")
|
93
|
async def team_change_process(team_id: int, db:Session = Depends(get_db), name: str = Form(...),
|
94
|
Authorize: AuthJWT = Depends()):
|
95
|
"""
|
96
|
Changes teams name to a new one given by user
|
97
|
"""
|
98
|
Authorize.jwt_optional()
|
99
|
current_user = Authorize.get_jwt_subject()
|
100
|
if current_user != "admin":
|
101
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
102
|
team = crud.change_team(db, team_id, name)
|
103
|
return RedirectResponse(url=f"/teams-web", status_code=303)
|