def add_action(version=app.config['ACTUAL_API']): """ Add a new LEA (Low Elementary Action) into database. The JSON structure must be in a defined format called Common Data format as: { "action": "eu:c4a:POI_EXIT", "user": "******", "pilot": "LCC", "location": "eu:c4a:Pharmacy:Vanilla123", "position": "38.976908 22.724375", "timestamp": "2015-05-20T07:08:41.013+03:00", "payload": { "instance_id": "1287" }, "rating": 0.45, "data_source_type": [ "sensors", "external_dataset" ], "extra": { "pilot_specific_field": "some value" } } :param basestring version: Api version :return: Different kind of HTML codes explaining if the action was successful """ if Utilities.check_connection(app, version): # We created a list of Python dict. data = _convert_to_dict(request.json) msg = Utilities.check_add_action_data(AR_DATABASE, data) # TODO review in the future if data and not msg and USER: # User and data are OK. save data into DB res_ar = AR_DATABASE.add_action(data) res_sr = SR_DATABASE.add_action(data) if res_ar and res_sr: logging.info( "add_action: the username: %s adds new action into database" % USER.username) return Response('Data stored in database OK\n'), 200 else: logging.error( "add_action: the username: %s failed to store data into database. 500" % USER.username) return "There is an error in DB", 500 else: logging.error("add_action: there is a problem with entered data") # Data is not valid, check the problem if msg and "duplicated" in msg: # Data sent is duplicated. return Response(msg), 409 else: # Standard Error return Response(msg), 400
def add_action(version=app.config['ACTUAL_API']): """ Add a new LEA (Low Elementary Action) into database. The JSON structure must be in a defined format called Common Data format as: { "action": "eu:c4a:POI_ENTER", "user": "******", "pilot": "ATH", "location": "eu:c4a:Shop:Ermou96", "position": "37.976908 23.724375", "timestamp": "2014-05-20T07:08:41.013+03:00", "payload": { "instance_id": "124" }, "data_source_type": [ "sensors", "external_dataset" ], "extra": { "pilot_specific_field": "some value" } } :param version: Api version :return: Different kind of HTML codes explaining if the action was successful """ if Utilities.check_connection(app, version): # We created a list of Python dict. data = _convert_to_dict(request.json) res, msg = Utilities.check_add_action_data(AR_DATABASE, data) if data and res and USER: # User and data are OK. save data into DB res_ar = AR_DATABASE.add_action(data) res_sr = SR_DATABASE.add_action(data) if res_ar and res_sr: Utilities.write_log_info(app, ( "add_action: the username: %s adds new action into database" % USER.username)) return Response('Data stored in database OK\n'), 200 else: Utilities.write_log_error(app, ( "add_action: the username: %s failed to store data into database. 500" % USER.username)) return "There is an error in DB", 500 else: return Response(msg), 400