source: src/mapservice/server_impl/controllers/LogsController.py@ a249e2d

debian/bullseye
Last change on this file since a249e2d 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: 4.5 KB
RevLine 
[ac11efe]1'''
[ec9e811]2 Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
[ac11efe]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
[306f18a]19from ..database.Model import User, Log, LogEntry, Water, Device, Gauge, ProcessingStatus, Queue, db
[ac11efe]20from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
[5e16955]21from ..tasks.tasks import log_process
[ec9e811]22
23def logs_get(submitter=None, order_by=None, offset=None, limit=None):
24 query = None
25
26 if submitter is not None:
27 submitter = User.query.filter_by(username=submitter).all()
28 if len(submitter) != 1:
29 raise MapserviceNotFound(detail="Could not find the submitter with id {}".format(submitter))
30 submitter = submitter[0]
31
32 query = None
33 if submitter is not None:
34 query = Log.query.filter_by(submitter = submitter)
35
36 if query is None:
37 query = Log.query
38
39 result = []
40 for log in query.offset(offset).limit(limit):
41 result.append(log.meta_data())
42 return result
43
[ac11efe]44def logs_log_get_by_id(id):
45 log = Log.query.filter_by(id=id).all()
46 if len(log) != 1:
47 raise MapserviceNotFound
[ec9e811]48 if connexion.request.headers['Accept'] == "application/geo+json":
49 return log[0].to_geojson(), 200, {'Content-Type': 'application/geo+json; charset=utf-8'}
[ac11efe]50 return log[0].log()
51
[ec9e811]52def logs_log_create(log_creation):
[ac11efe]53 user = User.loggedin_user()
54 if user is None:
55 raise MapserviceUnauthorized
[ec9e811]56
[ac11efe]57 water = Water.query.filter_by(id=log_creation.water).all()
58 if len(water) != 1:
[ec9e811]59 raise MapserviceBadRequest(detail='Water {} not found database'.format(log_creation.water))
60 water = water[0]
61
[ac11efe]62 device = Device.query.filter_by(id=log_creation.device).all()
63 if len(device) != 1:
[ec9e811]64 raise MapserviceBadRequest(detail='Device {} not found in database'.format(log_creation.device))
65 device = device[0]
66
[ac11efe]67 gauge = Gauge.query.filter_by(id=log_creation.gauge).all()
68 if len(gauge) != 1:
[ec9e811]69 raise MapserviceBadRequest(detail='Gauge {} not found in database'.format(log_creation.gauge))
70 gauge = gauge[0]
[ac11efe]71
[f601952]72 if log_creation.gauge_height is None:
73 raise MapserviceBadRequest(detail='Gauge Height (Stage) missing')
[ec9e811]74 else:
75 try:
[f601952]76 float(log_creation.gauge_height)
[ec9e811]77 except ValueError:
[f601952]78 raise MapserviceBadRequest(detail='Gauge Height (Stage) {} is no number'.format(log_creation.gauge_height))
[ec9e811]79
80 if log_creation.comment is None:
81 log_creation.comment = ""
[ac11efe]82
[f601952]83 log = Log(submitter=user, water=water, device=device, gauge=gauge, gauge_height=log_creation.gauge_height, start=None, end=None, comment=log_creation.comment)
[ac11efe]84 db.session.add(log)
85 db.session.commit()
86 return log.meta_data()
87
[ec9e811]88def logs_log_upload(log_id, body=None):
89 user = User.loggedin_user()
90 if user is None:
91 raise MapserviceUnauthorized
92
93 if body is None or not body:
94 raise MapserviceBadRequest(detail='No log data')
95
[a88607b]96 processing_state = ProcessingStatus.query.filter_by(label='Queued for Parsing').all()
[306f18a]97 if len(processing_state) != 1:
[a88607b]98 raise MapserviceBadRequest(detail='Processing status Queued for Parsing not found database')
[306f18a]99 processing_state = processing_state[0]
100
[ec9e811]101 log = Log.query.filter_by(id=log_id).all()
102 if len(log) != 1:
103 raise MapserviceBadRequest(detail='Log {} not found database'.format(log_id))
104 log = log[0]
105
106 # check if Log has already log entries
107 if LogEntry.query.filter_by(log=log).count() > 0:
108 raise MapserviceBadRequest(detail='Log {} has already log entries. Aborting'.format(log_id))
[a88607b]109
110 # Add Log to Queued table and change the processing state of Log
[306f18a]111 queue = Queue(log=log)
112 db.session.add(queue)
113 log.processingstatus = processing_state
[ec9e811]114 db.session.commit()
115
[a88607b]116 # request log parsing
[5e16955]117 log_process.delay(log.id, body.decode())
[a88607b]118
119 return log.meta_data()
[ec9e811]120
[ac11efe]121def logs_log_delete(id):
122 user = User.loggedin_user()
123 if user is None:
124 raise MapserviceUnauthorized
125
126 if not Log.query.filter_by(submitter = user).filter_by(id=id).delete():
127 raise MapserviceBadRequest(detail="Could not delete log with id {}".format(id))
128 db.session.commit()
129
130 query = Log.query.filter_by(submitter = user)
131 result = []
132 for log in query:
133 result.append(log.meta_data())
134 return result
135
Note: See TracBrowser for help on using the repository browser.