Projekt

Obecné

Profil

Stáhnout (2.06 KB) Statistiky
| Větev: | Tag: | Revize:
1
from typing import List
2

    
3
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
4
from sqlalchemy.orm import Session
5
from datetime import date
6
from sql_app import crud, models, schemas
7
from ..database import SessionLocal, engine
8
from fastapi import FastAPI, Request
9
from fastapi.responses import HTMLResponse
10
from fastapi.staticfiles import StaticFiles
11
from fastapi.templating import Jinja2Templates
12

    
13
models.Base.metadata.create_all(bind=engine)
14
templates = Jinja2Templates(directory="templates/licenses")
15
device_templates = Jinja2Templates(directory="templates/devices")
16

    
17
licenses_web = APIRouter(prefix="/api/v1")
18

    
19

    
20
# Dependency
21
def get_db():
22
    db = SessionLocal()
23
    try:
24
        yield db
25
    finally:
26
        db.close()
27

    
28

    
29
@licenses_web.get("/license-create", response_class=HTMLResponse)
30
async def licenses_create_web(request: Request):
31
    return templates.TemplateResponse("license_create.html", {"request": request})
32

    
33

    
34
@licenses_web.get("/licenses-web", response_class=HTMLResponse)
35
async def read_licenses_web(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
36
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
37
    return templates.TemplateResponse("licenses.html", {"request": request, "licenses": licenses})
38

    
39

    
40
@licenses_web.post("/licenses-web", response_class=HTMLResponse)
41
def create_license(request: Request, name: str = Form(...), expdate: date = Form(...), skip: int = 0, limit: int = 100,
42
                   db: Session = Depends(get_db)):
43
    db_license = crud.create_license(db, name, expdate)
44
    if db_license is None:
45
        print("something went wrong")
46
    devices = crud.get_devices(db, skip=skip, limit=limit)
47
    statuses = []
48
    for i in range(0, len(devices)):
49
        statuses.append(devices[i].logs[len(devices[i].logs) - 1].status)
50
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
51
    return device_templates.TemplateResponse("devices.html", {"request": request, "devs": len(devices), "devices": devices,
52
                                                       "statuses": statuses, "licenses": licenses})
(5-5/11)