def post_incidents_api(): try: if request.is_json: payload = request.get_json() json_name = None if 'name' in payload: json_name = payload['name'] json_message = None if 'message' in payload: json_message = payload['message'] json_status = 0 if 'status' in payload: json_status = int(payload['status']) json_visible = True if 'visible' in payload: json_visible = bool(payload['visible']) res = incidents.Incident(name=json_name, message=json_message, status=json_status, visible=json_visible).insert() return jsonify(res) else: return jsonify({'error': {'message': 'Expected a json object' }}) except Exception, e: return jsonify({'error': {'message': str(e)}})
def function(): """ Thisfunction will receive the nested sequence. First it will validate that the input data has the sequence, in the case that the array doesn't exist it returns an error. If the the array exists, the function will be called to flatten it. Once the array has been flattened, the function is called to save it in the database """ inputData = request.get_json(silent=True) print(inputData) try : originArray = inputData['items'] except : return jsonify({"status": "failed", "msg": 'Missing arguments'}),400 try : flattenArray = dataController.flatten(originArray) isSaved = dataController.saveArray(flattenArray) if isSaved: return jsonify({"status": "success", "msg": "The array was saved in database", "flattenArray": flattenArray}), 200 else: return jsonify({"status": "success", "msg": "The array was not saved in database","flattenArray": flattenArray}), 200 except Exception as error: print(error) return jsonify({'status': 'failed', 'msg': 'Something bad happened'}), 500
def onlyFlatten(): """ This endpoint was created only to verify the flatten This function will receive the nested sequence. First it will validate that the input data has the sequence, in the case that the array doesn't exist it returns an error. If the the array exists, the function will be called to flatten it. """ inputData = request.get_json(silent=True) print(inputData) try : originArray = inputData['items'] except : return jsonify({"status": "failed", "msg": 'Missing arguments'}),400 try : flattenArray = dataController.flatten(originArray) return jsonify({"status": "success","flattenArray": flattenArray}), 200 except Exception as error: print(error) return jsonify({'status': 'failed', 'msg': 'Something bad happened'}), 500
def excluir_filme(filme_id): resposta = jsonify({"resultado": "ok", "detalhes": "ok"}) try: Filme.query.filter(Filme.id == filme_id).delete() db.session.commit() except Exception as e: resposta = jsonify({"resultado":"erro", "detalhes":str(e)}) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def delete_incidents_api(incident_id): try: if incident_id is not None and type(incident_id) is int: res = incidents.Incident(id=incident_id).delete() return jsonify(res) else: res = incidents.Incident().delete() return jsonify(res) except Exception, e: return jsonify({'error': {'message': str(e)}})
def incluir_aluguel_filme_realizado(): resposta = jsonify({"resultado": "ok", "detalhes": "ok"}) dados = request.get_json() try: nova = AluguelFilme(**dados) db.session.add(nova) db.session.commit() except Exception as e: resposta = jsonify({"resultado":"erro", "detalhes":str(e)}) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def excluir_computador(computador_id): resposta = jsonify({"resultado": "ok", "detalhes": "ok"}) try: Computador.query.filter(Computador.id == computador_id).delete() bd.session.commit() except Exception as e: resposta = jsonify({"resultado":"erro", "detalhes":str(e)}) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def delete_helicopter(helicopter_id): response = jsonify({"result": "ok", "details": "ok"}) try: HelicopteroDeCombate.query.filter( HelicopteroDeCombate.id == helicopter_id).delete() db.session.commit() except Exception as e: response = jsonify({"result": "error", "details": str(e)}) response.headers.add("Access-Control-Allow-Origin", "*") return response
def create_helicopter(): response = jsonify({"result": "ok", "details": "ok"}) data = request.get_json() try: new_helicopter = HelicopteroDeCombate(**data) db.session.add(new_helicopter) db.session.commit() except Exception as e: response = jsonify({"result": "error", "details": str(e)}) response.headers.add("Access-Control-Allow-Origin", "*") return response
def incluir_computador(): resposta = jsonify({ "resultado": "ok", "detalhes": "ok" }) dados = request.get_json() try: computador = Computador(**dados) bd.session.add(computador) bd.session.commit() except Exception as e: resposta = jsonify({ "resultado": "erro", "detalhes": str(e) }) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def listar_lanhouses(): lanhouses = bd.session.query(LanHouse).all() lista_lanhouses = [ _.json() for _ in lanhouses ] resposta = jsonify(lista_lanhouses) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def listar_computadores(): computadores = bd.session.query(Computador).all() lista_computadores = [ _.json() for _ in computadores ] resposta = jsonify(lista_computadores) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def listar_usuarios(): usuarios = bd.session.query(Usuario).all() lista_usuarios = [ _.json() for _ in usuarios ] resposta = jsonify(lista_usuarios) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def get_incidents_api(incident_id): try: lim = request.args.get('limit') fr = request.args.get('from') if type(request.args.get('deleted')) is str: get_deleted = True else: get_deleted = False if request.args.get('id') is not None: incident_id = int(request.args.get('id')) if incident_id is not None and type(incident_id) is int: res = incidents.Incident(id=incident_id). get_json(deleted=get_deleted) return jsonify(res) else: res = incidents.Incident(). get_json(limit=lim, index=fr, deleted=get_deleted) return jsonify(res) except Exception, e: return jsonify({'error': {'message': str(e)}})
def version(): res = { 'meta': { 'on_latest': True, 'latest': { 'tag_name': 'v1.0', 'prelease': False, 'draft': False } }, 'data': '1.0.1-dev' } return jsonify(res)
def listar(classe): dados = None if classe == "Filme": dados = db.session.query(Filme).all() elif classe == "AluguelFilme": dados = db.session.query(AluguelFilme).all() elif classe == "Locadora": dados = db.session.query(Locadora).all() lista_jsons = [ x.json() for x in dados ] resposta = jsonify(lista_jsons) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def put_incidents_api(incident_id): try: if incident_id is not None and type(incident_id) is int: if request.is_json: target = incidents.Incident(id=incident_id) payload = request.get_json() if 'name' in payload: target.update(name=payload['name']) if 'message' in payload: target.update(message=payload['message']) if 'status' in payload: target.update(status=payload['status']) if 'visible' in payload: target.update(visible=payload['visible']) return jsonify({'data': 'Update succeeded.'}) else: return jsonify({'error': {'message': 'Expected a json object' }}) else: return jsonify({'error': {'message': "Don't determine incident" }}) except Exception, e: print e return jsonify({'error': {'message': str(e)}})
def index_table(table): data = None if table == "helicoptero-de-combates": data = db.session.query(HelicopteroDeCombate).all() elif table == "pilots": data = db.session.query(Pilot).all() elif table == "hangars": data = db.session.query(Hangar).all() json_list = [_.json() for _ in data] response = jsonify(json_list) response.headers.add("Access-Control-Allow-Origin", "*") return response
def ping(): res = {'data': 'pong'} return jsonify(res)
def listar_locadora(): locadora = db.session.query(Locadora).all() lista_jsons = [ x.json() for x in locadora ] resposta = jsonify(lista_jsons) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def listar_aluguel_filme(): aluguel_filme = db.session.query(AluguelFilme).all() lista_jsons = [ x.json() for x in aluguel_filme ] resposta = jsonify(lista_jsons) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def listar_filmes(): filmes = db.session.query(Filme).all() filmes_em_json = [ filme.json() for filme in filmes ] resposta = jsonify(filmes_em_json) resposta.headers.add("Access-Control-Allow-Origin", "*") return resposta
def get_files(): schema = models.FileSchema(many=True) files = models.File.query.order_by(models.File.id.desc()) data = schema.dump(files) return jsonify(data)