Projekt

Obecné

Profil

Stáhnout (7.75 KB) Statistiky
| Větev: | Tag: | Revize:
1
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 necessary attributes
37
    """
38
    Authorize.jwt_optional()
39
    current_user = Authorize.get_jwt_subject()
40

    
41
    device_dict = []
42
    devices = crud.get_body_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.b_logs[len(dev.b_logs) - 1]})
47
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
48
    if current_user == "admin":
49
        return templates.TemplateResponse("body_devices.html", {"request": request, "devices": device_dict,
50
                                                                "devs": devices, "teams": teams, "licenses": licenses,
51
                                                                "user": current_user})
52
    else:
53
        current_user = "guest"
54
        return templates.TemplateResponse("body_devices_normal.html", {"request": request, "devices": device_dict,
55
                                                                "devs": devices, "teams": teams, "licenses": licenses,
56
                                                                "user": current_user})
57

    
58

    
59
@body_device_web.post("/body-devices-web", response_class=HTMLResponse)
60
async def filter_devices(request: Request, skip: int = 0, limit: int = 100,
61
                         body_id: str = Form("all"), lic_id: str = Form("all"), team: str = Form("all"),
62
                         db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
63
    """
64
    Endpoint used for filtering body devices by user given inputs. returns html template with only
65
    body devices that has attributes defined by user input
66
    """
67
    Authorize.jwt_optional()
68
    current_user = Authorize.get_jwt_subject()
69
    device_dict = []
70
    devices_f = crud.get_filtered_bodydevices(db, body_id, lic_id, team)
71
    ids = []
72
    for d in devices_f:
73
        ids.append(d[0])
74
    devices = crud.get_bodydevices_with_ids(db, ids)
75
    teams = crud.get_teams(db, skip=skip, limit=limit)
76
    for dev in devices:
77
        lic = crud.get_license(db, dev.license_id)
78
        device_dict.append({"device": dev, "license": lic, "log": dev.b_logs[len(dev.b_logs) - 1]})
79
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
80
    if current_user == "admin":
81
        return templates.TemplateResponse("body_devices.html", {"request": request, "devices": device_dict,
82
                                                                "devs": devices, "teams": teams, "licenses": licenses,
83
                                                                "user": current_user})
84
    else:
85
        current_user = "guest"
86
        return templates.TemplateResponse("body_devices_normal.html", {"request": request, "devices": device_dict,
87
                                                                       "devs": devices, "teams": teams,
88
                                                                       "licenses": licenses,
89
                                                                       "user": current_user})
90

    
91

    
92
@body_device_web.get("/body-device-license/{device_id}", response_class=HTMLResponse)
93
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db),
94
                          Authorize: AuthJWT = Depends()):
95
    """
96
    Returns template with one body device and all available licenses that can be assigned to it. Plus available teams
97
    that can be assigned to device, inventory number and comment text input for this device.
98
    """
99
    Authorize.jwt_optional()
100
    current_user = Authorize.get_jwt_subject()
101
    if current_user != "admin":
102
        return RedirectResponse(url=f"/logs-web", status_code=303)
103
    device = crud.get_body_device(db, device_id)
104
    licenses = crud.get_licenses(db, 0, 100)
105
    lic_left = []
106
    for lic in licenses:
107
        if lic != device.license:
108
            lic_left.append(lic)
109
    teams = crud.get_teams(db, 0, 100)
110
    return templates.TemplateResponse("body_device_license.html",
111
                                      {"request": request, "device": device, "licenses": lic_left, "teams": teams})
112

    
113

    
114
@body_device_web.post("/body-devices-web-lic/{device_id}")
115
async def connect_post(device_id: int, lic: str = Form(...), db: Session = Depends(get_db),
116
                       Authorize: AuthJWT = Depends()):
117
    """
118
    Endpoint called from template from body_device_license.html template. Connects body device with license
119
    and redirects to body-devices-web endpoint
120
    """
121
    Authorize.jwt_optional()
122
    current_user = Authorize.get_jwt_subject()
123
    if current_user != "admin":
124
        return RedirectResponse(url=f"/logs-web", status_code=303)
125
    crud.update_bodydevice_license(db, device_id, int(lic))
126
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
127

    
128

    
129
@body_device_web.post("/body-devices-web-team/{device_id}")
130
async def delete_post(device_id: int, team_con: str = Form(...), db: Session = Depends(get_db),
131
                      Authorize: AuthJWT = Depends()):
132
    """
133
    Endpoint called from template from body_device_license.html template, connects device with new team
134
    and redirects to body-devices-web endpoint
135
    """
136
    Authorize.jwt_optional()
137
    current_user = Authorize.get_jwt_subject()
138
    if current_user != "admin":
139
        return RedirectResponse(url=f"/logs-web", status_code=303)
140
    crud.update_bodydevice_team(db, device_id, int(team_con))
141
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
142

    
143

    
144
@body_device_web.post("/body-devices-inv/{device_id}")
145
async def device_inv(device_id: int, dev_inv: str = Form(...), db: Session = Depends(get_db),
146
                     Authorize: AuthJWT = Depends()):
147
    """
148
    Endpoint called from template from body_device_license.html template, updates devices inventory number
149
    and redirects to body-devices-web endpoint
150
    """
151
    Authorize.jwt_optional()
152
    current_user = Authorize.get_jwt_subject()
153
    if current_user != "admin":
154
        return RedirectResponse(url=f"/logs-web", status_code=303)
155
    crud.update_bodydevice_inv(db, device_id, dev_inv)
156
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
157

    
158

    
159
@body_device_web.post("/body-devices-comm/{device_id}")
160
async def device_inv(device_id: int, dev_com: str = Form(...), db: Session = Depends(get_db),
161
                     Authorize: AuthJWT = Depends()):
162
    """
163
    Endpoint called from template from body_device_license.html template, updates devices comment
164
    and redirects to body-devices-web endpoint
165
    """
166
    Authorize.jwt_optional()
167
    current_user = Authorize.get_jwt_subject()
168
    if current_user != "admin":
169
        return RedirectResponse(url=f"/logs-web", status_code=303)
170
    crud.update_bodydevice_comm(db, device_id, dev_com)
171
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
(3-3/16)