source: src/mapservice/server_impl/controllers/DevicesController.py@ ae2ff15

debian/bullseye
Last change on this file since ae2ff15 was ae2ff15, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Fixed testing environment
    • Fixed some issues in the API and it's serving
  • Property mode set to 100644
File size: 2.3 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
22def devices_get(name=None, order_by=None, offset=None, limit=None):
23 query = None
24
25 user = User.loggedin_user()
26 if user is None:
[1cd45dd]27 raise MapserviceUnauthorized
[ec8d5d0]28
29 query = Device.query.filter_by(owner = user)
30 if name is not None:
31 query = query.filter(Device.name.like('%{}%'.format(name)))
32
33 result = []
34 for device in query.offset(offset).limit(limit):
35 result.append(device.to_dict())
36 return result
37
38def devices_get_by_id(id):
39 user = User.loggedin_user()
40 if user is None:
[1cd45dd]41 raise MapserviceUnauthorized
[ec8d5d0]42
43 device = Device.query.filter_by(owner = user).filter_by(id=id).all()
44 if len(device) != 1:
[1cd45dd]45 raise MapserviceNotFound
[ec8d5d0]46 return device[0].to_dict()
47
48def devices_create(device_creation):
49 user = User.loggedin_user()
50 if user is None:
[1cd45dd]51 raise MapserviceUnauthorized
[ec8d5d0]52
[acf8893]53 comment = ""
54 if device_creation.comment:
55 comment = device_creation.comment
56
57 device = Device(name=device_creation.name, owner=user, sensor_depth=device_creation.sensor_depth, comment=comment)
[ec8d5d0]58 db.session.add(device)
59 db.session.commit()
60 return device.to_dict()
61
62def devices_device_delete(id):
63 user = User.loggedin_user()
64 if user is None:
[1cd45dd]65 raise MapserviceUnauthorized
[ec8d5d0]66
67 if not Device.query.filter_by(owner = user).filter_by(id=id).delete():
[1cd45dd]68 raise MapserviceBadRequest(detail="Could not delete device with id {}".format(id))
[ec8d5d0]69 db.session.commit()
70
71 query = Device.query.filter_by(owner = user)
72 result = []
73 for device in query:
74 result.append(device.to_dict())
75 return result
Note: See TracBrowser for help on using the repository browser.