source: src/mapservice/server_impl/controllers/DevicesController.py@ 8818b65

debian/bullseye
Last change on this file since 8818b65 was 7be94aa, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Added yapf style config (src/python-style.yapf)
    • Updated all files to the yapf formatted version
  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[ec8d5d0]1'''
2 Copyright (C) 2019 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16'''
17import connexion
18
19from ..database.Model import User, Device, db
[1cd45dd]20from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
[ec8d5d0]21
[7be94aa]22
[ec8d5d0]23def devices_get(name=None, order_by=None, offset=None, limit=None):
[7be94aa]24 query = None
25
26 user = User.loggedin_user()
27 if user is None:
28 raise MapserviceUnauthorized
29
30 query = Device.query.filter_by(owner=user)
31 if name is not None:
32 query = query.filter(Device.name.like('%{}%'.format(name)))
[ec8d5d0]33
[7be94aa]34 result = []
35 for device in query.offset(offset).limit(limit):
36 result.append(device.to_dict())
37 return result
[ec8d5d0]38
39
40def devices_get_by_id(id):
[7be94aa]41 user = User.loggedin_user()
42 if user is None:
43 raise MapserviceUnauthorized
44
45 device = Device.query.filter_by(owner=user).filter_by(id=id).all()
46 if len(device) != 1:
47 raise MapserviceNotFound
48 return device[0].to_dict()
49
[ec8d5d0]50
51def devices_create(device_creation):
[7be94aa]52 user = User.loggedin_user()
53 if user is None:
54 raise MapserviceUnauthorized
55
56 comment = ""
57 if device_creation.comment:
58 comment = device_creation.comment
59
60 device = Device(
61 name=device_creation.name,
62 owner=user,
63 sensor_depth=device_creation.sensor_depth,
64 comment=comment
65 )
66 db.session.add(device)
67 db.session.commit()
68 return device.to_dict()
69
[ec8d5d0]70
71def devices_device_delete(id):
[7be94aa]72 user = User.loggedin_user()
73 if user is None:
74 raise MapserviceUnauthorized
75
76 if not Device.query.filter_by(owner=user).filter_by(id=id).delete():
77 raise MapserviceBadRequest(
78 detail="Could not delete device with id {}".format(id)
79 )
80 db.session.commit()
81
82 query = Device.query.filter_by(owner=user)
83 result = []
84 for device in query:
85 result.append(device.to_dict())
86 return result
Note: See TracBrowser for help on using the repository browser.