Projekt

Obecné

Profil

Stáhnout (2.68 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 7fe7be79 zemanm98@students.zcu.cz
        return templates.TemplateResponse("teams.html", {"request": request, "teams": teams, "user": current_user})
42 2fe004b1 Matej Zeman
    else:
43 7fe7be79 zemanm98@students.zcu.cz
        current_user = "guest"
44
        return templates.TemplateResponse("teams_normal.html", {"request": request, "teams": teams, "user": current_user})
45 eaf8ace4 Matej Zeman
46
47
@teams_web.get("/team-create", response_class=HTMLResponse)
48 5dc6d077 Matej Zeman
async def team_create_web(request: Request, Authorize: AuthJWT = Depends()):
49 4911f0ea Matej Zeman
    """
50
    Returns template with form for creating new team
51
    """
52 5dc6d077 Matej Zeman
    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 eaf8ace4 Matej Zeman
    return templates.TemplateResponse("team_create.html", {"request": request})
57
58
59 7fe7be79 zemanm98@students.zcu.cz
@teams_web.post("/teams-web-con")
60 5dc6d077 Matej Zeman
def create_team(name: str = Form(...), db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
61 4911f0ea Matej Zeman
    """
62
    Endpoint called from within form for creating new team. Creates new team and returns all teams in database
63
    """
64 5dc6d077 Matej Zeman
    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 7fe7be79 zemanm98@students.zcu.cz
    return RedirectResponse(url=f"/teams-web", status_code=303)