Projekt

Obecné

Profil

Stáhnout (6.25 KB) Statistiky
| Větev: | Tag: | Revize:
1 99c92c11 Matej Zeman
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime
2 aba60b70 Matej Zeman
from sqlalchemy.orm import relationship
3 99c92c11 Matej Zeman
from sqlalchemy.sql import func
4 aba60b70 Matej Zeman
from .database import Base
5
6
7
class Device(Base):
8 4911f0ea Matej Zeman
    """
9
    Class defining database table devices
10
    """
11 aba60b70 Matej Zeman
    __tablename__ = "devices"
12
13
    id = Column(Integer, primary_key=True, index=True)
14 99c92c11 Matej Zeman
    vendor_id = Column(String, index=True, nullable=False)
15
    product_id = Column(String, index=True, nullable=False)
16 aba60b70 Matej Zeman
    serial_number = Column(String, index=True, nullable=False)
17 35168e5f zemanm98@students.zcu.cz
    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 aba60b70 Matej Zeman
22 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with usb_logs and licenses
23
    # tables
24 aba60b70 Matej Zeman
    logs = relationship("USBLog", back_populates="device")
25 6c1e92e3 Matej Zeman
    licenses = relationship("DeviceLicense", back_populates="device_lic")
26 35168e5f zemanm98@students.zcu.cz
    team = relationship("Team", back_populates="devices")
27 aba60b70 Matej Zeman
28 6ad54e2e zemanm98@students.zcu.cz
29 aba60b70 Matej Zeman
class USBLog(Base):
30 4911f0ea Matej Zeman
    """
31
    Class defining database table usb_logs
32
    """
33 aba60b70 Matej Zeman
    __tablename__ = "usb_logs"
34
35
    id = Column(Integer, primary_key=True, index=True)
36 6c1e92e3 Matej Zeman
    pc_id = Column(Integer, ForeignKey("pc.id"))
37 99c92c11 Matej Zeman
    timestamp = Column(DateTime(timezone=True), server_default=func.now())
38 aba60b70 Matej Zeman
    status = Column(String, index=True, nullable=False)
39
    device_id = Column(Integer, ForeignKey("devices.id"))
40
41 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with devices and pc
42
    # tables
43 aba60b70 Matej Zeman
    device = relationship("Device", back_populates="logs")
44 6c1e92e3 Matej Zeman
    pc = relationship("PC", back_populates="logs_pc")
45
46
47
class License(Base):
48 4911f0ea Matej Zeman
    """
49
    Class defining database table licenses
50
    """
51 6c1e92e3 Matej Zeman
    __tablename__ = "licenses"
52
53
    id = Column(Integer, primary_key=True, index=True)
54 6ad54e2e zemanm98@students.zcu.cz
    name = Column(String, index=True, nullable=True)
55 35168e5f zemanm98@students.zcu.cz
    license_id = Column(String, index=True, nullable=False)
56
    expiration_date = Column(DateTime(timezone=True), nullable=True)
57 6c1e92e3 Matej Zeman
58 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with devices table
59 6c1e92e3 Matej Zeman
    devices = relationship("DeviceLicense", back_populates="licenses")
60 6ad54e2e zemanm98@students.zcu.cz
    bodydevice_lic = relationship("BodyDevice", back_populates="license")
61
    headdevice_lic = relationship("HeadDevice", back_populates="license")
62
63 6c1e92e3 Matej Zeman
64
class DeviceLicense(Base):
65 4911f0ea Matej Zeman
    """
66
    Class defining database table devices_licenses
67
    """
68 6c1e92e3 Matej Zeman
    __tablename__ = "devices_licenses"
69
70
    id = Column(Integer, primary_key=True, index=True)
71 ef87a1d2 Matej Zeman
    device_id = Column(Integer, ForeignKey("devices.id"))
72
    license_id = Column(Integer, ForeignKey("licenses.id"))
73 6c1e92e3 Matej Zeman
    assigned_datetime = Column(String, index=True, nullable=False)
74
75 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with devices and licenses
76
    # tables
77 6c1e92e3 Matej Zeman
    device_lic = relationship("Device", back_populates="licenses")
78
    licenses = relationship("License", back_populates="devices")
79
80
81
class PC(Base):
82 4911f0ea Matej Zeman
    """
83
    Class defining database table pc
84
    """
85 6c1e92e3 Matej Zeman
    __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 99c92c11 Matej Zeman
91 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with teams, usb_logs and ld_logs
92
    # tables
93 6c1e92e3 Matej Zeman
    logs_pc = relationship("USBLog", back_populates="pc")
94 eaf8ace4 Matej Zeman
    ld_pc = relationship("LDLog", back_populates="ldpc")
95 99c92c11 Matej Zeman
96
97
class Team(Base):
98 4911f0ea Matej Zeman
    """
99
    Class defining database table teams
100
    """
101 99c92c11 Matej Zeman
    __tablename__ = "teams"
102
103
    id = Column(Integer, primary_key=True, index=True)
104
    name = Column(String, index=True, nullable=False)
105 4911f0ea Matej Zeman
106 35168e5f zemanm98@students.zcu.cz
    devices = relationship("Device", back_populates="team")
107 6ad54e2e zemanm98@students.zcu.cz
    body_devices = relationship("BodyDevice", back_populates="team")
108
    head_devices = relationship("HeadDevice", back_populates="team")
109
110 eaf8ace4 Matej Zeman
111
class HeadDevice(Base):
112 4911f0ea Matej Zeman
    """
113
    Class defining database table head_devices
114
    """
115 eaf8ace4 Matej Zeman
    __tablename__ = "head_devices"
116
117
    id = Column(Integer, primary_key=True, index=True)
118
    serial_number = Column(String, index=True, nullable=False)
119 6ad54e2e zemanm98@students.zcu.cz
    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 eaf8ace4 Matej Zeman
125 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with ld_logs table
126 eaf8ace4 Matej Zeman
    h_logs = relationship("LDLog", back_populates="head_device")
127 6ad54e2e zemanm98@students.zcu.cz
    license = relationship("License", back_populates="headdevice_lic")
128
    team = relationship("Team", back_populates="head_devices")
129 eaf8ace4 Matej Zeman
130
131
class BodyDevice(Base):
132 4911f0ea Matej Zeman
    """
133
    Class defining database table body_devices
134
    """
135 eaf8ace4 Matej Zeman
    __tablename__ = "body_devices"
136
137
    id = Column(Integer, primary_key=True, index=True)
138
    serial_number = Column(String, index=True, nullable=False)
139 6ad54e2e zemanm98@students.zcu.cz
    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 eaf8ace4 Matej Zeman
145 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with ld_logs table
146 eaf8ace4 Matej Zeman
    b_logs = relationship("LDLog", back_populates="body_device")
147 6ad54e2e zemanm98@students.zcu.cz
    license = relationship("License", back_populates="bodydevice_lic")
148
    team = relationship("Team", back_populates="body_devices")
149 eaf8ace4 Matej Zeman
150
151
class LDLog(Base):
152 4911f0ea Matej Zeman
    """
153
    Class defining database table ld_logs
154
    """
155 eaf8ace4 Matej Zeman
    __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 4911f0ea Matej Zeman
    # relationships for foreign keys, thus connecting table with pc, head_devices and body_devices
165
    # tables
166 eaf8ace4 Matej Zeman
    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 cbd239c6 Matej Zeman
170 6ad54e2e zemanm98@students.zcu.cz
171 cbd239c6 Matej Zeman
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)