Projekt

Obecné

Profil

Stáhnout (6.25 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
    inventory_number = Column(String, index=True, nullable=True)
18
    comment = Column(String, index=True, nullable=True)
19

    
20
    team_id = Column(Integer, ForeignKey("teams.id"))
21

    
22
    # relationships for foreign keys, thus connecting table with usb_logs and licenses
23
    # tables
24
    logs = relationship("USBLog", back_populates="device")
25
    licenses = relationship("DeviceLicense", back_populates="device_lic")
26
    team = relationship("Team", back_populates="devices")
27

    
28

    
29
class USBLog(Base):
30
    """
31
    Class defining database table usb_logs
32
    """
33
    __tablename__ = "usb_logs"
34

    
35
    id = Column(Integer, primary_key=True, index=True)
36
    pc_id = Column(Integer, ForeignKey("pc.id"))
37
    timestamp = Column(DateTime(timezone=True), server_default=func.now())
38
    status = Column(String, index=True, nullable=False)
39
    device_id = Column(Integer, ForeignKey("devices.id"))
40

    
41
    # relationships for foreign keys, thus connecting table with devices and pc
42
    # tables
43
    device = relationship("Device", back_populates="logs")
44
    pc = relationship("PC", back_populates="logs_pc")
45

    
46

    
47
class License(Base):
48
    """
49
    Class defining database table licenses
50
    """
51
    __tablename__ = "licenses"
52

    
53
    id = Column(Integer, primary_key=True, index=True)
54
    name = Column(String, index=True, nullable=True)
55
    license_id = Column(String, index=True, nullable=False)
56
    expiration_date = Column(DateTime(timezone=True), nullable=True)
57

    
58
    # relationships for foreign keys, thus connecting table with devices table
59
    devices = relationship("DeviceLicense", back_populates="licenses")
60
    bodydevice_lic = relationship("BodyDevice", back_populates="license")
61
    headdevice_lic = relationship("HeadDevice", back_populates="license")
62

    
63

    
64
class DeviceLicense(Base):
65
    """
66
    Class defining database table devices_licenses
67
    """
68
    __tablename__ = "devices_licenses"
69

    
70
    id = Column(Integer, primary_key=True, index=True)
71
    device_id = Column(Integer, ForeignKey("devices.id"))
72
    license_id = Column(Integer, ForeignKey("licenses.id"))
73
    assigned_datetime = Column(String, index=True, nullable=False)
74

    
75
    # relationships for foreign keys, thus connecting table with devices and licenses
76
    # tables
77
    device_lic = relationship("Device", back_populates="licenses")
78
    licenses = relationship("License", back_populates="devices")
79

    
80

    
81
class PC(Base):
82
    """
83
    Class defining database table pc
84
    """
85
    __tablename__ = "pc"
86

    
87
    id = Column(Integer, primary_key=True, index=True)
88
    username = Column(String, index=True, nullable=False)
89
    hostname = Column(String, index=True, nullable=False)
90

    
91
    # relationships for foreign keys, thus connecting table with teams, usb_logs and ld_logs
92
    # tables
93
    logs_pc = relationship("USBLog", back_populates="pc")
94
    ld_pc = relationship("LDLog", back_populates="ldpc")
95

    
96

    
97
class Team(Base):
98
    """
99
    Class defining database table teams
100
    """
101
    __tablename__ = "teams"
102

    
103
    id = Column(Integer, primary_key=True, index=True)
104
    name = Column(String, index=True, nullable=False)
105

    
106
    devices = relationship("Device", back_populates="team")
107
    body_devices = relationship("BodyDevice", back_populates="team")
108
    head_devices = relationship("HeadDevice", back_populates="team")
109

    
110

    
111
class HeadDevice(Base):
112
    """
113
    Class defining database table head_devices
114
    """
115
    __tablename__ = "head_devices"
116

    
117
    id = Column(Integer, primary_key=True, index=True)
118
    serial_number = Column(String, index=True, nullable=False)
119
    inventory_number = Column(String, index=True, nullable=True)
120
    comment = Column(String, index=True, nullable=True)
121

    
122
    team_id = Column(Integer, ForeignKey("teams.id"))
123
    license_id = Column(Integer, ForeignKey("licenses.id"))
124

    
125
    # relationships for foreign keys, thus connecting table with ld_logs table
126
    h_logs = relationship("LDLog", back_populates="head_device")
127
    license = relationship("License", back_populates="headdevice_lic")
128
    team = relationship("Team", back_populates="head_devices")
129

    
130

    
131
class BodyDevice(Base):
132
    """
133
    Class defining database table body_devices
134
    """
135
    __tablename__ = "body_devices"
136

    
137
    id = Column(Integer, primary_key=True, index=True)
138
    serial_number = Column(String, index=True, nullable=False)
139
    inventory_number = Column(String, index=True, nullable=True)
140
    comment = Column(String, index=True, nullable=True)
141

    
142
    team_id = Column(Integer, ForeignKey("teams.id"))
143
    license_id = Column(Integer, ForeignKey("licenses.id"))
144

    
145
    # relationships for foreign keys, thus connecting table with ld_logs table
146
    b_logs = relationship("LDLog", back_populates="body_device")
147
    license = relationship("License", back_populates="bodydevice_lic")
148
    team = relationship("Team", back_populates="body_devices")
149

    
150

    
151
class LDLog(Base):
152
    """
153
    Class defining database table ld_logs
154
    """
155
    __tablename__ = "ld_logs"
156

    
157
    id = Column(Integer, primary_key=True, index=True)
158
    pc_id = Column(Integer, ForeignKey("pc.id"))
159
    timestamp = Column(DateTime(timezone=True), server_default=func.now())
160
    status = Column(String, index=True, nullable=False)
161
    head_id = Column(Integer, ForeignKey("head_devices.id"))
162
    body_id = Column(Integer, ForeignKey("body_devices.id"))
163

    
164
    # relationships for foreign keys, thus connecting table with pc, head_devices and body_devices
165
    # tables
166
    ldpc = relationship("PC", back_populates="ld_pc")
167
    head_device = relationship("HeadDevice", back_populates="h_logs")
168
    body_device = relationship("BodyDevice", back_populates="b_logs")
169

    
170

    
171
class User(Base):
172
    """
173
    Class defining user in database with its own role
174
    """
175
    __tablename__ = "users"
176

    
177
    id = Column(Integer, primary_key=True, index=True)
178
    username = Column(String, index=True, nullable=False)
179
    password = Column(String, index=True, nullable=False)
180
    role = Column(String, index=True, nullable=False)
(5-5/6)