| 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 | '''
|
|---|
| 17 | import connexion
|
|---|
| 18 |
|
|---|
| 19 | from ..database.Model import User, Device, db
|
|---|
| 20 | from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
|
|---|
| 21 |
|
|---|
| 22 | def 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:
|
|---|
| 27 | raise MapserviceUnauthorized
|
|---|
| 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 |
|
|---|
| 38 | def devices_get_by_id(id):
|
|---|
| 39 | user = User.loggedin_user()
|
|---|
| 40 | if user is None:
|
|---|
| 41 | raise MapserviceUnauthorized
|
|---|
| 42 |
|
|---|
| 43 | device = Device.query.filter_by(owner = user).filter_by(id=id).all()
|
|---|
| 44 | if len(device) != 1:
|
|---|
| 45 | raise MapserviceNotFound
|
|---|
| 46 | return device[0].to_dict()
|
|---|
| 47 |
|
|---|
| 48 | def devices_create(device_creation):
|
|---|
| 49 | user = User.loggedin_user()
|
|---|
| 50 | if user is None:
|
|---|
| 51 | raise MapserviceUnauthorized
|
|---|
| 52 |
|
|---|
| 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)
|
|---|
| 58 | db.session.add(device)
|
|---|
| 59 | db.session.commit()
|
|---|
| 60 | return device.to_dict()
|
|---|
| 61 |
|
|---|
| 62 | def devices_device_delete(id):
|
|---|
| 63 | user = User.loggedin_user()
|
|---|
| 64 | if user is None:
|
|---|
| 65 | raise MapserviceUnauthorized
|
|---|
| 66 |
|
|---|
| 67 | if not Device.query.filter_by(owner = user).filter_by(id=id).delete():
|
|---|
| 68 | raise MapserviceBadRequest(detail="Could not delete device with id {}".format(id))
|
|---|
| 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
|
|---|