1 |
b6f0e019
|
Matej Zeman
|
from typing import List
|
2 |
|
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
|
3 |
|
|
from sqlalchemy.orm import Session
|
4 |
|
|
from sql_app import crud, models, schemas
|
5 |
|
|
from ..database import SessionLocal, engine
|
6 |
|
|
from fastapi import FastAPI, Request
|
7 |
|
|
from fastapi.responses import HTMLResponse
|
8 |
|
|
from fastapi.staticfiles import StaticFiles
|
9 |
|
|
from fastapi.templating import Jinja2Templates
|
10 |
|
|
|
11 |
|
|
models.Base.metadata.create_all(bind=engine)
|
12 |
|
|
|
13 |
4911f0ea
|
Matej Zeman
|
# Path to html templates used in this file
|
14 |
4babde6c
|
Matej Zeman
|
templates = Jinja2Templates(directory="templates/pcs")
|
15 |
4911f0ea
|
Matej Zeman
|
|
16 |
|
|
# prefix used for all endpoints in this file
|
17 |
af6f5886
|
Matěj Zeman
|
pcs_web = APIRouter(prefix="")
|
18 |
b6f0e019
|
Matej Zeman
|
|
19 |
|
|
|
20 |
|
|
# Dependency
|
21 |
|
|
def get_db():
|
22 |
|
|
db = SessionLocal()
|
23 |
|
|
try:
|
24 |
|
|
yield db
|
25 |
|
|
finally:
|
26 |
|
|
db.close()
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
@pcs_web.get("/pcs-web", response_class=HTMLResponse)
|
30 |
|
|
async def read_pcs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
31 |
4911f0ea
|
Matej Zeman
|
"""
|
32 |
|
|
Returns template with all pcs currently saved in database
|
33 |
|
|
"""
|
34 |
b6f0e019
|
Matej Zeman
|
pcs = crud.get_pcs(db, skip=skip, limit=limit)
|
35 |
|
|
return templates.TemplateResponse("pcs.html", {"request": request, "pcs": pcs})
|
36 |
eaf8ace4
|
Matej Zeman
|
|
37 |
|
|
|
38 |
|
|
@pcs_web.get("/pc-team/{pc_id}", response_class=HTMLResponse)
|
39 |
|
|
async def connect_pc_team(request: Request, pc_id: int, db: Session = Depends(get_db)):
|
40 |
4911f0ea
|
Matej Zeman
|
"""
|
41 |
|
|
Returns template with Form for connecting pc with team
|
42 |
|
|
"""
|
43 |
eaf8ace4
|
Matej Zeman
|
pc = crud.get_pc(db, pc_id)
|
44 |
|
|
teams = crud.get_teams(db, 0, 100)
|
45 |
|
|
return templates.TemplateResponse("pcteam.html",
|
46 |
|
|
{"request": request, "pc": pc, "teams": teams})
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
@pcs_web.post("/pcs-web/{pc_id}", response_class=HTMLResponse)
|
50 |
|
|
async def connect_post(request: Request, pc_id: int, team: str = Form(...), skip: int = 0, limit: int = 100,
|
51 |
|
|
db: Session = Depends(get_db)):
|
52 |
4911f0ea
|
Matej Zeman
|
"""
|
53 |
|
|
Endpoint called from within form for connecting pc with team. Updates certain pc with new team.
|
54 |
|
|
"""
|
55 |
eaf8ace4
|
Matej Zeman
|
old_pc = crud.update_pc(db, pc_id, team)
|
56 |
|
|
pcs = crud.get_pcs(db, skip=skip, limit=limit)
|
57 |
|
|
return templates.TemplateResponse("pcs.html", {"request": request, "pcs": pcs})
|