Revize 99c92c11
Přidáno uživatelem Matěj Zeman před asi 2 roky(ů)
server/sql_app/api/devices.py | ||
---|---|---|
23 | 23 |
db.close() |
24 | 24 |
|
25 | 25 |
|
26 |
@device.get("/devices_web/", response_class=HTMLResponse)
|
|
26 |
@device.get("/devices-web/", response_class=HTMLResponse)
|
|
27 | 27 |
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
28 | 28 |
devices = crud.get_devices(db, skip=skip, limit=limit) |
29 | 29 |
return templates.TemplateResponse("devices.html", {"request": request, "devs": devices}) |
server/sql_app/api/licenses.py | ||
---|---|---|
2 | 2 |
|
3 | 3 |
from fastapi import Depends, FastAPI, HTTPException, APIRouter |
4 | 4 |
from sqlalchemy.orm import Session |
5 |
from datetime import date |
|
5 | 6 |
from ...sql_app import crud, models, schemas |
6 | 7 |
from ..database import SessionLocal, engine |
7 | 8 |
from fastapi import FastAPI, Request |
... | ... | |
24 | 25 |
db.close() |
25 | 26 |
|
26 | 27 |
|
27 |
@licenses.get("/licenses_web/", response_class=HTMLResponse)
|
|
28 |
@licenses.get("/licenses-web/", response_class=HTMLResponse)
|
|
28 | 29 |
async def read_pcs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
29 | 30 |
licenses = crud.get_licenses(db, skip=skip, limit=limit) |
30 | 31 |
return templates.TemplateResponse("licenses.html", {"request": request, "licenses": licenses}) |
... | ... | |
32 | 33 |
|
33 | 34 |
@licenses.post("/license/", response_model=schemas.License) |
34 | 35 |
def create_license(license: schemas.LicenseCreate, db: Session = Depends(get_db)): |
35 |
print(crud.create_license(db=db, license=license))
|
|
36 |
print(crud.create_license(db=db, name=license.name, expdate=license.expiration_date))
|
|
36 | 37 |
|
37 | 38 |
|
38 | 39 |
@licenses.get("/licenses/", response_model=List[schemas.License]) |
... | ... | |
41 | 42 |
return licenses |
42 | 43 |
|
43 | 44 |
|
44 |
@licenses.post("/device_license/", response_model=schemas.DeviceLicense)
|
|
45 |
@licenses.post("/device-license/", response_model=schemas.DeviceLicense)
|
|
45 | 46 |
def create_device_license(device_license: schemas.DeviceLicenseCreate, db: Session = Depends(get_db)): |
46 | 47 |
print(crud.create_device_license(db=db, device_license=device_license)) |
server/sql_app/api/pcs.py | ||
---|---|---|
23 | 23 |
db.close() |
24 | 24 |
|
25 | 25 |
|
26 |
@pcs.get("/pcs_web/", response_class=HTMLResponse)
|
|
26 |
@pcs.get("/pcs-web/", response_class=HTMLResponse)
|
|
27 | 27 |
async def read_pcs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
28 | 28 |
pcs = crud.get_pcs(db, skip=skip, limit=limit) |
29 | 29 |
return templates.TemplateResponse("pcs.html", {"request": request, "pcs": pcs}) |
server/sql_app/api/teams.py | ||
---|---|---|
1 |
from typing import List |
|
2 |
|
|
3 |
from fastapi import Depends, FastAPI, HTTPException, APIRouter |
|
4 |
from sqlalchemy.orm import Session |
|
5 |
from ...sql_app import crud, models, schemas |
|
6 |
from ..database import SessionLocal, engine |
|
7 |
from fastapi import FastAPI, Request |
|
8 |
from fastapi.responses import HTMLResponse |
|
9 |
from fastapi.staticfiles import StaticFiles |
|
10 |
from fastapi.templating import Jinja2Templates |
|
11 |
|
|
12 |
models.Base.metadata.create_all(bind=engine) |
|
13 |
templates = Jinja2Templates(directory="../templates/teams") |
|
14 |
|
|
15 |
teams = APIRouter() |
|
16 |
|
|
17 |
# Dependency |
|
18 |
def get_db(): |
|
19 |
db = SessionLocal() |
|
20 |
try: |
|
21 |
yield db |
|
22 |
finally: |
|
23 |
db.close() |
|
24 |
|
|
25 |
|
|
26 |
@teams.get("/teams-web/", response_class=HTMLResponse) |
|
27 |
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
|
28 |
teams = crud.get_teams(db, skip=skip, limit=limit) |
|
29 |
return templates.TemplateResponse("teams.html", {"request": request, "teams": teams}) |
|
30 |
|
|
31 |
|
|
32 |
@teams.post("/team/", response_model=schemas.Team) |
|
33 |
def create_device(team: schemas.TeamCreate, db: Session = Depends(get_db)): |
|
34 |
print(crud.create_team(db=db, name=team.name)) |
|
35 |
|
|
36 |
|
|
37 |
@teams.get("/teams/", response_model=List[schemas.Device]) |
|
38 |
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
|
39 |
teams = crud.get_teams(db, skip=skip, limit=limit) |
|
40 |
return teams |
|
41 |
|
|
42 |
|
|
43 |
@teams.get("/team/{team_id}", response_model=schemas.Device) |
|
44 |
def read_device(team_id: int, db: Session = Depends(get_db)): |
|
45 |
db_team = crud.get_team(db, team_id=team_id) |
|
46 |
if db_team is None: |
|
47 |
raise HTTPException(status_code=404, detail="Device not found") |
|
48 |
return db_team |
server/sql_app/api/usb_logs.py | ||
---|---|---|
1 | 1 |
from typing import List |
2 | 2 |
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form |
3 |
from datetime import datetime |
|
3 | 4 |
from sqlalchemy.orm import Session |
4 | 5 |
from ...sql_app import crud, models, schemas |
5 | 6 |
from ..database import SessionLocal, engine |
... | ... | |
23 | 24 |
db.close() |
24 | 25 |
|
25 | 26 |
|
26 |
@usblogs.get("/logs_web/", response_class=HTMLResponse)
|
|
27 |
@usblogs.get("/logs-web/", response_class=HTMLResponse)
|
|
27 | 28 |
async def read_logs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
28 | 29 |
logs = crud.get_logs(db, skip=skip, limit=limit) |
29 |
pcs = [] |
|
30 |
for log in logs: |
|
31 |
if log.pc_id not in pcs: |
|
32 |
pcs.append(log.pc_id) |
|
33 | 30 |
return templates.TemplateResponse("logs.html", {"request": request, "logs": logs}) |
34 | 31 |
|
35 | 32 |
|
36 | 33 |
@usblogs.post("/usb-logs/", response_model=schemas.USBLog) |
37 | 34 |
def create_device_logs(log: schemas.USBTempBase, db: Session = Depends(get_db)): |
38 | 35 |
dev = crud.find_device(db, log.device) |
36 |
dat = datetime.strptime(log.timestamp, '%Y-%m-%d %H:%M:%S.%f') |
|
39 | 37 |
if dev is None: |
40 | 38 |
dev = crud.create_device(db=db, device=log.device) |
41 | 39 |
pc = crud.find_pc(db, log.username, log.hostname) |
42 | 40 |
if pc is None: |
43 | 41 |
pc = crud.create_pc(db=db, user=log.username, host=log.hostname) |
44 | 42 |
|
45 |
print(crud.create_device_logs(db=db, item=log, dev_id=dev.id, pc_id=pc.id)) |
|
43 |
print(crud.create_device_logs(db=db, item=log, dev_id=dev.id, pc_id=pc.id, date=dat))
|
|
46 | 44 |
|
47 | 45 |
|
48 | 46 |
@usblogs.get("/logs/", response_model=List[schemas.USBLog]) |
server/sql_app/crud.py | ||
---|---|---|
1 |
from sqlalchemy.orm import Session
|
|
1 |
from datetime import datetime, date
|
|
2 | 2 |
|
3 |
from sqlalchemy.orm import Session |
|
4 |
from sqlalchemy import and_ |
|
3 | 5 |
from . import models, schemas |
4 | 6 |
|
5 | 7 |
|
... | ... | |
12 | 14 |
|
13 | 15 |
|
14 | 16 |
def find_device(db: Session, device: schemas.DeviceBase): |
15 |
return db.query(models.Device).filter(models.Device.product_id == device.product_id and
|
|
16 |
models.Device.vendor_id == device.vendor_id and
|
|
17 |
models.Device.serial_number == device.serial_number).first() |
|
17 |
return db.query(models.Device).filter(and_(models.Device.product_id == device.product_id,
|
|
18 |
models.Device.vendor_id == device.vendor_id,
|
|
19 |
models.Device.serial_number == device.serial_number)).first()
|
|
18 | 20 |
|
19 | 21 |
|
20 | 22 |
def create_device(db: Session, device: schemas.DeviceBase): |
21 | 23 |
db_device = models.Device(vendor_id=device.vendor_id, product_id=device.product_id, |
22 |
serial_number=device.serial_number) |
|
24 |
serial_number=device.serial_number, assigned=False)
|
|
23 | 25 |
db.add(db_device) |
24 | 26 |
db.commit() |
25 | 27 |
db.refresh(db_device) |
... | ... | |
34 | 36 |
return db.query(models.License).offset(skip).limit(limit).all() |
35 | 37 |
|
36 | 38 |
|
37 |
def create_license(db: Session, license: schemas.LicenseBase):
|
|
38 |
db_license = models.License(name=license.name, expiration_date=license.expiration_date)
|
|
39 |
def create_license(db: Session, name: str, expdate: date):
|
|
40 |
db_license = models.License(name=name, expiration_date=expdate)
|
|
39 | 41 |
db.add(db_license) |
40 | 42 |
db.commit() |
41 | 43 |
db.refresh(db_license) |
... | ... | |
60 | 62 |
|
61 | 63 |
|
62 | 64 |
def find_pc(db: Session, username: str, hostname: str): |
63 |
return db.query(models.PC).filter(models.PC.username == username and |
|
64 |
models.PC.hostname == hostname).first() |
|
65 |
return db.query(models.PC).filter(and_(models.PC.username == username, |
|
66 |
models.PC.hostname == hostname)).first() |
|
67 |
|
|
68 |
|
|
69 |
def find_pcs(db: Session, pcs: []): |
|
70 |
return db.query(models.PC).filter(models.PC.id.in_(pcs)).all() |
|
65 | 71 |
|
66 | 72 |
|
67 | 73 |
def create_pc(db: Session, user: str, host: str): |
68 |
db_pc = models.PC(username=user, hostname=host) |
|
74 |
db_pc = models.PC(username=user, hostname=host, assigned=False)
|
|
69 | 75 |
db.add(db_pc) |
70 | 76 |
db.commit() |
71 | 77 |
db.refresh(db_pc) |
72 | 78 |
return db_pc |
73 | 79 |
|
74 | 80 |
|
81 |
def get_team(db: Session, team_id: int): |
|
82 |
return db.query(models.Team).filter(models.Team.id == team_id).first() |
|
83 |
|
|
84 |
|
|
85 |
def get_teams(db: Session, skip: int = 0, limit: int = 100): |
|
86 |
return db.query(models.Team).offset(skip).limit(limit).all() |
|
87 |
|
|
88 |
|
|
89 |
def create_team(db: Session, name: str): |
|
90 |
db_team = models.PC(name=name) |
|
91 |
db.add(db_team) |
|
92 |
db.commit() |
|
93 |
db.refresh(db_team) |
|
94 |
return db_team |
|
95 |
|
|
96 |
|
|
75 | 97 |
def get_logs(db: Session, skip: int = 0, limit: int = 100): |
76 | 98 |
return db.query(models.USBLog).offset(skip).limit(limit).all() |
77 | 99 |
|
... | ... | |
80 | 102 |
return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all() |
81 | 103 |
|
82 | 104 |
|
83 |
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int, pc_id: int): |
|
84 |
db_log = models.USBLog(pc_id=pc_id, timestamp=item.timestamp, status=item.status, device_id=dev_id)
|
|
105 |
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int, pc_id: int, date: datetime):
|
|
106 |
db_log = models.USBLog(pc_id=pc_id, timestamp=date, status=item.status, device_id=dev_id)
|
|
85 | 107 |
db.add(db_log) |
86 | 108 |
db.commit() |
87 | 109 |
db.refresh(db_log) |
88 | 110 |
return db_log |
111 |
|
server/sql_app/main.py | ||
---|---|---|
3 | 3 |
from server.sql_app.api.licenses import licenses |
4 | 4 |
from server.sql_app.api.pcs import pcs |
5 | 5 |
from server.sql_app.api.usb_logs import usblogs |
6 |
from server.sql_app.api.teams import teams |
|
6 | 7 |
from fastapi import FastAPI |
7 | 8 |
|
8 | 9 |
|
... | ... | |
11 | 12 |
app.include_router(licenses) |
12 | 13 |
app.include_router(pcs) |
13 | 14 |
app.include_router(usblogs) |
15 |
app.include_router(teams) |
|
14 | 16 |
|
15 | 17 |
if __name__ == "__main__": |
16 | 18 |
uvicorn.run(app, host="192.168.0.22", port=8000) |
server/sql_app/models.py | ||
---|---|---|
1 |
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String |
|
1 |
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime
|
|
2 | 2 |
from sqlalchemy.orm import relationship |
3 |
|
|
3 |
from sqlalchemy.sql import func |
|
4 | 4 |
from .database import Base |
5 | 5 |
|
6 | 6 |
|
... | ... | |
8 | 8 |
__tablename__ = "devices" |
9 | 9 |
|
10 | 10 |
id = Column(Integer, primary_key=True, index=True) |
11 |
vendor_id = Column(Integer, index=True, nullable=False)
|
|
12 |
product_id = Column(Integer, index=True, nullable=False)
|
|
11 |
vendor_id = Column(String, index=True, nullable=False)
|
|
12 |
product_id = Column(String, index=True, nullable=False)
|
|
13 | 13 |
serial_number = Column(String, index=True, nullable=False) |
14 |
assigned = Column(Boolean, index=True, nullable=False) |
|
14 | 15 |
|
15 | 16 |
logs = relationship("USBLog", back_populates="device") |
16 | 17 |
licenses = relationship("DeviceLicense", back_populates="device_lic") |
... | ... | |
21 | 22 |
|
22 | 23 |
id = Column(Integer, primary_key=True, index=True) |
23 | 24 |
pc_id = Column(Integer, ForeignKey("pc.id")) |
24 |
timestamp = Column(String, index=True, nullable=False)
|
|
25 |
timestamp = Column(DateTime(timezone=True), server_default=func.now())
|
|
25 | 26 |
status = Column(String, index=True, nullable=False) |
26 | 27 |
device_id = Column(Integer, ForeignKey("devices.id")) |
27 | 28 |
|
... | ... | |
34 | 35 |
|
35 | 36 |
id = Column(Integer, primary_key=True, index=True) |
36 | 37 |
name = Column(String, index=True, nullable=False) |
37 |
expiration_date = Column(String, index=True, nullable=False)
|
|
38 |
expiration_date = Column(DateTime(timezone=True), server_default=func.now())
|
|
38 | 39 |
|
39 | 40 |
devices = relationship("DeviceLicense", back_populates="licenses") |
40 | 41 |
|
... | ... | |
43 | 44 |
__tablename__ = "devices_licenses" |
44 | 45 |
|
45 | 46 |
id = Column(Integer, primary_key=True, index=True) |
46 |
device_id = Column(Integer, ForeignKey("devices.id"))
|
|
47 |
license_id = Column(Integer, ForeignKey("licenses.id"))
|
|
47 |
device_id = Column(String, ForeignKey("devices.id"))
|
|
48 |
license_id = Column(String, ForeignKey("licenses.id"))
|
|
48 | 49 |
assigned_datetime = Column(String, index=True, nullable=False) |
49 | 50 |
|
50 | 51 |
device_lic = relationship("Device", back_populates="licenses") |
... | ... | |
57 | 58 |
id = Column(Integer, primary_key=True, index=True) |
58 | 59 |
username = Column(String, index=True, nullable=False) |
59 | 60 |
hostname = Column(String, index=True, nullable=False) |
61 |
assigned = Column(Boolean, index=True, nullable=False) |
|
62 |
team_id = Column(Integer, ForeignKey("teams.id")) |
|
63 |
|
|
64 |
team = relationship("Team", back_populates="pcs") |
|
60 | 65 |
logs_pc = relationship("USBLog", back_populates="pc") |
66 |
|
|
67 |
|
|
68 |
class Team(Base): |
|
69 |
__tablename__ = "teams" |
|
70 |
|
|
71 |
id = Column(Integer, primary_key=True, index=True) |
|
72 |
name = Column(String, index=True, nullable=False) |
|
73 |
pcs = relationship("PC", back_populates="team") |
server/sql_app/schemas.py | ||
---|---|---|
1 | 1 |
from typing import List, Optional |
2 |
|
|
2 |
from datetime import datetime, date |
|
3 | 3 |
from pydantic import BaseModel |
4 | 4 |
|
5 | 5 |
|
6 |
|
|
7 | 6 |
class DeviceLicenseBase(BaseModel): |
8 | 7 |
device_id: int |
9 | 8 |
license_id: int |
... | ... | |
22 | 21 |
|
23 | 22 |
|
24 | 23 |
class USBLogBase(BaseModel): |
25 |
timestamp: str
|
|
24 |
timestamp: datetime
|
|
26 | 25 |
status: str |
27 | 26 |
|
28 | 27 |
|
... | ... | |
40 | 39 |
|
41 | 40 |
|
42 | 41 |
class DeviceBase(BaseModel): |
43 |
vendor_id: int
|
|
44 |
product_id: int
|
|
42 |
vendor_id: str
|
|
43 |
product_id: str
|
|
45 | 44 |
serial_number: str |
46 | 45 |
|
47 | 46 |
|
... | ... | |
49 | 48 |
pass |
50 | 49 |
|
51 | 50 |
|
52 |
class Device(DeviceBase):
|
|
51 |
class Device(DeviceCreate):
|
|
53 | 52 |
id: int |
53 |
assigned: bool |
|
54 | 54 |
logs: List[USBLog] = [] |
55 | 55 |
licenses: List[DeviceLicense] = [] |
56 | 56 |
|
... | ... | |
69 | 69 |
|
70 | 70 |
class PC(PCCreate): |
71 | 71 |
id: int |
72 |
assigned: bool |
|
72 | 73 |
logs_pc: List[USBLog] = [] |
73 | 74 |
|
74 | 75 |
class Config: |
75 | 76 |
orm_mode = True |
76 | 77 |
|
77 | 78 |
|
79 |
class TeamBase(BaseModel): |
|
80 |
name: str |
|
81 |
|
|
82 |
|
|
83 |
class TeamCreate(TeamBase): |
|
84 |
pass |
|
85 |
|
|
86 |
|
|
87 |
class Team(TeamCreate): |
|
88 |
id: int |
|
89 |
pcs: List[PC] = [] |
|
90 |
|
|
91 |
class Config: |
|
92 |
orm_mode = True |
|
93 |
|
|
94 |
|
|
78 | 95 |
class LicenseBase(BaseModel): |
79 | 96 |
name: str |
80 |
expiration_date: str
|
|
97 |
expiration_date: date
|
|
81 | 98 |
|
82 | 99 |
|
83 | 100 |
class LicenseCreate(LicenseBase): |
server/templates/teams/teams.html | ||
---|---|---|
1 |
<html> |
|
2 |
<head> |
|
3 |
<title>Teams Details</title> |
|
4 |
</head> |
|
5 |
<body> |
|
6 |
<table> |
|
7 |
<TR> |
|
8 |
<TH>ID</TH> |
|
9 |
<TH>Name</TH> |
|
10 |
</TR> |
|
11 |
{% for team in teams %} |
|
12 |
<TR> |
|
13 |
<TD class="ID">{{team.id}}</TD> |
|
14 |
<TD class="Vendor ID">{{team.name}}</TD> |
|
15 |
</TR> |
|
16 |
{% endfor %} |
|
17 |
</table> |
|
18 |
</body> |
|
19 |
</html> |
server/templates/usb-logs/logs.html | ||
---|---|---|
6 | 6 |
<table> |
7 | 7 |
<TR> |
8 | 8 |
<TH>ID</TH> |
9 |
<TH>PC ID</TH> |
|
9 |
<TH>PC Username</TH> |
|
10 |
<TH>PC Hostname</TH> |
|
10 | 11 |
<TH>Timestamp</TH> |
11 | 12 |
<TH>Status</TH> |
12 |
<TH>Device ID</TH> |
|
13 |
<TH>Device Product ID</TH> |
|
14 |
<TH>Device Serial Number</TH> |
|
13 | 15 |
</TR> |
14 | 16 |
{% for log in logs %} |
15 | 17 |
<TR> |
16 | 18 |
<TD class="ID">{{log.id}}</TD> |
17 |
<TD class="Username">{{log.pc_id}}</TD> |
|
19 |
<TD class="Username">{{log.pc.username}}</TD> |
|
20 |
<TD class="Hostname">{{log.pc.hostname}}</TD> |
|
18 | 21 |
<TD class="Timestamp">{{log.timestamp}}</TD> |
19 | 22 |
<TD class="Status">{{log.status}}</TD> |
20 |
<TD class="Device ID">{{log.device_id}}</TD> |
|
23 |
<TD class="DeviceProductID">{{log.device.product_id}}</TD> |
|
24 |
<TD class="DeviceSerialNumber">{{log.device.serial_number}}</TD> |
|
21 | 25 |
</TR> |
22 | 26 |
{% endfor %} |
23 | 27 |
</table> |
Také k dispozici: Unified diff
re #9429 Added class and funcionality for Teams table. Modified date parsing and storing. Modified View at /usb-logs/. Replaced "_" with "-" in all endpoints.