def post(self): """Handle POST request for this view. Url ---> /api/v1/auth/login/""" data = request.get_json(silent=True, force=True) try: email = data["email"] password = data["password"] except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 # Get the user object using their email (unique to every user) # print(dir(User.User)) user = find_by_email(email) get_projectmembers(1) if not user: # User does not exist. Therefore, we return an error msg response = { "success": False, "msg": "Invalid email, Please try again" } return make_response(jsonify(response)), 400 # Try to authenticate the found user using their password if not user.verify_password(password): response = { "success": False, "msg": "Wrong password, Please try again" } return make_response(jsonify(response)), 402 access_token = create_access_token(identity=user.id, fresh=True) refresh_token = create_refresh_token(user.id) if not access_token or not refresh_token: response = {"success": False, "msg": "Something went wrong!"} # Return a server error using the HTTP Error Code 500 (Internal # Server Error) return make_response(jsonify(response)), 500 # Generate the access token. This will be used as the # authorization header response = { "success": True, "msg": "You logged in successfully.", "access_token": access_token, "refresh_token": refresh_token, "body": to_json(user), } return make_response(jsonify(response)), 200
def get(self, project_id): try: if not project_id: response = { "success":False, "msg": "Project id not provided" } return make_response(jsonify(response)), 400 project = find_by_project_id(project_id) project['members'] = get_projectmembers(project_id) response = { "success": True, "msg": "Project found", "body": project } return make_response(jsonify(response)), 200 except Exception: response = { "success":False, "msg": "Something went wrong!" } # Return a server error using the HTTP Error Code 500 (Internal # Server Error) return make_response(jsonify(response)), 500
def put(self, project_id): """Handle PUT request for this view. Url --> /api/v1/project/update""" # getting JSON data from request post_data = request.get_json(silent=True, force=True) try: project_name = post_data["project_name"] project_description = post_data["project_description"] except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 # Search the database for this project_name project = find_by_project_name(project_name) if project and project['id'] != project_id: # There already exists another project with the same name. # So we can't let this project use that name response = { "success": False, "msg": "Project name already taken." } return make_response(jsonify(response)), 400 try: project = find_by_project_id(project_id) project['members'] = get_projectmembers(project_id) if not project: response = { "success": False, "msg": "Project not present."} return make_response(jsonify(response)), 404 data = { "project_name": project_name, "project_description": project_description } project_new = update_project(project_id, data) response = { "success": True, "msg": "Project updated.", "body": project_new } return make_response(jsonify(response)), 201 except Exception as err: print("Error occurred: ", err) response = { "success": False, "msg": "Something went wrong"} return make_response(jsonify(response)), 500
def post(self): """Handle POST request for this view. Url --> /api/v1/project/create""" # getting JSON data from request post_data = request.get_json(silent=True, force=True) current_user = get_jwt_identity() try: project_name = post_data["project_name"] project_description = post_data["project_description"] admin_id = current_user except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 # Querying the database with requested project_name project = find_by_project_name(project_name) if project: # There is an existing project with the same name. We don't want to create two # projects with the same name. # Return a message to the user telling them that they have already created a project # with this name. response = { "success": False, "msg": "Project already exists. Please change the Project Name." } return make_response(jsonify(response)), 400 # There is no project so we'll try to create a new one """Save the new Project.""" try: project = Project(project_name=project_name, project_description=project_description, admin_id=admin_id) project_new = save_project(project) """Save the project admin.""" team = Team(team_name="admin", role="admin", project_id=project_new['id']) team_new = save_team(team) project_member = ProjectMember(user_id=admin_id, team_id=team_new['id']) project_member_new = save_project_member(project_member) project_new['members'] = get_projectmembers(project_new['id']) res = { "project": project_new, "team": team_new, "project_member": project_member_new } except Exception as err: print("Error occured: ", err) response = {"success": False, "msg": "Something went wrong!!"} return make_response(jsonify(response)), 500 response = { "success": True, "msg": "Your project was created successfully.", "body": res } # return a response notifying the user that they registered # successfully return make_response(jsonify(response)), 201