Revize 35168e5f
Přidáno uživatelem Matěj Zeman před více než 2 roky(ů)
server/sql_app/api/devices_web.py | ||
---|---|---|
8 | 8 |
from pydantic import BaseModel |
9 | 9 |
from sqlalchemy.orm import Session |
10 | 10 |
from sql_app.api.auth import fake_users_db |
11 |
from sql_app import crud, models |
|
11 |
from sql_app import crud, models, schemas
|
|
12 | 12 |
from ..database import SessionLocal, engine |
13 | 13 |
|
14 | 14 |
models.Base.metadata.create_all(bind=engine) |
... | ... | |
38 | 38 |
Authorize.jwt_optional() |
39 | 39 |
current_user = Authorize.get_jwt_subject() |
40 | 40 |
|
41 |
device_dict = [] |
|
41 | 42 |
devices = crud.get_devices(db, skip=skip, limit=limit) |
42 |
statuses = [] |
|
43 |
# adding state for each device in list |
|
44 |
for i in range(0, len(devices)): |
|
45 |
statuses.append(devices[i].logs[len(devices[i].logs) - 1].status) |
|
43 |
teams = crud.get_teams(db, skip=skip, limit=limit) |
|
44 |
# adding dictionary entry with all inforamtions needed in template |
|
45 |
for dev in devices: |
|
46 |
if len(dev.licenses) > 0: |
|
47 |
for lic in dev.licenses: |
|
48 |
device_dict.append({"device": dev, "license": lic.licenses, "log": dev.logs[len(dev.logs) - 1]}) |
|
49 |
else: |
|
50 |
device_dict.append({"device": dev, "license": dev.licenses, "log": dev.logs[len(dev.logs) - 1]}) |
|
46 | 51 |
licenses = crud.get_licenses(db, skip=skip, limit=limit) |
47 | 52 |
if current_user == "admin": |
48 |
return templates.TemplateResponse("devices.html", {"request": request, "devs": len(devices), "devices": devices, |
|
49 |
"statuses": statuses, "licenses": licenses, "user": current_user}) |
|
53 |
return templates.TemplateResponse("devices.html", {"request": request, "devices": device_dict, |
|
54 |
"licenses": licenses, "devs": devices, |
|
55 |
"teams": teams, "user": current_user}) |
|
50 | 56 |
else: |
51 | 57 |
current_user = "guest" |
52 |
return templates.TemplateResponse("devices_normal.html", {"request": request, "devs": len(devices), "devices": devices,
|
|
53 |
"statuses": statuses, "licenses": licenses, "user": current_user})
|
|
58 |
return templates.TemplateResponse("devices_normal.html", {"request": request, "devices": device_dict,
|
|
59 |
"licenses": licenses, "user": current_user}) |
|
54 | 60 |
|
55 | 61 |
|
56 | 62 |
@device_web.post("/devices-web", response_class=HTMLResponse) |
57 |
async def filter_devices(request: Request, skip: int = 0, limit: int = 100, lic: str = Form("all"), |
|
63 |
async def filter_devices(request: Request, skip: int = 0, limit: int = 100, |
|
64 |
keyman_id: str = Form("all"), lic_name: str = Form("all"), |
|
65 |
lic_id: str = Form("all"), team: str = Form("all"), |
|
58 | 66 |
db: Session = Depends(get_db), Authorize: AuthJWT = Depends()): |
59 | 67 |
""" |
60 | 68 |
Endpoint used for filtering devices by license. returns html template with only |
... | ... | |
62 | 70 |
""" |
63 | 71 |
Authorize.jwt_optional() |
64 | 72 |
current_user = Authorize.get_jwt_subject() |
65 |
devices = crud.get_devices(db, skip=skip, limit=limit) |
|
66 |
def_devices = [] |
|
73 |
device_dict = [] |
|
74 |
devices_f = crud.get_filtered_devices(db, keyman_id, lic_name, lic_id, team) |
|
75 |
ids = [] |
|
76 |
for d in devices_f: |
|
77 |
ids.append(d[0]) |
|
78 |
devices = crud.get_devices_with_ids(db, ids) |
|
79 |
teams = crud.get_teams(db, skip=skip, limit=limit) |
|
80 |
# adding dictionary entry with all inforamtions needed in template |
|
67 | 81 |
for dev in devices: |
68 |
for l in dev.licenses: |
|
69 |
if dev not in def_devices and l.licenses.name == lic: |
|
70 |
def_devices.append(dev) |
|
71 |
# if input was default all |
|
72 |
if lic == "all": |
|
73 |
def_devices = devices |
|
74 |
statuses = [] |
|
75 |
for i in range(0, len(def_devices)): |
|
76 |
statuses.append(def_devices[i].logs[len(def_devices[i].logs) - 1].status) |
|
82 |
if len(dev.licenses) > 0: |
|
83 |
for lic in dev.licenses: |
|
84 |
device_dict.append({"device": dev, "license": lic.licenses, "log": dev.logs[len(dev.logs) - 1]}) |
|
85 |
else: |
|
86 |
device_dict.append({"device": dev, "license": dev.licenses, "log": dev.logs[len(dev.logs) - 1]}) |
|
77 | 87 |
licenses = crud.get_licenses(db, skip=skip, limit=limit) |
78 | 88 |
if current_user == "admin": |
79 |
return templates.TemplateResponse("devices.html", |
|
80 |
{"request": request, "devs": len(def_devices), "devices": def_devices,
|
|
81 |
"statuses": statuses, "licenses": licenses, "user": current_user})
|
|
89 |
return templates.TemplateResponse("devices.html", {"request": request, "devices": device_dict,
|
|
90 |
"licenses": licenses, "devs": devices,
|
|
91 |
"teams": teams, "user": current_user})
|
|
82 | 92 |
else: |
83 | 93 |
current_user = "guest" |
84 |
return templates.TemplateResponse("devices_normal.html", |
|
85 |
{"request": request, "devs": len(def_devices), "devices": def_devices, |
|
86 |
"statuses": statuses, "licenses": licenses, "user": current_user}) |
|
94 |
return templates.TemplateResponse("devices_normal.html", {"request": request, "devices": device_dict, |
|
95 |
"licenses": licenses, "user": current_user}) |
|
87 | 96 |
|
88 | 97 |
|
89 | 98 |
@device_web.get("/device-license/{device_id}", response_class=HTMLResponse) |
... | ... | |
98 | 107 |
return RedirectResponse(url=f"/logs-web", status_code=303) |
99 | 108 |
device = crud.get_device(db, device_id) |
100 | 109 |
dev_licenses = crud.get_device_licenses(db, device_id) |
101 |
lic_names = []
|
|
110 |
lic_ids = []
|
|
102 | 111 |
dev_lics = [] |
103 | 112 |
for dev_lic in dev_licenses: |
104 | 113 |
dev_lics.append(dev_lic.licenses) |
105 | 114 |
for dev_lic in dev_licenses: |
106 |
lic_names.append(dev_lic.licenses.name)
|
|
115 |
lic_ids.append(dev_lic.licenses.license_id)
|
|
107 | 116 |
licenses = crud.get_licenses(db, 0, 100) |
108 | 117 |
lic_left = [] |
109 | 118 |
for lic in licenses: |
110 |
if lic.name not in lic_names and lic not in lic_left:
|
|
119 |
if lic.license_id not in lic_ids and lic not in lic_left:
|
|
111 | 120 |
lic_left.append(lic) |
121 |
teams = crud.get_teams(db, 0, 100) |
|
112 | 122 |
return templates.TemplateResponse("devicelicense.html", |
113 |
{"request": request, "device": device, "licenses": lic_left, "dev_lic": dev_lics}) |
|
123 |
{"request": request, "device": device, "licenses": lic_left, "dev_lic": dev_lics, |
|
124 |
"teams": teams}) |
|
114 | 125 |
|
115 | 126 |
|
116 | 127 |
@device_web.post("/devices-web/{device_id}") |
... | ... | |
141 | 152 |
return RedirectResponse(url=f"/logs-web", status_code=303) |
142 | 153 |
crud.delete_device_license(db, device_id, int(lic_del)) |
143 | 154 |
return RedirectResponse(url=f"/devices-web", status_code=303) |
155 |
|
|
156 |
@device_web.post("/devices-web-team/{device_id}") |
|
157 |
async def dev_team_con(device_id: int, team_con: str = Form(...), db: Session = Depends(get_db), |
|
158 |
Authorize: AuthJWT = Depends()): |
|
159 |
""" |
|
160 |
Endpoint called from template for deleting device-license connection. Adds entry to bodydevices_licenses |
|
161 |
table and redirects to devices-web endpoint |
|
162 |
""" |
|
163 |
Authorize.jwt_optional() |
|
164 |
current_user = Authorize.get_jwt_subject() |
|
165 |
if current_user != "admin": |
|
166 |
return RedirectResponse(url=f"/logs-web", status_code=303) |
|
167 |
crud.update_device(db, device_id, team_con) |
|
168 |
return RedirectResponse(url=f"/devices-web", status_code=303) |
|
169 |
|
|
170 |
@device_web.post("/devices-web-inv/{device_id}") |
|
171 |
async def dev_inv_new(device_id: int, dev_inv: str = Form(...), db: Session = Depends(get_db), |
|
172 |
Authorize: AuthJWT = Depends()): |
|
173 |
""" |
|
174 |
Endpoint called from template for deleting device-license connection. Adds entry to bodydevices_licenses |
|
175 |
table and redirects to devices-web endpoint |
|
176 |
""" |
|
177 |
Authorize.jwt_optional() |
|
178 |
current_user = Authorize.get_jwt_subject() |
|
179 |
if current_user != "admin": |
|
180 |
return RedirectResponse(url=f"/logs-web", status_code=303) |
|
181 |
crud.update_device_inv(db, device_id, dev_inv) |
|
182 |
return RedirectResponse(url=f"/devices-web", status_code=303) |
|
183 |
|
|
184 |
@device_web.post("/devices-web-comment/{device_id}") |
|
185 |
async def dev_comm_new(device_id: int, dev_com: str = Form(...), db: Session = Depends(get_db), |
|
186 |
Authorize: AuthJWT = Depends()): |
|
187 |
""" |
|
188 |
Endpoint called from template for deleting device-license connection. Adds entry to bodydevices_licenses |
|
189 |
table and redirects to devices-web endpoint |
|
190 |
""" |
|
191 |
Authorize.jwt_optional() |
|
192 |
current_user = Authorize.get_jwt_subject() |
|
193 |
if current_user != "admin": |
|
194 |
return RedirectResponse(url=f"/logs-web", status_code=303) |
|
195 |
crud.update_device_com(db, device_id, dev_com) |
|
196 |
return RedirectResponse(url=f"/devices-web", status_code=303) |
Také k dispozici: Unified diff
Fixed minor issues. View on keyman devices changed. Changed database for new keyman x license connection.