1 |
0fcb708f
|
Matej Zeman
|
from datetime import datetime
|
2 |
|
|
|
3 |
|
|
from fastapi import Depends, APIRouter, Form
|
4 |
|
|
from fastapi import Request
|
5 |
|
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
6 |
|
|
from fastapi.templating import Jinja2Templates
|
7 |
|
|
from fastapi_jwt_auth import AuthJWT
|
8 |
|
|
from pydantic import BaseModel
|
9 |
|
|
from sqlalchemy.orm import Session
|
10 |
|
|
from sql_app.api.auth import fake_users_db
|
11 |
|
|
from sql_app import crud, models
|
12 |
|
|
from ..database import SessionLocal, engine
|
13 |
|
|
|
14 |
|
|
models.Base.metadata.create_all(bind=engine)
|
15 |
|
|
|
16 |
|
|
# Path to html templates used in this file
|
17 |
|
|
templates = Jinja2Templates(directory="templates/head_devices")
|
18 |
|
|
|
19 |
|
|
# prefix used for all endpoints in this file
|
20 |
|
|
head_device_web = APIRouter(prefix="")
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
# Dependency
|
24 |
|
|
def get_db():
|
25 |
|
|
db = SessionLocal()
|
26 |
|
|
try:
|
27 |
|
|
yield db
|
28 |
|
|
finally:
|
29 |
|
|
db.close()
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
@head_device_web.get("/head-devices-web", response_class=HTMLResponse)
|
33 |
|
|
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
|
34 |
|
|
Authorize: AuthJWT = Depends()):
|
35 |
|
|
"""
|
36 |
|
|
Returns template with all head devices and necessary attributes
|
37 |
|
|
"""
|
38 |
|
|
Authorize.jwt_optional()
|
39 |
|
|
current_user = Authorize.get_jwt_subject()
|
40 |
|
|
|
41 |
|
|
device_dict = []
|
42 |
|
|
devices = crud.get_head_devices(db, skip=skip, limit=limit)
|
43 |
|
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
44 |
|
|
for dev in devices:
|
45 |
|
|
lic = crud.get_license(db, dev.license_id)
|
46 |
|
|
device_dict.append({"device": dev, "license": lic, "log": dev.h_logs[len(dev.h_logs) - 1]})
|
47 |
|
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
48 |
|
|
if current_user == "admin":
|
49 |
|
|
return templates.TemplateResponse("head_devices.html", {"request": request, "devices": device_dict,
|
50 |
|
|
"devs": devices, "teams": teams, "licenses": licenses,
|
51 |
93d0ec10
|
Matej Zeman
|
"user": current_user, "body_val": "", "lic_val": "",
|
52 |
|
|
"team_val": ""})
|
53 |
0fcb708f
|
Matej Zeman
|
else:
|
54 |
|
|
current_user = "guest"
|
55 |
|
|
return templates.TemplateResponse("head_devices_normal.html", {"request": request, "devices": device_dict,
|
56 |
|
|
"devs": devices, "teams": teams, "licenses": licenses,
|
57 |
93d0ec10
|
Matej Zeman
|
"user": current_user, "body_val": "", "lic_val": "",
|
58 |
|
|
"team_val": ""})
|
59 |
0fcb708f
|
Matej Zeman
|
|
60 |
|
|
|
61 |
|
|
@head_device_web.post("/head-devices-web", response_class=HTMLResponse)
|
62 |
|
|
async def filter_devices(request: Request, skip: int = 0, limit: int = 100,
|
63 |
|
|
body_id: str = Form("all"), lic_id: str = Form("all"), team: str = Form("all"),
|
64 |
|
|
db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
|
65 |
|
|
"""
|
66 |
|
|
Endpoint used for filtering head devices by user given inputs. returns html template with only
|
67 |
|
|
head devices that has attributes defined by user input
|
68 |
|
|
"""
|
69 |
|
|
Authorize.jwt_optional()
|
70 |
|
|
current_user = Authorize.get_jwt_subject()
|
71 |
|
|
device_dict = []
|
72 |
|
|
devices_f = crud.get_filtered_headdevices(db, body_id, lic_id, team)
|
73 |
|
|
ids = []
|
74 |
|
|
for d in devices_f:
|
75 |
|
|
ids.append(d[0])
|
76 |
|
|
devices = crud.get_headdevices_with_ids(db, ids)
|
77 |
|
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
78 |
|
|
for dev in devices:
|
79 |
|
|
lic = crud.get_license(db, dev.license_id)
|
80 |
|
|
device_dict.append({"device": dev, "license": lic, "log": dev.h_logs[len(dev.h_logs) - 1]})
|
81 |
|
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
82 |
93d0ec10
|
Matej Zeman
|
if body_id == "all":
|
83 |
|
|
body_id = ""
|
84 |
|
|
if lic_id == "all":
|
85 |
|
|
lic_id = ""
|
86 |
|
|
if team == "all":
|
87 |
|
|
team = ""
|
88 |
0fcb708f
|
Matej Zeman
|
if current_user == "admin":
|
89 |
|
|
return templates.TemplateResponse("head_devices.html", {"request": request, "devices": device_dict,
|
90 |
|
|
"devs": devices, "teams": teams, "licenses": licenses,
|
91 |
93d0ec10
|
Matej Zeman
|
"user": current_user, "body_val": body_id, "lic_val": lic_id,
|
92 |
|
|
"team_val": team})
|
93 |
0fcb708f
|
Matej Zeman
|
else:
|
94 |
|
|
current_user = "guest"
|
95 |
|
|
return templates.TemplateResponse("head_devices_normal.html", {"request": request, "devices": device_dict,
|
96 |
|
|
"devs": devices, "teams": teams,
|
97 |
|
|
"licenses": licenses,
|
98 |
93d0ec10
|
Matej Zeman
|
"user": current_user, "body_val": body_id, "lic_val": lic_id,
|
99 |
|
|
"team_val": team})
|
100 |
0fcb708f
|
Matej Zeman
|
|
101 |
|
|
|
102 |
|
|
@head_device_web.get("/head-device-license/{device_id}", response_class=HTMLResponse)
|
103 |
|
|
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db),
|
104 |
|
|
Authorize: AuthJWT = Depends()):
|
105 |
|
|
"""
|
106 |
|
|
Returns template with one head device and all available licenses that can be assigned to it, plus team and comment
|
107 |
|
|
and inventory number inputs.
|
108 |
|
|
"""
|
109 |
|
|
Authorize.jwt_optional()
|
110 |
|
|
current_user = Authorize.get_jwt_subject()
|
111 |
|
|
if current_user != "admin":
|
112 |
|
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
113 |
|
|
device = crud.get_head_device(db, device_id)
|
114 |
|
|
licenses = crud.get_licenses(db, 0, 100)
|
115 |
|
|
lic_left = []
|
116 |
|
|
for lic in licenses:
|
117 |
|
|
if lic != device.license:
|
118 |
|
|
lic_left.append(lic)
|
119 |
|
|
teams = crud.get_teams(db, 0, 100)
|
120 |
|
|
return templates.TemplateResponse("headlicense.html",
|
121 |
|
|
{"request": request, "device": device, "licenses": lic_left, "teams": teams})
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
@head_device_web.post("/head-devices-web-lic/{device_id}")
|
125 |
|
|
async def connect_post(device_id: int, lic: str = Form(...), db: Session = Depends(get_db),
|
126 |
|
|
Authorize: AuthJWT = Depends()):
|
127 |
|
|
"""
|
128 |
|
|
Endpoint called from template for connecting head device with license and redirects to head-devices-web endpoint
|
129 |
|
|
"""
|
130 |
|
|
Authorize.jwt_optional()
|
131 |
|
|
current_user = Authorize.get_jwt_subject()
|
132 |
|
|
if current_user != "admin":
|
133 |
|
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
134 |
|
|
crud.update_headdevice_license(db, device_id, int(lic))
|
135 |
|
|
return RedirectResponse(url=f"/head-devices-web", status_code=303)
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
@head_device_web.post("/head-devices-web-team/{device_id}")
|
139 |
|
|
async def delete_post(device_id: int, team_con: str = Form(...), db: Session = Depends(get_db),
|
140 |
|
|
Authorize: AuthJWT = Depends()):
|
141 |
|
|
"""
|
142 |
|
|
Endpoint called from template for connecting head device with team and redirects to body-devices-web endpoint
|
143 |
|
|
"""
|
144 |
|
|
Authorize.jwt_optional()
|
145 |
|
|
current_user = Authorize.get_jwt_subject()
|
146 |
|
|
if current_user != "admin":
|
147 |
|
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
148 |
|
|
crud.update_headdevice_team(db, device_id, int(team_con))
|
149 |
|
|
return RedirectResponse(url=f"/head-devices-web", status_code=303)
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
@head_device_web.post("/head-devices-inv/{device_id}")
|
153 |
|
|
async def device_inv(device_id: int, dev_inv: str = Form(...), db: Session = Depends(get_db),
|
154 |
|
|
Authorize: AuthJWT = Depends()):
|
155 |
|
|
"""
|
156 |
|
|
Endpoint called from within from headlicense.html template. Changes head devices inventory number with new one
|
157 |
|
|
given from user
|
158 |
|
|
"""
|
159 |
|
|
Authorize.jwt_optional()
|
160 |
|
|
current_user = Authorize.get_jwt_subject()
|
161 |
|
|
if current_user != "admin":
|
162 |
|
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
163 |
|
|
crud.update_headdevice_inv(db, device_id, dev_inv)
|
164 |
|
|
return RedirectResponse(url=f"/head-devices-web", status_code=303)
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
@head_device_web.post("/head-devices-comm/{device_id}")
|
168 |
|
|
async def device_inv(device_id: int, dev_com: str = Form(...), db: Session = Depends(get_db),
|
169 |
|
|
Authorize: AuthJWT = Depends()):
|
170 |
|
|
"""
|
171 |
|
|
Endpoint called from within from headlicense.html template. Changes head devices comment with new one
|
172 |
|
|
given from user
|
173 |
|
|
"""
|
174 |
|
|
Authorize.jwt_optional()
|
175 |
|
|
current_user = Authorize.get_jwt_subject()
|
176 |
|
|
if current_user != "admin":
|
177 |
|
|
return RedirectResponse(url=f"/logs-web", status_code=303)
|
178 |
|
|
crud.update_headdevice_comm(db, device_id, dev_com)
|
179 |
|
|
return RedirectResponse(url=f"/head-devices-web", status_code=303)
|