def post(self, project_id): post_data = request.get_json(silent=True, force=True) try: user_email = post_data["member_email"] except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 # Check if user exists user_obj = find_by_email(user_email) if not user_obj: response = {'success': False, 'msg': 'User does not exist'} return make_response(jsonify(response)), 404 user = to_json(user_obj) roles = get_user_roles(user['id'], project_id) # Check if user is already not admin if 'admin' not in roles: response = {'success': False, 'msg': 'User is already not admin'} return make_response(jsonify(response)), 400 admin_team = find_admin_team_of_project(project_id) delete_by_user_id_team_id(user['id'], admin_team['id']) response = { 'success': True, 'msg': 'User removed as admin', } return make_response(jsonify(response)), 200
def post(self, project_id): """ Handle POST request for this view. Url --> /api/v1/project/remove_project_member/<int:project_id> """ # getting JSON data from request post_data = request.get_json(silent=True, force=True) current_user = get_jwt_identity() roles = get_user_roles(current_user, project_id) if "admin" not in roles: print("Error occured: user not admin") response = {"success": False, "msg": "User not admin."} return make_response(jsonify(response)), 403 try: user_email = post_data["member_email"] except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 try: user_obj = find_by_email(user_email) user = to_json(user_obj) if not user: response = {"success": False, "msg": "User not found."} return make_response(jsonify(response)), 404 try: team_ids = get_teams_of_user_in_project(user['id'], project_id) for id in team_ids: delete_by_user_id_team_id(user['id'], id) project_members = count_users_in_team(id) if project_members == 0: delete_team(id) response = {"success": True, "msg": "ProjectMember deleted."} return make_response(jsonify(response)), 200 except Exception: response = { "success": False, "msg": "Could not delete projectmember from all teams" } return make_response(jsonify(response)), 500 except Exception: 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/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 post(self, project_id, team_id): """ Handle POST request for this view. Url --> /api/v1/team/add_team_member/<int:project_id>/<int:team_id> """ # getting JSON data from request post_data = request.get_json(silent=True, force=True) current_user = get_jwt_identity() roles = get_user_roles(current_user, project_id) if "admin" not in roles: print("Error occured: user not admin") response = {"success": False, "msg": "User not admin."} return make_response(jsonify(response)), 403 try: user_email = post_data["member_email"] user_obj = find_by_email(user_email) user = to_json(user_obj) if not user: response = {"success": False, "msg": "User not found."} return make_response(jsonify(response)), 404 team = find_by_id(team_id) if not team: response = {"success": False, "msg": "Team does not exist."} return make_response(jsonify(response)), 404 project_member = save_projectmember(user['id'], team['id']) team_new = find_by_id(project_member['team_id']) res = {"project_member": project_member, "team": team_new} response = { "success": True, "msg": "Team member added.", "body": res } return make_response(jsonify(response)), 201 except Exception: response = {"success": False, "msg": "Could not save team member."} return make_response(jsonify(response)), 500
def post(self, project_id, team_id): """ Handle POST request for this view. Url --> /api/v1/team/remove_team_member/<int:project_id>/<int:team_id> """ # getting JSON data from request post_data = request.get_json(silent=True, force=True) try: user_email = post_data["member_email"] except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 try: user_obj = find_by_email(user_email) user = to_json(user_obj) if not user: response = {"success": False, "msg": "User not found."} return make_response(jsonify(response)), 404 try: delete_by_user_id_team_id(user['id'], team_id) project_members = count_users_in_team(team_id) if project_members == 0: delete_team(team_id) response = {"success": True, "msg": "Team member deleted."} return make_response(jsonify(response)), 200 except Exception: response = { "success": False, "msg": "Could not delete projectmember from all teams" } return make_response(jsonify(response)), 500 except Exception: response = {"success": False, "msg": "Something went wrong!"} return make_response(jsonify(response)), 500
def post(self): # Querying the database with requested email data = request.get_json(silent=True, force=True) try: name = data["name"] username = data["username"] email = data["email"] except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 user = find_by_email(email) if not user: # There is no user so we'll try to register them user = User(email=email, username=username, name=name) try: user_new = save(user) except Exception: # An error occured, therefore return a string msg # containing the error response = {"success": False, "msg": "Something went wrong!"} return make_response(jsonify(response)), 500 access_token = create_access_token(identity=user_new['id'], fresh=True) refresh_token = create_refresh_token(user_new['id']) if not access_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": user_new } return make_response(jsonify(response)), 201 else: # There is an existing user, Let him login. access_token = create_access_token(identity=user.id, fresh=True) refresh_token = create_refresh_token(user.id) 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)), 202
def post(self, project_id): """ Handle POST request for this view. Url --> /api/v1/project/add_project_member/<int:project_id> """ # getting JSON data from request post_data = request.get_json(silent=True, force=True) current_user = get_jwt_identity() roles = get_user_roles(current_user, project_id) if "admin" not in roles: print("Error occured: user not admin") response = {"success": False, "msg": "User not admin."} return make_response(jsonify(response)), 403 try: user_email = post_data["member_email"] team_name = post_data["team_name"] role = post_data["role"] except KeyError as err: response = { "success": False, "msg": f'{str(err)} key is not present' } return make_response(jsonify(response)), 400 try: if role not in allowed_teams: response = { "success": False, "msg": "Team role is not allowed." } return make_response(jsonify(response)), 400 user_obj = find_by_email(user_email) user = to_json(user_obj) if not user: response = {"success": False, "msg": "User not found."} return make_response(jsonify(response)), 404 roles = get_user_roles(user['id'], project_id) if "admin" in roles: print("Error occured: user already admin") response = {"success": False, "msg": "User already admin."} return make_response(jsonify(response)), 400 team_exist = find_by_team_name(team_name) if team_exist: project_member_exist = find_by_user_id_team_id( user['id'], team_exist['id']) if team_exist and team_name == "admin": response = { "success": False, "msg": "Admin role already exists." } return make_response(jsonify(response)), 400 if team_exist and project_member_exist: response = { "success": False, "msg": "Projectmember already exists in this team." } return make_response(jsonify(response)), 400 if not team_exist: try: team = Team(team_name=team_name, project_id=project_id, role=role) team_exist = save_team(team) except Exception as err: print("Error occured: ", err) response = { "success": False, "msg": "Could not save a team." } return make_response(jsonify(response)), 400 try: project_member = ProjectMember(user_id=user['id'], team_id=team_exist['id']) project_member_new = save_project_member(project_member) user_added = find_by_user_id(project_member_new['user_id']) response = { "success": True, "msg": "ProjectMember added.", "body": user_added } return make_response(jsonify(response)), 201 except Exception as err: print("Error occured: ", err) response = { "success": False, "msg": "Could not save the projectmember." } return make_response(jsonify(response)), 500 except Exception: response = {"success": False, "msg": "Something went wrong!!"} return make_response(jsonify(response)), 500