Projekt

Obecné

Profil

Stáhnout (7.57 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/head_devices")
18

    
19
# prefix used for all endpoints in this file
20
head_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
@head_device_web.get("/head-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 head devices and necessary attributes
37
    """
38
    Authorize.jwt_optional()
39
    current_user = Authorize.get_jwt_subject()
40

    
41
    device_dict = []
42
    devices = crud.get_head_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.h_logs[len(dev.h_logs) - 1]})
47
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
48
    if current_user == "admin":
49
        return templates.TemplateResponse("head_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("head_devices_normal.html", {"request": request, "devices": device_dict,
55
                                                                "devs": devices, "teams": teams, "licenses": licenses,
56
                                                                "user": current_user})
57

    
58

    
59
@head_device_web.post("/head-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 head devices by user given inputs. returns html template with only
65
    head 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_headdevices(db, body_id, lic_id, team)
71
    ids = []
72
    for d in devices_f:
73
        ids.append(d[0])
74
    devices = crud.get_headdevices_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.h_logs[len(dev.h_logs) - 1]})
79
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
80
    if current_user == "admin":
81
        return templates.TemplateResponse("head_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("head_devices_normal.html", {"request": request, "devices": device_dict,
87
                                                                       "devs": devices, "teams": teams,
88
                                                                       "licenses": licenses,
89
                                                                       "user": current_user})
90

    
91

    
92
@head_device_web.get("/head-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 head device and all available licenses that can be assigned to it, plus team and comment
97
    and inventory number inputs.
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_head_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("headlicense.html",
111
                                      {"request": request, "device": device, "licenses": lic_left, "teams": teams})
112

    
113

    
114
@head_device_web.post("/head-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 for connecting head device with license and redirects to head-devices-web endpoint
119
    """
120
    Authorize.jwt_optional()
121
    current_user = Authorize.get_jwt_subject()
122
    if current_user != "admin":
123
        return RedirectResponse(url=f"/logs-web", status_code=303)
124
    crud.update_headdevice_license(db, device_id, int(lic))
125
    return RedirectResponse(url=f"/head-devices-web", status_code=303)
126

    
127

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

    
141

    
142
@head_device_web.post("/head-devices-inv/{device_id}")
143
async def device_inv(device_id: int, dev_inv: str = Form(...), db: Session = Depends(get_db),
144
                     Authorize: AuthJWT = Depends()):
145
    """
146
    Endpoint called from within from headlicense.html template. Changes head devices inventory number with new one
147
    given from user
148
    """
149
    Authorize.jwt_optional()
150
    current_user = Authorize.get_jwt_subject()
151
    if current_user != "admin":
152
        return RedirectResponse(url=f"/logs-web", status_code=303)
153
    crud.update_headdevice_inv(db, device_id, dev_inv)
154
    return RedirectResponse(url=f"/head-devices-web", status_code=303)
155

    
156

    
157
@head_device_web.post("/head-devices-comm/{device_id}")
158
async def device_inv(device_id: int, dev_com: str = Form(...), db: Session = Depends(get_db),
159
                     Authorize: AuthJWT = Depends()):
160
    """
161
    Endpoint called from within from headlicense.html template. Changes head devices comment with new one
162
    given from user
163
    """
164
    Authorize.jwt_optional()
165
    current_user = Authorize.get_jwt_subject()
166
    if current_user != "admin":
167
        return RedirectResponse(url=f"/logs-web", status_code=303)
168
    crud.update_headdevice_comm(db, device_id, dev_com)
169
    return RedirectResponse(url=f"/head-devices-web", status_code=303)
(6-6/16)