def put(self, id): # Update the record for the provided id # with the details provided. return def delete(self, id): # Delete the record for the provided id. product = Product.query.filter_by(id=id).first() db.session.delete(product) db.session.commit() response = {'message': 'Success'} return jsonify(response) product_view_requests = ProductViewRequests.as_view('product_view_requests') product_view = ProductView.as_view('product_view') app.add_url_rule( '/product/', view_func=product_view, methods=['GET', 'POST'] ) app.add_url_rule( '/product/<int:id>', view_func=product_view, methods=['GET', 'DELETE'] )
} return jsonify(res) def post(self): name = request.form.get('name') gender = request.form.get('gender') company = request.form.get('company') cliente = Clientes(name, gender, company) db.session.add(cliente) db.session.commit() return jsonify({ cliente.id: { 'name': cliente.name, 'gender': str(cliente.gender), 'company': str(cliente.company), } }) def put(self, id): # Update the record for the provided id # with the details provided. return def delete(self, id): # Delete the record for the provided id. return clientes_view = ClientesView.as_view('clientes_view') app.add_url_rule('/cliente/', view_func=clientes_view, methods=['GET', 'POST']) app.add_url_rule('/cliente/<int:id>', view_func=clientes_view, methods=['GET'])
db.session.add(gas_station) db.session.commit() return jsonify({ gas_station.id: { 'name': gas_station.name, 'place_id': gas_station.place_id, 'vicinity': gas_station.vicinity } }) def put(self, id): gas_station = GasStation.query.filter_by(id=id).first() # for req in request.form: # if req in GasStation.__dict__.keys(): # gas_station[req] = request.form.get(req) return '' def delete(self, id): # Delete the record for the provided id. return gas_station_view = GasStationView.as_view('gas_station_view') app.add_url_rule('/gas_station/', view_func=gas_station_view, methods=['GET', 'POST']) app.add_url_rule('/gas_station/<int:id>', view_func=gas_station_view, methods=['GET', 'PUT'])
db.session.commit() company_dict = { 'id': company.id, 'name': company.name, } data = {'data': company_dict} return jsonify(data) def put(self, id): # Update the record for the provided id # with the details provided. return def delete(self, id): # Delete the record for the provided id. company = Company.query.filter_by(id=id).first() db.session.delete(company) db.session.commit() response = {'message': 'Success'} return jsonify(response) company_view_requests = CompanyViewRequests.as_view('company_view_requests') company_view = CompanyView.as_view('company_view') app.add_url_rule('/company/', view_func=company_view, methods=['GET', 'POST']) app.add_url_rule('/company/<int:id>', view_func=company_view, methods=['GET', 'DELETE'])
password = request.form.get('password') confirm_password = request.form.get('confirm_password') if password and len(password) >= 5: if password == confirm_password: user.password = password user.save() return jsonify( {'msg': 'Password changed successfully'}) else: return jsonify( {'msg': 'Both password did not matched '}) else: return jsonify( {'msg': 'Minimum length of password should be 5'}) else: return jsonify({'message': 'Please login to reset password.'}) else: return jsonify({'msg': "unauthorized access"}) User_view = UserView.as_view('user_view') Login_view = LoginView.as_view('login_view') app.add_url_rule('/registration/', view_func=User_view, methods=['POST']) app.add_url_rule('/users/', view_func=User_view, methods=['GET']) app.add_url_rule('/user/<int:id>', view_func=User_view, methods=['GET']) app.add_url_rule('/deleteuser/<int:id>', view_func=User_view, methods=['DELETE']) app.add_url_rule('/login', view_func=Login_view, methods=['POST']) app.add_url_rule('/changePassword', view_func=Login_view, methods=['PUT'])
def put(self, id): name = request.form.get('name') category = request.form.get('category') michellin_stars = request.form.get('michellin_stars') db.session.query(Restaurant).filter(Restaurant.id == id).update({'michellin_stars': str(michellin_stars),'name': name, 'category': category}) db.session.commit() res = { 'name': restaurant.name, 'category': restaurant.category, 'michellin_stars' : str(restaurant.michellin_stars) } return jsonify(res) def delete(self, id): restaurant = Restaurant.query.filter_by(id=id).first() db.session.delete(restaurant) db.session.commit() return jsonify({restaurant.id: { 'name': restaurant.name, 'category': restaurant.category, 'michellin_stars': str(michellin_stars) }}) restaurant_view = RestaurantView.as_view('restaurant_view') app.add_url_rule( '/restaurant/', view_func=restaurant_view, methods=['GET', 'POST'] ) app.add_url_rule( '/restaurant/<int:id>', view_func=restaurant_view, methods=['GET','PUT',"DELETE"] )
else: return jsonify({"msg": 'You are not authorized'}) def delete(self, id): # Delete the record for the provided id. auth_header = request.headers.get('Authorization') access_token = auth_header.split(" ")[0] if access_token: user_id = User.decode_token(access_token) if user_id: user = User.query.get(user_id) borrowed_book = Borrow.query.join(User).join(Book).filter( Book.id == id).filter(User.id == user.id).filter( Borrow.returned == False) borrowed_book_sqlquery = str(borrowed_book) % (id, user.id) reslt = db.engine.execute(borrowed_book_sqlquery).fetchall() if len(reslt) > 0: borrow_id = reslt[0][0] borrow_obj = Borrow.query.get(borrow_id) borrow_obj.returned = True borrow_obj.save() return jsonify( {'msg': 'Yoy have sucessfully returned Book.'}) else: return jsonify({'msg': 'Yoy have not borrowed this Book.'}) Borrow_view = BorrowView.as_view('Borrow_view') app.add_url_rule('/borrow/<int:id>', view_func=Borrow_view, methods=['GET']) app.add_url_rule('/return/<int:id>', view_func=Borrow_view, methods=['DELETE'])
} return jsonify(res) def post(self): title = request.form.get('title') rating = request.form.get('rating') body = request.form.get('body') review = Review(title, rating, body) db.session.add(review) db.session.commit() return jsonify({ review.id: { 'title': review.title, 'rating': str(review.rating), 'body': review.body, } }) def put(self, id): # Update the record for the provided id # with the details provided. return def delete(self, id): # Delete the record for the provided id. return review_view = ReviewView.as_view('review_view') app.add_url_rule('/review/', view_func=review_view, methods=['GET', 'POST']) app.add_url_rule('/review/<int:id>', view_func=review_view, methods=['GET'])
'name': product.name, 'price': str(product.price), }})""" def put(self, id): # Update the record for the provided id # with the details provided. return def delete(self, id): # Delete the record for the provided id. return coleta_view = ColetaView.as_view('coleta_view') app.add_url_rule('/coleta/', view_func=coleta_view, methods=['GET', 'POST']) app.add_url_rule('/coleta/<int:id>', view_func=coleta_view, methods=['GET']) class LigaView(MethodView): def get(self, id=None, page=1): try: #comport = serial.Serial('/dev/ttyUSB0',9600) time.sleep(1.5) PARAM_LIGAR = '1' PARAM_ASCII_LIGAR = str(chr(49)) comport.write(PARAM_LIGAR) comport.write(PARAM_ASCII_LIGAR) #comport.close() return jsonify({"temperatura": "ligado", "umidade": "ligado"})
} return jsonify(response) def delete(self, id): # Delete the record for the provided id. if User.isAdmin(request) : book = Book.query.filter_by(id=id).first() book.delete() return jsonify({'msg': 'deleted'}) else : response = { 'message': 'you are not allowed for book modification.' } return jsonify(response) BookOperation_view = BookOperationView.as_view('book_opration') app.add_url_rule( '/add_book', view_func=BookOperation_view, methods=['POST'] ) app.add_url_rule( '/update_book/<int:id>', view_func=BookOperation_view, methods=['PUT'] ) app.add_url_rule( '/delete_book/<int:id>', view_func=BookOperation_view, methods=['DELETE'] )
response = {'message': 'Success'} return jsonify(response) company_view_requests = CompanyViewRequests.as_view('company_view_requests') company_view = CompanyView.as_view('company_view') company_post_init_view = CompanyPostFrontView.as_view('company_post_init_view') company_delete_view = CompanyDeleteView.as_view('company_delete_view') company_count_ape_view = CompanyCountCodeApe.as_view('company_count_ape_view') code_ape_view = CodeApeView.as_view('code_ape_view') code_ape_front_view = CodeApePostFrontView.as_view('code_ape_front_view') company_count_region = CompanyCountRegion.as_view('company_count_region') company_count_ile_de_france = CompanyCountIleDeFrance.as_view( 'company_count_ile_de_france') app.add_url_rule('/company/', view_func=company_view, methods=['GET', 'POST']) app.add_url_rule('/codeape/', view_func=code_ape_view, methods=['GET', 'POST']) app.add_url_rule('/codeape/init', view_func=code_ape_front_view, methods=['POST']) app.add_url_rule('/company/count/', view_func=company_count_ape_view, methods=['GET']) app.add_url_rule('/company/count/region/', view_func=company_count_region, methods=['GET'])
if (user_data.password == hashlib.md5( password.encode('utf-8')).hexdigest() and email == user_data.email): print('test') token = user_data.encode_token(user_data.email) print('token: ', token) return jsonify({ 'message': { 'id': user_data.id, 'name': user_data.name, 'email': user_data.email, 'token': token.decode(), } }) else: return jsonify( {'error': { 'message': 'password is incorrect', }}) except Exception as e: return jsonify({'error': { 'message': 'email not found', }}) user_view = UserView.as_view('user_view') login = Login.as_view('login') app.add_url_rule('/register/', view_func=user_view, methods=['POST']) app.add_url_rule('/login/', view_func=login, methods=['POST'])
'rol': user.rol } # API credentials api_username = "******" api_password = "******" # Protect API def protectAPI(f): def userDecorated(*args, **kwargs): auth = request.authorization if api_username == auth.username and api_password == auth.password: return f(*args, **kwargs) # Unauthorized return abort(401) return userDecorated ### Routes ### # user_view = UserAPI.as_view('user_view') # Unprotected API user_view = protectAPI(UserAPI.as_view('user_view')) # GET app.add_url_rule('/api/users/', view_func=user_view, methods=['GET', 'POST']) # CRUD app.add_url_rule('/api/users/<int:id>', view_func=user_view, methods=['GET', 'POST', 'PUT', 'DELETE'])
imageURL = request.args.get('imageURL') type = request.args.get('type') ingredients = request.args.get('ingredients') steps = request.args.get('steps') product = Meal(foodName, calories, imageURL, type, ingredients, steps) db.session.add(product) db.session.commit() return jsonify({ product.id: { 'foodName': product.foodName, 'calories': product.calories, 'imageURL': product.imageURL, 'type': product.type, 'ingredients': product.ingredients, 'steps': product.steps, } }) def put(self, id): # Update the record for the provided id # with the details provided. return def delete(self, id): # Delete the record for the provided id. return product_view = ProductView.as_view('product_view') app.add_url_rule('/meal/', view_func=product_view, methods=['GET', 'POST']) app.add_url_rule('/meal/<int:id>', view_func=product_view, methods=['GET'])
from flask.views import MethodView from my_app.product.model.product import Product from my_app import app class ProductAPI(MethodView): def get(self): products = Product.query.all() # Ahora utilizamos JSON return products product_view = ProductAPI.as_view('product_view') app.add_url_rule('/api/products/', view_func=product_view, methods=['GET'])
categories = Category.query.all() if id: category = Category.query.get(id) res = categoryToJson(category) else: res = [] for c in categories: res.append(categoryToJson(c)) return sendResJson(res,None,200) def categoryToJson(category: Category): return { 'id': category.id, 'name': category.name } category_view = CategoryApi.as_view('category_view') app.add_url_rule('/api/categories/', view_func=category_view, methods=['GET']) app.add_url_rule('/api/categories/<int:id>', view_func=category_view, methods=['GET'])
if not id: # find and return the first 10 objects, if no gived an id example_items = ExampleModelClass.query.paginate(1, 10).items result = {item.id: {'name': item.name} for item in example_items} else: # find and return a gived id object example_item = ExampleModelClass.query.filter_by(id=id).first() if not example_item: abort(404, 'Item não existe') result = {'name': example_item.name} return jsonify(result), 200 def post(self): # create an object and save the data name = json.loads(request.data.decode()).get('name') example_item = ExampleModelClass(name) db.session.add(example_item) db.session.commit() return jsonify({ example_item.id: { 'name': example_item.name } }) example_view = ExampleRESTClassModelView.as_view('example_view') app.add_url_rule('/example/', view_func=example_view, methods=['GET', 'POST']) app.add_url_rule('/example/<int:id>', view_func=example_view, methods=['GET'])