Commit c6a0633b authored by Nelso Jost's avatar Nelso Jost

FIX: api for posting multiple raw sensor data

parent 22ddc453
......@@ -29,45 +29,50 @@ def api_post_rawsensordata(board_id):
return jsonify({'error':
'Request does not have a valid JSON attribute.'})
d = request.json
if type(d) is str:
d = json.loads(request.json)
r = request.json
if type(r) is str:
r = json.loads(request.json)
if not 'user_hash' in d or d['user_hash'] != board.user.password_hash:
return jsonify({'error': 'Missing or invalid JSON "user_hash" '
'attribute.'})
if not 'datetime' in d:
return jsonify({'error': "JSON missing or invalid datetime attribute."
" Expeted format: {'value': str, 'format': str, 'source': str}"})
try:
dt = datetime.strptime(d['datetime']['value'],
d['datetime']['format'])
except:
return jsonify({'error': "Invalid datetime format."})
print(r)
if not 'sensors' in d or not len(d['sensors']):
return jsonify({'error': 'JSON missing or invalid sensors attribute'})
if not 'user_hash' in r or r['user_hash'] != board.user.password_hash:
return jsonify({'error': 'Missing or invalid JSON "user_hash" '
'attribute for board of ID {}.'.format(board_id)})
sensors = {}
for d in r['data']:
for nickname in d['sensors']:
try:
sensors[board.get_sensor_or_404(nickname)] = d['sensors'][nickname]
except:
return jsonify({'error': "This board does not have a sensor named"
"'{}'.".format(nickname)})
if not 'datetime' in d:
return jsonify({'error': "JSON missing or invalid datetime attribute."
" Expeted format: {'value': str, 'format': str, 'source': str}"})
for sensor, value in sensors.items():
try:
sensor.add_rawdata(datetime=dt, value=value,
avoid_duplication=True)
dt = datetime.strptime(d['datetime']['value'],
d['datetime']['format'])
except:
return jsonify({'error': "Datetime conflit for sensor '{}':"
"raw data with datetime '{}' already "
"exists!".format(nickname, dt)})
return jsonify({'error': "Invalid datetime format."})
if not 'sensors' in d or not len(d['sensors']):
return jsonify({'error': 'JSON missing or invalid sensors attribute'})
sensors = {}
for nickname in d['sensors']:
try:
sensors[board.get_sensor_or_404(nickname)] = d['sensors'][nickname]
except:
return jsonify({'error': "This board does not have a sensor named"
"'{}'.".format(nickname)})
for sensor, value in sensors.items():
try:
sensor.add_rawdata(datetime=dt, value=value,
avoid_duplication=True)
except Exception as e:
return jsonify({'error': '{}'.format(e)})
# "Datetime conflit for sensor '{}':"
# "raw data with datetime '{}' already "
# "exists!".format(nickname, dt)})
db.session.commit()
return jsonify({'success': 'Data was sucessfully saved on the board.'})
return jsonify({'success': 'All data was sucessfully saved on the board.'})
......@@ -93,10 +93,13 @@ class Sensor(db.Model, DBUtils):
''' Add raw sensor data. RawSensorData fields are given by kwargs.
'''
if avoid_duplication:
if self.rawdata.filter_by(datetime=kwargs['datetime']).first():
raise ValueError('Datetime already exists!')
if self.rawdata.filter_by(datetime=datetime).first():
raise ValueError('Datetime already exists for {}!'
.format(self.nickname))
db.session.add(RawSensorData(sensor_id=self.id, datetime=datetime, value=value))
db.session.add(RawSensorData(sensor_id=self.id,
datetime=datetime,
value=value))
@classmethod
def delete(cls, key):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment