Projekt

Obecné

Profil

Stáhnout (8.34 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, "body_val": "", "lic_val": "",
52
                                                                "team_val": ""})
53
    else:
54
        current_user = "guest"
55
        return templates.TemplateResponse("body_devices_normal.html", {"request": request, "devices": device_dict,
56
                                                                "devs": devices, "teams": teams, "licenses": licenses,
57
                                                                "user": current_user, "body_val": "", "lic_val": "",
58
                                                                "team_val": ""})
59

    
60

    
61
@body_device_web.post("/body-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 body devices by user given inputs. returns html template with only
67
    body 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_bodydevices(db, body_id, lic_id, team)
73
    ids = []
74
    for d in devices_f:
75
        ids.append(d[0])
76
    devices = crud.get_bodydevices_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.b_logs[len(dev.b_logs) - 1]})
81
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
82
    if body_id == "all":
83
        body_id = ""
84
    if lic_id == "all":
85
        lic_id = ""
86
    if team == "all":
87
        team = ""
88
    if current_user == "admin":
89
        return templates.TemplateResponse("body_devices.html", {"request": request, "devices": device_dict,
90
                                                                "devs": devices, "teams": teams, "licenses": licenses,
91
                                                                "user": current_user, "body_val": body_id, "lic_val": lic_id,
92
                                                                "team_val": team})
93
    else:
94
        current_user = "guest"
95
        return templates.TemplateResponse("body_devices_normal.html", {"request": request, "devices": device_dict,
96
                                                                       "devs": devices, "teams": teams,
97
                                                                       "licenses": licenses,
98
                                                                       "user": current_user, "body_val": body_id, "lic_val": lic_id,
99
                                                                       "team_val": team})
100

    
101

    
102
@body_device_web.get("/body-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 body device and all available licenses that can be assigned to it. Plus available teams
107
    that can be assigned to device, inventory number and comment text input for this device.
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_body_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("body_device_license.html",
121
                                      {"request": request, "device": device, "licenses": lic_left, "teams": teams})
122

    
123

    
124
@body_device_web.post("/body-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 from body_device_license.html template. Connects body device with license
129
    and redirects to body-devices-web endpoint
130
    """
131
    Authorize.jwt_optional()
132
    current_user = Authorize.get_jwt_subject()
133
    if current_user != "admin":
134
        return RedirectResponse(url=f"/logs-web", status_code=303)
135
    crud.update_bodydevice_license(db, device_id, int(lic))
136
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
137

    
138

    
139
@body_device_web.post("/body-devices-web-team/{device_id}")
140
async def delete_post(device_id: int, team_con: str = Form(...), db: Session = Depends(get_db),
141
                      Authorize: AuthJWT = Depends()):
142
    """
143
    Endpoint called from template from body_device_license.html template, connects device with new team
144
    and redirects to body-devices-web endpoint
145
    """
146
    Authorize.jwt_optional()
147
    current_user = Authorize.get_jwt_subject()
148
    if current_user != "admin":
149
        return RedirectResponse(url=f"/logs-web", status_code=303)
150
    crud.update_bodydevice_team(db, device_id, int(team_con))
151
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
152

    
153

    
154
@body_device_web.post("/body-devices-inv/{device_id}")
155
async def device_inv(device_id: int, dev_inv: str = Form(...), db: Session = Depends(get_db),
156
                     Authorize: AuthJWT = Depends()):
157
    """
158
    Endpoint called from template from body_device_license.html template, updates devices inventory number
159
    and redirects to body-devices-web endpoint
160
    """
161
    Authorize.jwt_optional()
162
    current_user = Authorize.get_jwt_subject()
163
    if current_user != "admin":
164
        return RedirectResponse(url=f"/logs-web", status_code=303)
165
    crud.update_bodydevice_inv(db, device_id, dev_inv)
166
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
167

    
168

    
169
@body_device_web.post("/body-devices-comm/{device_id}")
170
async def device_inv(device_id: int, dev_com: str = Form(...), db: Session = Depends(get_db),
171
                     Authorize: AuthJWT = Depends()):
172
    """
173
    Endpoint called from template from body_device_license.html template, updates devices comment
174
    and redirects to body-devices-web endpoint
175
    """
176
    Authorize.jwt_optional()
177
    current_user = Authorize.get_jwt_subject()
178
    if current_user != "admin":
179
        return RedirectResponse(url=f"/logs-web", status_code=303)
180
    crud.update_bodydevice_comm(db, device_id, dev_com)
181
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
(3-3/16)