def update_plant(): """Updates existing Plant in database. # request body args: plant_id, name, description, avatar_image """ json_data = request.get_json() plant_id = json_data['plant_id'] json_data['last_update'] = datetime.now() try: result = Plant.query.filter_by(id=plant_id).first() if not result: return error_plant.not_exists() else: exceptions = ['id', 'book_id'] updated_query = update_query_object(result, json_data, exceptions) db.session.add(updated_query) db.session.commit() except Exception as e: return error_plant.unable_to_update(e) res_data = jsonify({'message': 'Plant updated', 'success': True}) return make_response(res_data, 200)
def create_plant(): """Creates new Plant in db """ json_data = request.get_json() book_id = json_data['book_id'] json_data['last_update'] = datetime.now() json_data['created_at'] = datetime.now() try: result = Book.query.filter_by(id=book_id).first() if not result: return error_book.not_exists() else: new_plant = update_query_object(Plant(), json_data) db.session.add(new_plant) db.session.commit() except Exception as e: return error_plant.unable_to_create(e) data = { 'plant_id': new_plant.id, 'message': 'New Plant created', 'success': True } return make_response(jsonify(data), 200)
def update_plants_book(): """Updates existing Plant's book_id in database. # request body args: book_id, plant_id """ json_data = request.get_json() plant_id = json_data['plant_id'] book_id = json_data['book_id'] json_data['last_update'] = datetime.now() try: new_book = Book.query.filter_by(id=book_id).first() plant = Plant.query.filter_by(id=plant_id).first() if not new_book: return error_book.not_exists() if not plant: return error_plant.not_exists() else: exceptions = ['id'] updated_query = update_query_object(plant, json_data, exceptions) db.session.add(updated_query) db.session.commit() except Exception as e: return error_plant.unable_to_update(e) res_data = jsonify({'message': "Plant's Book updated", 'success': True}) return make_response(res_data, 200)
def update_book(user): """Updates existing book in database. request body args: book_id, user_id, name, description, avatar_image """ json_data = request.get_json() user_id = user.id book_id = json_data['book_id'] json_data['last_update'] = datetime.now() try: result = Book.query.filter_by(user_id=user_id, id=book_id).first() if not result: return error_book.not_exists() else: exceptions = ['user_id', 'book_id'] updated_query = update_query_object( result, json_data, exceptions ) db.session.add(updated_query) db.session.commit() except Exception as e: return error_book.unable_to_update(e) res_data = jsonify({ 'message': 'Book updated', 'success': True }) return make_response(res_data, 200)
def create_species(): json_data = request.get_json() common_name = json_data['common_name'] try: result = Encyclopedia.query.filter_by(common_name=common_name, validated=True) if not result: new_species = update_query_object( Encyclopedia(), json_data ) db.session.add(new_species) db.session.commit() except Exception as e: return abort(404)
def plant_update_action(action): if action not in ['dew', 'watering']: return ErrorHandler.abort() json_data = request.get_json() plant_ids = json_data['plant_ids'] json_data['last_update'] = datetime.now() json_data['last_{}'.format(action)] = datetime.now().isoformat() if not plant_ids: return error_plant.provide_parameters() try: results = Plant.query.filter(Plant.id.in_(plant_ids)).all() if not results: return error_plant.not_exists() else: for result in results: current_time = json_data['last_{}'.format(action)] last_action = getattr(result, '{}s'.format(action)) if last_action is None: print('{}s'.format(action)) json_data['{}s'.format(action)] = json.dumps( {'{}s'.format(action): [current_time]}) else: action_property = json.loads(last_action) action_property['{}s'.format(action)].append(current_time) json_data['{}s'.format(action)] = json.dumps( action_property) exceptions = ['id', 'book_id'] updated_query = update_query_object(result, json_data, exceptions) db.session.add(updated_query) db.session.commit() except Exception as e: print(e) return error_plant.unable_to_update(e) res_data = jsonify({'message': 'Plant updated', 'success': True}) return make_response(res_data, 200)