Revize 4911f0ea
Přidáno uživatelem Matěj Zeman před asi 2 roky(ů)
server/sql_app/crud.py | ||
---|---|---|
6 | 6 |
|
7 | 7 |
|
8 | 8 |
def get_device(db: Session, device_id: int): |
9 |
""" |
|
10 |
returns one specific devices by given id |
|
11 |
""" |
|
9 | 12 |
return db.query(models.Device).filter(models.Device.id == device_id).first() |
10 | 13 |
|
11 | 14 |
|
12 | 15 |
def get_devices(db: Session, skip: int = 0, limit: int = 100): |
16 |
""" |
|
17 |
returns all devices in database |
|
18 |
""" |
|
13 | 19 |
return db.query(models.Device).offset(skip).limit(limit).all() |
14 | 20 |
|
15 | 21 |
|
16 | 22 |
def find_device(db: Session, device: schemas.DeviceBase): |
23 |
""" |
|
24 |
finds one device with product_id, vendor_id and serial_number same as in given DeviceBase object |
|
25 |
""" |
|
17 | 26 |
return db.query(models.Device).filter(and_(models.Device.product_id == device.product_id, |
18 | 27 |
models.Device.vendor_id == device.vendor_id, |
19 | 28 |
models.Device.serial_number == device.serial_number)).first() |
20 | 29 |
|
21 | 30 |
|
22 | 31 |
def create_device(db: Session, device: schemas.DeviceBase): |
32 |
""" |
|
33 |
creates new device with data from given DeviceBase object |
|
34 |
""" |
|
23 | 35 |
db_device = models.Device(vendor_id=device.vendor_id, product_id=device.product_id, |
24 | 36 |
serial_number=device.serial_number, assigned=False) |
25 | 37 |
db.add(db_device) |
... | ... | |
29 | 41 |
|
30 | 42 |
|
31 | 43 |
def get_license(db: Session, license_id: int): |
44 |
""" |
|
45 |
returns one specific license by given id |
|
46 |
""" |
|
32 | 47 |
return db.query(models.License).filter(models.License.id == license_id).first() |
33 | 48 |
|
34 | 49 |
|
35 | 50 |
def get_licenses(db: Session, skip: int = 0, limit: int = 100): |
51 |
""" |
|
52 |
returns all licenses in database |
|
53 |
""" |
|
36 | 54 |
return db.query(models.License).offset(skip).limit(limit).all() |
37 | 55 |
|
38 | 56 |
|
39 | 57 |
def find_license(db: Session, name: str): |
58 |
""" |
|
59 |
finds one license by given string name |
|
60 |
""" |
|
40 | 61 |
return db.query(models.License).filter(models.License.name == name).first() |
41 | 62 |
|
42 | 63 |
|
43 | 64 |
def create_license(db: Session, name: str, expdate: date): |
65 |
""" |
|
66 |
creates new license with given name and expiration date |
|
67 |
""" |
|
44 | 68 |
db_license = models.License(name=name, expiration_date=expdate) |
45 | 69 |
db.add(db_license) |
46 | 70 |
db.commit() |
... | ... | |
49 | 73 |
|
50 | 74 |
|
51 | 75 |
def get_license_devices(db: Session, license_id: int): |
76 |
""" |
|
77 |
returns all entries in devices_licenses table |
|
78 |
""" |
|
52 | 79 |
return db.query(models.DeviceLicense).filter(models.DeviceLicense.license_id == license_id).all() |
53 | 80 |
|
54 | 81 |
|
55 | 82 |
def create_device_license(db: Session, device: int, license: int, time: datetime): |
83 |
""" |
|
84 |
creates new entry in devices_licenses table with device id, license id and time. |
|
85 |
""" |
|
56 | 86 |
db_device_license = models.DeviceLicense(device_id=device, license_id=license, |
57 | 87 |
assigned_datetime=time) |
58 | 88 |
db.add(db_device_license) |
... | ... | |
62 | 92 |
|
63 | 93 |
|
64 | 94 |
def find_pc_by_username(db: Session, name: str): |
95 |
""" |
|
96 |
Finds one pc by given username |
|
97 |
""" |
|
65 | 98 |
return db.query(models.PC).filter(models.PC.username == name).first() |
66 | 99 |
|
67 | 100 |
|
68 | 101 |
def get_pc(db: Session, pc_id: int): |
102 |
""" |
|
103 |
returns one specific pc by given id |
|
104 |
""" |
|
69 | 105 |
return db.query(models.PC).filter(models.PC.id == pc_id).first() |
70 | 106 |
|
71 | 107 |
|
72 | 108 |
def update_pc(db: Session, pc_id: int, team: str): |
109 |
""" |
|
110 |
Function updates team of one specific pc |
|
111 |
""" |
|
73 | 112 |
old_pc = get_pc(db, pc_id) |
74 | 113 |
team = get_team(db, int(team)) |
75 | 114 |
new = {'id': old_pc.id, 'username': old_pc.username, 'hostname': old_pc.hostname, 'assigned': True, |
... | ... | |
82 | 121 |
|
83 | 122 |
|
84 | 123 |
def get_pcs(db: Session, skip: int = 0, limit: int = 100): |
124 |
""" |
|
125 |
returns all pcs in database |
|
126 |
""" |
|
85 | 127 |
return db.query(models.PC).offset(skip).limit(limit).all() |
86 | 128 |
|
87 | 129 |
|
88 | 130 |
def find_pc(db: Session, username: str, hostname: str): |
131 |
""" |
|
132 |
Finds one pc with given username and hostname |
|
133 |
""" |
|
89 | 134 |
return db.query(models.PC).filter(and_(models.PC.username == username, |
90 | 135 |
models.PC.hostname == hostname)).first() |
91 | 136 |
|
92 | 137 |
|
93 | 138 |
def find_pc_by_name(db: Session, username: str): |
139 |
""" |
|
140 |
Finds one pc by its username |
|
141 |
""" |
|
94 | 142 |
return db.query(models.PC).filter(models.PC.username == username).first() |
95 | 143 |
|
96 | 144 |
|
97 | 145 |
def find_pc_by_name_all(db: Session, username: str): |
146 |
""" |
|
147 |
Finds all pcs with same username |
|
148 |
""" |
|
98 | 149 |
return db.query(models.PC).filter(models.PC.username == username).offset(0).limit(100).all() |
99 | 150 |
|
100 | 151 |
|
101 | 152 |
def find_pcs(db: Session, pcs: []): |
153 |
""" |
|
154 |
Finds all pcs with ids in given id array |
|
155 |
""" |
|
102 | 156 |
return db.query(models.PC).filter(models.PC.id.in_(pcs)).all() |
103 | 157 |
|
104 | 158 |
|
105 | 159 |
def get_pcs_by_team(db: Session, team_id: int): |
160 |
""" |
|
161 |
returns all pcs in given team by team id |
|
162 |
""" |
|
106 | 163 |
return db.query(models.PC).filter(models.PC.team_id == team_id).all() |
107 | 164 |
|
108 | 165 |
|
109 | 166 |
def create_pc(db: Session, user: str, host: str): |
167 |
""" |
|
168 |
creates new pc with given username and hostname |
|
169 |
""" |
|
110 | 170 |
db_pc = models.PC(username=user, hostname=host, assigned=False) |
111 | 171 |
db.add(db_pc) |
112 | 172 |
db.commit() |
... | ... | |
115 | 175 |
|
116 | 176 |
|
117 | 177 |
def get_team(db: Session, team_id: int): |
178 |
""" |
|
179 |
returns one specific team wit given id |
|
180 |
""" |
|
118 | 181 |
return db.query(models.Team).filter(models.Team.id == team_id).first() |
119 | 182 |
|
120 | 183 |
|
121 | 184 |
def get_teams(db: Session, skip: int = 0, limit: int = 100): |
185 |
""" |
|
186 |
returns all teams currently saved in database |
|
187 |
""" |
|
122 | 188 |
return db.query(models.Team).offset(skip).limit(limit).all() |
123 | 189 |
|
124 | 190 |
|
125 | 191 |
def find_team(db: Session, name: str): |
192 |
""" |
|
193 |
Finds one specific team by its name |
|
194 |
""" |
|
126 | 195 |
return db.query(models.Team).filter(models.Team.name == name).first() |
127 | 196 |
|
128 | 197 |
|
129 | 198 |
def create_team(db: Session, name: str): |
199 |
""" |
|
200 |
Creates new team with given name |
|
201 |
""" |
|
130 | 202 |
db_team = models.Team(name=name) |
131 | 203 |
db.add(db_team) |
132 | 204 |
db.commit() |
... | ... | |
135 | 207 |
|
136 | 208 |
|
137 | 209 |
def get_head_device(db: Session, head_id: int): |
210 |
""" |
|
211 |
Returns one specific head device by given id |
|
212 |
""" |
|
138 | 213 |
return db.query(models.HeadDevice).filter(models.HeadDevice.id == head_id).first() |
139 | 214 |
|
140 | 215 |
|
141 | 216 |
def get_head_devices(db: Session, skip: int = 0, limit: int = 100): |
217 |
""" |
|
218 |
Returns all head devices saved in database |
|
219 |
""" |
|
142 | 220 |
return db.query(models.HeadDevice).offset(skip).limit(limit).all() |
143 | 221 |
|
144 | 222 |
|
145 | 223 |
def find_head_device(db: Session, serial: schemas.HeadDeviceBase): |
224 |
""" |
|
225 |
Finds one head device by its serial number |
|
226 |
""" |
|
146 | 227 |
return db.query(models.HeadDevice).filter(models.HeadDevice.serial_number == serial.serial_number).first() |
147 | 228 |
|
148 | 229 |
|
149 | 230 |
def create_head_device(db: Session, log: schemas.HeadDeviceBase): |
231 |
""" |
|
232 |
Creates new head device |
|
233 |
""" |
|
150 | 234 |
db_head = models.HeadDevice(serial_number=log.serial_number) |
151 | 235 |
db.add(db_head) |
152 | 236 |
db.commit() |
... | ... | |
155 | 239 |
|
156 | 240 |
|
157 | 241 |
def get_body_device(db: Session, body_id: int): |
242 |
""" |
|
243 |
Returns one specific body device by given id |
|
244 |
""" |
|
158 | 245 |
return db.query(models.BodyDevice).filter(models.BodyDevice.id == body_id).first() |
159 | 246 |
|
160 | 247 |
|
161 | 248 |
def get_body_devices(db: Session, skip: int = 0, limit: int = 100): |
249 |
""" |
|
250 |
Returns all body devices saved in database |
|
251 |
""" |
|
162 | 252 |
return db.query(models.BodyDevice).offset(skip).limit(limit).all() |
163 | 253 |
|
164 | 254 |
|
165 | 255 |
def find_body_device(db: Session, serial: schemas.BodyDeviceBase): |
256 |
""" |
|
257 |
Finds one body device by its serial number |
|
258 |
""" |
|
166 | 259 |
return db.query(models.BodyDevice).filter(models.BodyDevice.serial_number == serial.serial_number).first() |
167 | 260 |
|
168 | 261 |
|
169 | 262 |
def create_body_device(db: Session, log: schemas.BodyDeviceBase): |
263 |
""" |
|
264 |
Creates new Body device |
|
265 |
""" |
|
170 | 266 |
db_body = models.BodyDevice(serial_number=log.serial_number) |
171 | 267 |
db.add(db_body) |
172 | 268 |
db.commit() |
... | ... | |
175 | 271 |
|
176 | 272 |
|
177 | 273 |
def get_ld_logs(db: Session, skip: int = 0, limit: int = 100): |
274 |
""" |
|
275 |
Returns all ld debugger logs in database |
|
276 |
""" |
|
178 | 277 |
return db.query(models.LDLog).offset(skip).limit(limit).all() |
179 | 278 |
|
180 | 279 |
|
181 | 280 |
def create_ld_logs(db: Session, item: schemas.LDTempBase, head_id: int, body_id: int, pc_id: int, date: datetime): |
281 |
""" |
|
282 |
Creates new ld log for ld_logs database table |
|
283 |
""" |
|
182 | 284 |
db_ld = models.LDLog(pc_id=pc_id, timestamp=date, status=item.status, head_id=head_id, body_id=body_id) |
183 | 285 |
db.add(db_ld) |
184 | 286 |
db.commit() |
... | ... | |
187 | 289 |
|
188 | 290 |
|
189 | 291 |
def get_logs(db: Session, skip: int = 0, limit: int = 100): |
292 |
""" |
|
293 |
Returns all usb logs in database ordered by timestamp |
|
294 |
""" |
|
190 | 295 |
return db.query(models.USBLog).order_by(desc(models.USBLog.timestamp)).offset(skip).limit(limit).all() |
191 | 296 |
|
192 | 297 |
|
193 | 298 |
def get_log(db: Session, device_id: int, skip: int = 0, limit: int = 100): |
299 |
""" |
|
300 |
Returns all usb logs in database sorted by id |
|
301 |
""" |
|
194 | 302 |
return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all() |
195 | 303 |
|
196 | 304 |
|
197 | 305 |
def find_filtered_logs(db: Session, logs: []): |
306 |
""" |
|
307 |
Returns all usb logs with ids in given id array. |
|
308 |
""" |
|
198 | 309 |
return db.query(models.USBLog).filter(models.USBLog.id.in_(logs)).order_by(desc(models.USBLog.timestamp)).all() |
199 | 310 |
|
200 | 311 |
|
201 | 312 |
def get_filtered_logs(db: Session, pc: str, tema: str, lic: str): |
313 |
""" |
|
314 |
Function creates query string used for filtering by pc username, team name and license name. |
|
315 |
Depending on selected filters assembles query string for database |
|
316 |
""" |
|
202 | 317 |
execute_string = "SELECT * FROM usb_logs AS logs" |
203 | 318 |
if pc != "all": |
204 | 319 |
pcs = find_pc_by_username(db, pc) |
... | ... | |
226 | 341 |
else: |
227 | 342 |
execute_string += " WHERE logs.device_id IN " + defin_ids |
228 | 343 |
|
344 |
# executing assembled query string |
|
229 | 345 |
result = db.execute(execute_string) |
230 | 346 |
return result |
231 | 347 |
|
232 | 348 |
|
233 | 349 |
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int, pc_id: int, date: datetime): |
350 |
""" |
|
351 |
Creates new USB log for usb_logs database table |
|
352 |
""" |
|
234 | 353 |
db_log = models.USBLog(pc_id=pc_id, timestamp=date, status=item.status, device_id=dev_id) |
235 | 354 |
db.add(db_log) |
236 | 355 |
db.commit() |
Také k dispozici: Unified diff
re #9577 Api code documentation.