1
|
<!doctype html>
|
2
|
<html lang="en">
|
3
|
<head>
|
4
|
<meta charset="utf-8">
|
5
|
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
|
6
|
<meta name="generator" content="pdoc 0.10.0" />
|
7
|
<title>sql_app.api.devices_web API documentation</title>
|
8
|
<meta name="description" content="" />
|
9
|
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
|
10
|
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
|
11
|
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
|
12
|
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
|
13
|
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
|
14
|
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
|
15
|
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
|
16
|
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
|
17
|
</head>
|
18
|
<body>
|
19
|
<main>
|
20
|
<article id="content">
|
21
|
<header>
|
22
|
<h1 class="title">Module <code>sql_app.api.devices_web</code></h1>
|
23
|
</header>
|
24
|
<section id="section-intro">
|
25
|
<details class="source">
|
26
|
<summary>
|
27
|
<span>Expand source code</span>
|
28
|
</summary>
|
29
|
<pre><code class="python">from typing import List
|
30
|
|
31
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
|
32
|
from sqlalchemy.orm import Session
|
33
|
from sql_app import crud, models, schemas
|
34
|
from datetime import datetime
|
35
|
from ..database import SessionLocal, engine
|
36
|
from fastapi import FastAPI, Request
|
37
|
from fastapi.responses import HTMLResponse
|
38
|
from fastapi.staticfiles import StaticFiles
|
39
|
from fastapi.templating import Jinja2Templates
|
40
|
|
41
|
models.Base.metadata.create_all(bind=engine)
|
42
|
|
43
|
# Path to html templates used in this file
|
44
|
templates = Jinja2Templates(directory="../templates/devices")
|
45
|
|
46
|
# prefix used for all endpoints in this file
|
47
|
device_web = APIRouter(prefix="/api/v1")
|
48
|
|
49
|
|
50
|
# Dependency
|
51
|
def get_db():
|
52
|
db = SessionLocal()
|
53
|
try:
|
54
|
yield db
|
55
|
finally:
|
56
|
db.close()
|
57
|
|
58
|
|
59
|
@device_web.get("/devices-web", response_class=HTMLResponse)
|
60
|
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
61
|
"""
|
62
|
Returns template with all devices and its current states
|
63
|
"""
|
64
|
devices = crud.get_devices(db, skip=skip, limit=limit)
|
65
|
statuses = []
|
66
|
# adding state for each device in list
|
67
|
for i in range(0, len(devices)):
|
68
|
statuses.append(devices[i].logs[len(devices[i].logs)-1].status)
|
69
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
70
|
return templates.TemplateResponse("devices.html", {"request": request, "devs": len(devices), "devices": devices,
|
71
|
"statuses": statuses, "licenses": licenses})
|
72
|
|
73
|
|
74
|
@device_web.post("/devices-web", response_class=HTMLResponse)
|
75
|
async def filter_devices(request: Request, skip: int = 0, limit: int = 100, lic: str = Form("all"),
|
76
|
db: Session = Depends(get_db)):
|
77
|
"""
|
78
|
Endpoint used for filtering devices by license. returns html template with only
|
79
|
devices that has assigned license defined by user input
|
80
|
"""
|
81
|
devices = crud.get_devices(db, skip=skip, limit=limit)
|
82
|
def_devices = []
|
83
|
for dev in devices:
|
84
|
for l in dev.licenses:
|
85
|
if dev not in def_devices and l.licenses.name == lic:
|
86
|
def_devices.append(dev)
|
87
|
# if input was default all
|
88
|
if lic == "all":
|
89
|
def_devices = devices
|
90
|
statuses = []
|
91
|
for i in range(0, len(def_devices)):
|
92
|
statuses.append(def_devices[i].logs[len(def_devices[i].logs) - 1].status)
|
93
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
94
|
return templates.TemplateResponse("devices.html", {"request": request, "devs": len(def_devices), "devices": def_devices,
|
95
|
"statuses": statuses, "licenses": licenses})
|
96
|
|
97
|
|
98
|
@device_web.get("/device-license/{device_id}", response_class=HTMLResponse)
|
99
|
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db)):
|
100
|
"""
|
101
|
Returns template with one device and all available licenses that can be assigned to it.
|
102
|
"""
|
103
|
device = crud.get_device(db, device_id)
|
104
|
licenses = crud.get_licenses(db, 0, 100)
|
105
|
return templates.TemplateResponse("devicelicense.html",
|
106
|
{"request": request, "device": device, "licenses": licenses})
|
107
|
|
108
|
|
109
|
@device_web.post("/devices-web/{device_id}", response_class=HTMLResponse)
|
110
|
async def connect_post(request: Request, device_id: int, lic: str = Form(...), skip: int = 0, limit: int = 100,
|
111
|
db: Session = Depends(get_db)):
|
112
|
"""
|
113
|
Endpoint called from template for connecting device with license. Adds entry to devices_licenses
|
114
|
table and returns template with all devices in database
|
115
|
"""
|
116
|
crud.create_device_license(db, device_id, int(lic), datetime.now())
|
117
|
devices = crud.get_devices(db, skip=skip, limit=limit)
|
118
|
statuses = []
|
119
|
# adding state for each device in list
|
120
|
for i in range(0, len(devices)):
|
121
|
statuses.append(devices[i].logs[len(devices[i].logs) - 1].status)
|
122
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
123
|
return templates.TemplateResponse("devices.html", {"request": request, "devs": len(devices), "devices": devices,
|
124
|
"statuses": statuses, "licenses": licenses})</code></pre>
|
125
|
</details>
|
126
|
</section>
|
127
|
<section>
|
128
|
</section>
|
129
|
<section>
|
130
|
</section>
|
131
|
<section>
|
132
|
<h2 class="section-title" id="header-functions">Functions</h2>
|
133
|
<dl>
|
134
|
<dt id="sql_app.api.devices_web.connect_dev_lic"><code class="name flex">
|
135
|
<span>async def <span class="ident">connect_dev_lic</span></span>(<span>request: starlette.requests.Request, device_id: int, db: sqlalchemy.orm.session.Session = Depends(get_db))</span>
|
136
|
</code></dt>
|
137
|
<dd>
|
138
|
<div class="desc"><p>Returns template with one device and all available licenses that can be assigned to it.</p></div>
|
139
|
<details class="source">
|
140
|
<summary>
|
141
|
<span>Expand source code</span>
|
142
|
</summary>
|
143
|
<pre><code class="python">@device_web.get("/device-license/{device_id}", response_class=HTMLResponse)
|
144
|
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db)):
|
145
|
"""
|
146
|
Returns template with one device and all available licenses that can be assigned to it.
|
147
|
"""
|
148
|
device = crud.get_device(db, device_id)
|
149
|
licenses = crud.get_licenses(db, 0, 100)
|
150
|
return templates.TemplateResponse("devicelicense.html",
|
151
|
{"request": request, "device": device, "licenses": licenses})</code></pre>
|
152
|
</details>
|
153
|
</dd>
|
154
|
<dt id="sql_app.api.devices_web.connect_post"><code class="name flex">
|
155
|
<span>async def <span class="ident">connect_post</span></span>(<span>request: starlette.requests.Request, device_id: int, lic: str = Form(Ellipsis), skip: int = 0, limit: int = 100, db: sqlalchemy.orm.session.Session = Depends(get_db))</span>
|
156
|
</code></dt>
|
157
|
<dd>
|
158
|
<div class="desc"><p>Endpoint called from template for connecting device with license. Adds entry to devices_licenses
|
159
|
table and returns template with all devices in database</p></div>
|
160
|
<details class="source">
|
161
|
<summary>
|
162
|
<span>Expand source code</span>
|
163
|
</summary>
|
164
|
<pre><code class="python">@device_web.post("/devices-web/{device_id}", response_class=HTMLResponse)
|
165
|
async def connect_post(request: Request, device_id: int, lic: str = Form(...), skip: int = 0, limit: int = 100,
|
166
|
db: Session = Depends(get_db)):
|
167
|
"""
|
168
|
Endpoint called from template for connecting device with license. Adds entry to devices_licenses
|
169
|
table and returns template with all devices in database
|
170
|
"""
|
171
|
crud.create_device_license(db, device_id, int(lic), datetime.now())
|
172
|
devices = crud.get_devices(db, skip=skip, limit=limit)
|
173
|
statuses = []
|
174
|
# adding state for each device in list
|
175
|
for i in range(0, len(devices)):
|
176
|
statuses.append(devices[i].logs[len(devices[i].logs) - 1].status)
|
177
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
178
|
return templates.TemplateResponse("devices.html", {"request": request, "devs": len(devices), "devices": devices,
|
179
|
"statuses": statuses, "licenses": licenses})</code></pre>
|
180
|
</details>
|
181
|
</dd>
|
182
|
<dt id="sql_app.api.devices_web.filter_devices"><code class="name flex">
|
183
|
<span>async def <span class="ident">filter_devices</span></span>(<span>request: starlette.requests.Request, skip: int = 0, limit: int = 100, lic: str = Form(all), db: sqlalchemy.orm.session.Session = Depends(get_db))</span>
|
184
|
</code></dt>
|
185
|
<dd>
|
186
|
<div class="desc"><p>Endpoint used for filtering devices by license. returns html template with only
|
187
|
devices that has assigned license defined by user input</p></div>
|
188
|
<details class="source">
|
189
|
<summary>
|
190
|
<span>Expand source code</span>
|
191
|
</summary>
|
192
|
<pre><code class="python">@device_web.post("/devices-web", response_class=HTMLResponse)
|
193
|
async def filter_devices(request: Request, skip: int = 0, limit: int = 100, lic: str = Form("all"),
|
194
|
db: Session = Depends(get_db)):
|
195
|
"""
|
196
|
Endpoint used for filtering devices by license. returns html template with only
|
197
|
devices that has assigned license defined by user input
|
198
|
"""
|
199
|
devices = crud.get_devices(db, skip=skip, limit=limit)
|
200
|
def_devices = []
|
201
|
for dev in devices:
|
202
|
for l in dev.licenses:
|
203
|
if dev not in def_devices and l.licenses.name == lic:
|
204
|
def_devices.append(dev)
|
205
|
# if input was default all
|
206
|
if lic == "all":
|
207
|
def_devices = devices
|
208
|
statuses = []
|
209
|
for i in range(0, len(def_devices)):
|
210
|
statuses.append(def_devices[i].logs[len(def_devices[i].logs) - 1].status)
|
211
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
212
|
return templates.TemplateResponse("devices.html", {"request": request, "devs": len(def_devices), "devices": def_devices,
|
213
|
"statuses": statuses, "licenses": licenses})</code></pre>
|
214
|
</details>
|
215
|
</dd>
|
216
|
<dt id="sql_app.api.devices_web.get_db"><code class="name flex">
|
217
|
<span>def <span class="ident">get_db</span></span>(<span>)</span>
|
218
|
</code></dt>
|
219
|
<dd>
|
220
|
<div class="desc"></div>
|
221
|
<details class="source">
|
222
|
<summary>
|
223
|
<span>Expand source code</span>
|
224
|
</summary>
|
225
|
<pre><code class="python">def get_db():
|
226
|
db = SessionLocal()
|
227
|
try:
|
228
|
yield db
|
229
|
finally:
|
230
|
db.close()</code></pre>
|
231
|
</details>
|
232
|
</dd>
|
233
|
<dt id="sql_app.api.devices_web.read_devices"><code class="name flex">
|
234
|
<span>async def <span class="ident">read_devices</span></span>(<span>request: starlette.requests.Request, skip: int = 0, limit: int = 100, db: sqlalchemy.orm.session.Session = Depends(get_db))</span>
|
235
|
</code></dt>
|
236
|
<dd>
|
237
|
<div class="desc"><p>Returns template with all devices and its current states</p></div>
|
238
|
<details class="source">
|
239
|
<summary>
|
240
|
<span>Expand source code</span>
|
241
|
</summary>
|
242
|
<pre><code class="python">@device_web.get("/devices-web", response_class=HTMLResponse)
|
243
|
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
244
|
"""
|
245
|
Returns template with all devices and its current states
|
246
|
"""
|
247
|
devices = crud.get_devices(db, skip=skip, limit=limit)
|
248
|
statuses = []
|
249
|
# adding state for each device in list
|
250
|
for i in range(0, len(devices)):
|
251
|
statuses.append(devices[i].logs[len(devices[i].logs)-1].status)
|
252
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
253
|
return templates.TemplateResponse("devices.html", {"request": request, "devs": len(devices), "devices": devices,
|
254
|
"statuses": statuses, "licenses": licenses})</code></pre>
|
255
|
</details>
|
256
|
</dd>
|
257
|
</dl>
|
258
|
</section>
|
259
|
<section>
|
260
|
</section>
|
261
|
</article>
|
262
|
<nav id="sidebar">
|
263
|
<h1>Index</h1>
|
264
|
<div class="toc">
|
265
|
<ul></ul>
|
266
|
</div>
|
267
|
<ul id="index">
|
268
|
<li><h3>Super-module</h3>
|
269
|
<ul>
|
270
|
<li><code><a title="sql_app.api" href="index.html">sql_app.api</a></code></li>
|
271
|
</ul>
|
272
|
</li>
|
273
|
<li><h3><a href="#header-functions">Functions</a></h3>
|
274
|
<ul class="">
|
275
|
<li><code><a title="sql_app.api.devices_web.connect_dev_lic" href="#sql_app.api.devices_web.connect_dev_lic">connect_dev_lic</a></code></li>
|
276
|
<li><code><a title="sql_app.api.devices_web.connect_post" href="#sql_app.api.devices_web.connect_post">connect_post</a></code></li>
|
277
|
<li><code><a title="sql_app.api.devices_web.filter_devices" href="#sql_app.api.devices_web.filter_devices">filter_devices</a></code></li>
|
278
|
<li><code><a title="sql_app.api.devices_web.get_db" href="#sql_app.api.devices_web.get_db">get_db</a></code></li>
|
279
|
<li><code><a title="sql_app.api.devices_web.read_devices" href="#sql_app.api.devices_web.read_devices">read_devices</a></code></li>
|
280
|
</ul>
|
281
|
</li>
|
282
|
</ul>
|
283
|
</nav>
|
284
|
</main>
|
285
|
<footer id="footer">
|
286
|
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
|
287
|
</footer>
|
288
|
</body>
|
289
|
</html>
|