Projekt

Obecné

Profil

Stáhnout (4.92 KB) Statistiky
| Větev: | Tag: | Revize:
1
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime
2
from sqlalchemy.orm import relationship
3
from sqlalchemy.sql import func
4
from .database import Base
5

    
6

    
7
class Device(Base):
8
    """
9
    Class defining database table devices
10
    """
11
    __tablename__ = "devices"
12

    
13
    id = Column(Integer, primary_key=True, index=True)
14
    vendor_id = Column(String, index=True, nullable=False)
15
    product_id = Column(String, index=True, nullable=False)
16
    serial_number = Column(String, index=True, nullable=False)
17
    assigned = Column(Boolean, index=True, nullable=False)
18

    
19
    # relationships for foreign keys, thus connecting table with usb_logs and licenses
20
    # tables
21
    logs = relationship("USBLog", back_populates="device")
22
    licenses = relationship("DeviceLicense", back_populates="device_lic")
23

    
24

    
25
class USBLog(Base):
26
    """
27
    Class defining database table usb_logs
28
    """
29
    __tablename__ = "usb_logs"
30

    
31
    id = Column(Integer, primary_key=True, index=True)
32
    pc_id = Column(Integer, ForeignKey("pc.id"))
33
    timestamp = Column(DateTime(timezone=True), server_default=func.now())
34
    status = Column(String, index=True, nullable=False)
35
    device_id = Column(Integer, ForeignKey("devices.id"))
36

    
37
    # relationships for foreign keys, thus connecting table with devices and pc
38
    # tables
39
    device = relationship("Device", back_populates="logs")
40
    pc = relationship("PC", back_populates="logs_pc")
41

    
42

    
43
class License(Base):
44
    """
45
    Class defining database table licenses
46
    """
47
    __tablename__ = "licenses"
48

    
49
    id = Column(Integer, primary_key=True, index=True)
50
    name = Column(String, index=True, nullable=False)
51
    expiration_date = Column(DateTime(timezone=True), server_default=func.now())
52

    
53
    # relationships for foreign keys, thus connecting table with devices table
54
    devices = relationship("DeviceLicense", back_populates="licenses")
55

    
56

    
57
class DeviceLicense(Base):
58
    """
59
    Class defining database table devices_licenses
60
    """
61
    __tablename__ = "devices_licenses"
62

    
63
    id = Column(Integer, primary_key=True, index=True)
64
    device_id = Column(Integer, ForeignKey("devices.id"))
65
    license_id = Column(Integer, ForeignKey("licenses.id"))
66
    assigned_datetime = Column(String, index=True, nullable=False)
67

    
68
    # relationships for foreign keys, thus connecting table with devices and licenses
69
    # tables
70
    device_lic = relationship("Device", back_populates="licenses")
71
    licenses = relationship("License", back_populates="devices")
72

    
73

    
74
class PC(Base):
75
    """
76
    Class defining database table pc
77
    """
78
    __tablename__ = "pc"
79

    
80
    id = Column(Integer, primary_key=True, index=True)
81
    username = Column(String, index=True, nullable=False)
82
    hostname = Column(String, index=True, nullable=False)
83
    assigned = Column(Boolean, index=True, nullable=False)
84
    team_id = Column(Integer, ForeignKey("teams.id"))
85

    
86
    # relationships for foreign keys, thus connecting table with teams, usb_logs and ld_logs
87
    # tables
88
    team = relationship("Team", back_populates="pcs")
89
    logs_pc = relationship("USBLog", back_populates="pc")
90
    ld_pc = relationship("LDLog", back_populates="ldpc")
91

    
92

    
93
class Team(Base):
94
    """
95
    Class defining database table teams
96
    """
97
    __tablename__ = "teams"
98

    
99
    id = Column(Integer, primary_key=True, index=True)
100
    name = Column(String, index=True, nullable=False)
101

    
102
    # relationships for foreign keys, thus connecting table with pc table
103
    pcs = relationship("PC", back_populates="team")
104

    
105

    
106
class HeadDevice(Base):
107
    """
108
    Class defining database table head_devices
109
    """
110
    __tablename__ = "head_devices"
111

    
112
    id = Column(Integer, primary_key=True, index=True)
113
    serial_number = Column(String, index=True, nullable=False)
114

    
115
    # relationships for foreign keys, thus connecting table with ld_logs table
116
    h_logs = relationship("LDLog", back_populates="head_device")
117

    
118

    
119
class BodyDevice(Base):
120
    """
121
    Class defining database table body_devices
122
    """
123
    __tablename__ = "body_devices"
124

    
125
    id = Column(Integer, primary_key=True, index=True)
126
    serial_number = Column(String, index=True, nullable=False)
127

    
128
    # relationships for foreign keys, thus connecting table with ld_logs table
129
    b_logs = relationship("LDLog", back_populates="body_device")
130

    
131

    
132
class LDLog(Base):
133
    """
134
    Class defining database table ld_logs
135
    """
136
    __tablename__ = "ld_logs"
137

    
138
    id = Column(Integer, primary_key=True, index=True)
139
    pc_id = Column(Integer, ForeignKey("pc.id"))
140
    timestamp = Column(DateTime(timezone=True), server_default=func.now())
141
    status = Column(String, index=True, nullable=False)
142
    head_id = Column(Integer, ForeignKey("head_devices.id"))
143
    body_id = Column(Integer, ForeignKey("body_devices.id"))
144

    
145
    # relationships for foreign keys, thus connecting table with pc, head_devices and body_devices
146
    # tables
147
    ldpc = relationship("PC", back_populates="ld_pc")
148
    head_device = relationship("HeadDevice", back_populates="h_logs")
149
    body_device = relationship("BodyDevice", back_populates="b_logs")
(5-5/6)