Projekt

Obecné

Profil

Stáhnout (6.06 KB) Statistiky
| Větev: | Tag: | Revize:
1 b6f0e019 Matej Zeman
from datetime import datetime
2 c78ad6f2 Matej Zeman
3
from fastapi import Depends, APIRouter, Form
4
from fastapi import Request
5 2fe004b1 Matej Zeman
from fastapi.responses import HTMLResponse, RedirectResponse
6 b6f0e019 Matej Zeman
from fastapi.templating import Jinja2Templates
7 c78ad6f2 Matej Zeman
from fastapi_jwt_auth import AuthJWT
8
from pydantic import BaseModel
9
from sqlalchemy.orm import Session
10 2fe004b1 Matej Zeman
from sql_app.api.auth import fake_users_db
11 c78ad6f2 Matej Zeman
from sql_app import crud, models
12
from ..database import SessionLocal, engine
13 b6f0e019 Matej Zeman
14
models.Base.metadata.create_all(bind=engine)
15
16 4911f0ea Matej Zeman
# Path to html templates used in this file
17 4babde6c Matej Zeman
templates = Jinja2Templates(directory="templates/devices")
18 4911f0ea Matej Zeman
19
# prefix used for all endpoints in this file
20 af6f5886 Matěj Zeman
device_web = APIRouter(prefix="")
21 b6f0e019 Matej Zeman
22
23
# Dependency
24
def get_db():
25
    db = SessionLocal()
26
    try:
27
        yield db
28
    finally:
29
        db.close()
30
31
32
@device_web.get("/devices-web", response_class=HTMLResponse)
33 c78ad6f2 Matej Zeman
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
34
                       Authorize: AuthJWT = Depends()):
35 4911f0ea Matej Zeman
    """
36
    Returns template with all devices and its current states
37
    """
38 c78ad6f2 Matej Zeman
    Authorize.jwt_optional()
39
    current_user = Authorize.get_jwt_subject()
40
41 b6f0e019 Matej Zeman
    devices = crud.get_devices(db, skip=skip, limit=limit)
42 eaf8ace4 Matej Zeman
    statuses = []
43 4911f0ea Matej Zeman
    # adding state for each device in list
44 eaf8ace4 Matej Zeman
    for i in range(0, len(devices)):
45 c78ad6f2 Matej Zeman
        statuses.append(devices[i].logs[len(devices[i].logs) - 1].status)
46 eaf8ace4 Matej Zeman
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
47 2fe004b1 Matej Zeman
    if current_user == "admin":
48
        return templates.TemplateResponse("devices.html", {"request": request, "devs": len(devices), "devices": devices,
49 7fe7be79 zemanm98@students.zcu.cz
                                                           "statuses": statuses, "licenses": licenses, "user": current_user})
50 2fe004b1 Matej Zeman
    else:
51 7fe7be79 zemanm98@students.zcu.cz
        current_user = "guest"
52 2fe004b1 Matej Zeman
        return templates.TemplateResponse("devices_normal.html", {"request": request, "devs": len(devices), "devices": devices,
53 7fe7be79 zemanm98@students.zcu.cz
                                                                  "statuses": statuses, "licenses": licenses, "user": current_user})
54 b6f0e019 Matej Zeman
55
56
@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"),
58 7fe7be79 zemanm98@students.zcu.cz
                         db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
59 4911f0ea Matej Zeman
    """
60
    Endpoint used for filtering devices by license. returns html template with only
61
    devices that has assigned license defined by user input
62
    """
63 7fe7be79 zemanm98@students.zcu.cz
    Authorize.jwt_optional()
64
    current_user = Authorize.get_jwt_subject()
65 b6f0e019 Matej Zeman
    devices = crud.get_devices(db, skip=skip, limit=limit)
66
    def_devices = []
67
    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 4911f0ea Matej Zeman
    # if input was default all
72 b6f0e019 Matej Zeman
    if lic == "all":
73
        def_devices = devices
74 eaf8ace4 Matej Zeman
    statuses = []
75
    for i in range(0, len(def_devices)):
76
        statuses.append(def_devices[i].logs[len(def_devices[i].logs) - 1].status)
77
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
78 7fe7be79 zemanm98@students.zcu.cz
    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})
82
    else:
83
        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})
87 eaf8ace4 Matej Zeman
88
89
@device_web.get("/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 4911f0ea Matej Zeman
    """
93
    Returns template with one 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 eaf8ace4 Matej Zeman
    device = crud.get_device(db, device_id)
100 7fe7be79 zemanm98@students.zcu.cz
    dev_licenses = crud.get_device_licenses(db, device_id)
101
    lic_names = []
102
    dev_lics = []
103
    for dev_lic in dev_licenses:
104
        dev_lics.append(dev_lic.licenses)
105
    for dev_lic in dev_licenses:
106
        lic_names.append(dev_lic.licenses.name)
107 eaf8ace4 Matej Zeman
    licenses = crud.get_licenses(db, 0, 100)
108 7fe7be79 zemanm98@students.zcu.cz
    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 eaf8ace4 Matej Zeman
    return templates.TemplateResponse("devicelicense.html",
113 7fe7be79 zemanm98@students.zcu.cz
                                      {"request": request, "device": device, "licenses": lic_left, "dev_lic": dev_lics})
114 eaf8ace4 Matej Zeman
115
116 7fe7be79 zemanm98@students.zcu.cz
@device_web.post("/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 4911f0ea Matej Zeman
    """
120
    Endpoint called from template for connecting device with license. Adds entry to devices_licenses
121 7fe7be79 zemanm98@students.zcu.cz
    table and redirects to devices-web endpoint
122 4911f0ea Matej Zeman
    """
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 eaf8ace4 Matej Zeman
    crud.create_device_license(db, device_id, int(lic), datetime.now())
128 7fe7be79 zemanm98@students.zcu.cz
    return RedirectResponse(url=f"/devices-web", status_code=303)
129
130
131
@device_web.post("/devices-web-del/{device_id}")
132 5dc6d077 Matej Zeman
async def delete_post(device_id: int, lic_del: str = Form(...), db: Session = Depends(get_db),
133
                      Authorize: AuthJWT = Depends()):
134 7fe7be79 zemanm98@students.zcu.cz
    """
135
    Endpoint called from template for deleting device-license connection. Adds entry to bodydevices_licenses
136
    table and redirects to 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_device_license(db, device_id, int(lic_del))
143
    return RedirectResponse(url=f"/devices-web", status_code=303)