def start(): try: logging.info("In /start in main_controller.py") if request.methods == "POST": logging.info("Into POST condition in main_controller.py") username = request.json['username'] email = request.json['email'] if len(username) > 5 and len( email) > 14 and request.methods == 'POST': logging.info("redirecting to /post method of main_logic.py") return redirect(url_for('/user'), method='POST') elif request.methods == "GET": logging.info("Into GET condition in main_controller") logging.info("redirecting to /get method of main_logic.py") return redirect(url_for('/user'), method='GET') else: logging.warning("Method entered is not defined") return 'Invalid Choice' except Exception as ex: logging.error("Exception occurred at /start") print(ex)
def add_user(): try: logging.info("In POST add_user method of main_logic.py") username = request.json['username'] email = request.json['email'] # validate input if username and email and request.method == 'POST': logging.info( "In if condition of POST add_user method of main_logic.py ") new_user = User(username, email) logging.info("after new_user added in main_logic.py") db.session.add(new_user) db.session.commit() logging.info("New User Details: username: {}, email: {}".format( username, email)) logging.info("Done commit") all_users = User.query.all() result = users_schema.dump(all_users) return users_schema.jsonify(result) else: logging.warning("Not able to add new user") return 'Error while adding new user' except Exception as ex: logging.error( "Error occurred in POST add_user method of main_logic.py") print(ex)
def user_update(id): try: logging.info("In PUT/<id> user_update method of main_logic.py") if id and request.method == 'PUT': user = User.query.get(id) username = request.json['username'] email = request.json['email'] if username is not None and email is not None: user.email = email user.username = username db.session.commit() logging.info("success: updated {}".format(user)) return jsonify(user) else: return 'Error username and email not available' else: return 'Error in updating data of id:{}'.format(id) except Exception as ex: logging.error( "Error occurred in PUT user_update method of main_logic. Not able to update" ) print(ex)
def user_details(id): try: logging.info("In GET/<id> user_details method of main_logic.py") if id and request.method == 'GET': user = User.query.get(id) logging.info("Success: Displaying all the data of {}".format(user)) return jsonify(user) else: return 'Error in Extracting Data of id:{}'.format(id) except Exception as ex: logging.error( "Error occurred in GET/<id> user_details method of main_logic. Not able to Collect data" ) print(ex)
def user_delete(id): try: logging.info("In DELETE user_delete method of main_logic.py") if id and request.method == 'DELETE': user = User.query.get(id) db.session.delete(user) db.session.commit() logging.info("success: Deleted {}".format(user)) return jsonify(user) else: return 'Error in Deleting Data of id:{}'.format(id) except Exception as ex: logging.error( "Error occurred in DELETE user_delete method of main_logic.Not able to delete" ) print(ex)
def get_user(): try: logging.info("In GET get_user method of main_logic.py") all_users = User.query.all() result = users_schema.dump(all_users) if all_users and result and request.method == 'GET': logging.info("Success: Displaying all the data") return jsonify(result) else: return 'Error in Extracting Data' except Exception as ex: logging.error( "Error occurred in GET get_user method of main_logic. Not able to collect data" ) print(ex)
def start_id(id): try: logging.info("In /start/<id> in main_controller") if request.methods == "PUT": logging.info("Into Put method in main_controller.py") logging.info("redirecting to /put/<id> method in main_logic.py") return redirect(url_for('/user/<id>'), method='PUT') elif request.methods == "DELETE": logging.info("Into Delete method in main_controller") logging.info("redirecting to /delete/<id> method in main_logic.py") return redirect(url_for('/user/<id>'), method='DELETE') elif request.methods == "GET": logging.info("Into GET/<id> method in main_controller") logging.info("redirecting to /get/<id> method in main_logic.py") return redirect(url_for('/user/<id>'), method='GET') except Exception as ex: logging.error("Exception occurred at /start/<id>") print(ex)
from flask_sqlalchemy import SQLAlchemy from server import app, logging logging.info("In config.py: database Configuration") app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Data.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app)