Projekt

Obecné

Profil

Stáhnout (6.22 KB) Statistiky
| Větev: | Tag: | Revize:
1 7fe7be79 zemanm98@students.zcu.cz
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/body-devices")
18
19
# prefix used for all endpoints in this file
20
body_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
@body_device_web.get("/body-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 body devices and its current states
37
    """
38
    Authorize.jwt_optional()
39
    current_user = Authorize.get_jwt_subject()
40
41
    devices = crud.get_body_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].b_logs[len(devices[i].b_logs) - 1].status)
46
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
47
    if current_user == "admin":
48
        return templates.TemplateResponse("body_devices.html", {"request": request, "devs": len(devices), "devices": devices,
49
                                                           "statuses": statuses, "licenses": licenses, "user": current_user})
50
    else:
51
        current_user = "guest"
52
        return templates.TemplateResponse("body_devices_normal.html", {"request": request, "devs": len(devices), "devices": devices,
53
                                                           "statuses": statuses, "licenses": licenses, "user": current_user})
54
55
56
@body_device_web.post("/body-devices-web", response_class=HTMLResponse)
57
async def filter_devices(request: Request, skip: int = 0, limit: int = 100, lic: str = Form("all"),
58
                         db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
59
    """
60
    Endpoint used for filtering body devices by license. returns html template with only
61
    body devices that has assigned license defined by user input
62
    """
63
    Authorize.jwt_optional()
64
    current_user = Authorize.get_jwt_subject()
65
    devices = crud.get_body_devices(db, skip=skip, limit=limit)
66
    def_devices = []
67
    for dev in devices:
68
        for l in dev.debug_licenses:
69
            if dev not in def_devices and l.b_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].b_logs[len(def_devices[i].b_logs) - 1].status)
77
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
78
    if current_user == "admin":
79
        return templates.TemplateResponse("body_devices.html",
80
                                          {"request": request, "devs": len(def_devices), "devices": def_devices,
81
                                           "statuses": statuses, "licenses": licenses, "user": current_user})
82
    else:
83
        current_user = "guest"
84
        return templates.TemplateResponse("body_devices_normal.html",
85
                                          {"request": request, "devs": len(def_devices), "devices": def_devices,
86
                                           "statuses": statuses, "licenses": licenses, "user": current_user})
87
88
89
@body_device_web.get("/body-device-license/{device_id}", response_class=HTMLResponse)
90 5dc6d077 Matej Zeman
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db),
91
                          Authorize: AuthJWT = Depends()):
92 7fe7be79 zemanm98@students.zcu.cz
    """
93
    Returns template with one body device and all available licenses that can be assigned to it.
94
    """
95 5dc6d077 Matej Zeman
    Authorize.jwt_optional()
96
    current_user = Authorize.get_jwt_subject()
97
    if current_user != "admin":
98
        return RedirectResponse(url=f"/logs-web", status_code=303)
99 7fe7be79 zemanm98@students.zcu.cz
    device = crud.get_body_device(db, device_id)
100
    dev_licenses = crud.get_bodydevice_license(db, device_id)
101
    lic_names = []
102
    dev_lics = []
103
    for dev_lic in dev_licenses:
104
        dev_lics.append(dev_lic.b_licenses)
105
    for dev_lic in dev_licenses:
106
        lic_names.append(dev_lic.b_licenses.name)
107
    licenses = crud.get_licenses(db, 0, 100)
108
    lic_left = []
109
    for lic in licenses:
110
        if lic.name not in lic_names and lic not in lic_left:
111
            lic_left.append(lic)
112
    return templates.TemplateResponse("body_device_license.html",
113
                                      {"request": request, "device": device, "licenses": lic_left, "dev_lic": dev_lics})
114
115
116
@body_device_web.post("/body-devices-web/{device_id}")
117 5dc6d077 Matej Zeman
async def connect_post(device_id: int, lic: str = Form(...), db: Session = Depends(get_db),
118
                       Authorize: AuthJWT = Depends()):
119 7fe7be79 zemanm98@students.zcu.cz
    """
120
    Endpoint called from template for connecting body device with license. Adds entry to bodydevices_licenses
121
    table and redirects to body-devices-web endpoint
122
    """
123 5dc6d077 Matej Zeman
    Authorize.jwt_optional()
124
    current_user = Authorize.get_jwt_subject()
125
    if current_user != "admin":
126
        return RedirectResponse(url=f"/logs-web", status_code=303)
127 7fe7be79 zemanm98@students.zcu.cz
    crud.create_body_device_license(db, device_id, int(lic), datetime.now())
128
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
129
130
131
@body_device_web.post("/body-devices-web-del/{device_id}")
132 5dc6d077 Matej Zeman
async def delete_post(device_id: int, b_lic: str = Form(...), db: Session = Depends(get_db),
133
                      Authorize: AuthJWT = Depends()):
134 7fe7be79 zemanm98@students.zcu.cz
    """
135
    Endpoint called from template for connecting body device with license. Adds entry to devices_licenses
136
    table and redirects to body-devices-web endpoint
137
    """
138 5dc6d077 Matej Zeman
    Authorize.jwt_optional()
139
    current_user = Authorize.get_jwt_subject()
140
    if current_user != "admin":
141
        return RedirectResponse(url=f"/logs-web", status_code=303)
142 7fe7be79 zemanm98@students.zcu.cz
    crud.delete_bodydevice_license(db, device_id, int(b_lic))
143
    return RedirectResponse(url=f"/body-devices-web", status_code=303)