Projekt

Obecné

Profil

Stáhnout (3.6 KB) Statistiky
| Větev: | Tag: | Revize:
1 99c92c11 Matej Zeman
from datetime import datetime, date
2 aba60b70 Matej Zeman
3 99c92c11 Matej Zeman
from sqlalchemy.orm import Session
4
from sqlalchemy import and_
5 aba60b70 Matej Zeman
from . import models, schemas
6
7
8
def get_device(db: Session, device_id: int):
9
    return db.query(models.Device).filter(models.Device.id == device_id).first()
10
11
12
def get_devices(db: Session, skip: int = 0, limit: int = 100):
13
    return db.query(models.Device).offset(skip).limit(limit).all()
14
15
16
def find_device(db: Session, device: schemas.DeviceBase):
17 99c92c11 Matej Zeman
    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()
20 aba60b70 Matej Zeman
21
22
def create_device(db: Session, device: schemas.DeviceBase):
23
    db_device = models.Device(vendor_id=device.vendor_id, product_id=device.product_id,
24 99c92c11 Matej Zeman
                              serial_number=device.serial_number, assigned=False)
25 aba60b70 Matej Zeman
    db.add(db_device)
26
    db.commit()
27
    db.refresh(db_device)
28
    return db_device
29
30
31 6c1e92e3 Matej Zeman
def get_license(db: Session, license_id: int):
32
    return db.query(models.License).filter(models.License.id == license_id).first()
33
34
35
def get_licenses(db: Session, skip: int = 0, limit: int = 100):
36
    return db.query(models.License).offset(skip).limit(limit).all()
37
38
39 99c92c11 Matej Zeman
def create_license(db: Session, name: str, expdate: date):
40
    db_license = models.License(name=name, expiration_date=expdate)
41 6c1e92e3 Matej Zeman
    db.add(db_license)
42
    db.commit()
43
    db.refresh(db_license)
44
    return db_license
45
46
47
def create_device_license(db: Session, device_license: schemas.DeviceLicenseBase):
48
    db_device_license = models.DeviceLicense(device_id=device_license.device_id, license_id=device_license.license_id,
49
                                             assigned_datetime=device_license.assigned_datetime)
50
    db.add(db_device_license)
51
    db.commit()
52
    db.refresh(db_device_license)
53
    return db_device_license
54
55
56
def get_pc(db: Session, pc_id: int):
57
    return db.query(models.PC).filter(models.PC.id == pc_id).first()
58
59
60
def get_pcs(db: Session, skip: int = 0, limit: int = 100):
61
    return db.query(models.PC).offset(skip).limit(limit).all()
62
63
64
def find_pc(db: Session, username: str, hostname: str):
65 99c92c11 Matej Zeman
    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()
71 6c1e92e3 Matej Zeman
72
73
def create_pc(db: Session, user: str, host: str):
74 99c92c11 Matej Zeman
    db_pc = models.PC(username=user, hostname=host, assigned=False)
75 6c1e92e3 Matej Zeman
    db.add(db_pc)
76
    db.commit()
77
    db.refresh(db_pc)
78
    return db_pc
79
80
81 99c92c11 Matej Zeman
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
97 aba60b70 Matej Zeman
def get_logs(db: Session, skip: int = 0, limit: int = 100):
98
    return db.query(models.USBLog).offset(skip).limit(limit).all()
99
100
101
def get_log(db: Session, device_id: int, skip: int = 0, limit: int = 100):
102
    return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all()
103
104
105 99c92c11 Matej Zeman
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)
107 aba60b70 Matej Zeman
    db.add(db_log)
108
    db.commit()
109
    db.refresh(db_log)
110
    return db_log