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): """Handle POST request for this view. Url --> /api/v1/auth/register""" # getting JSON data from request post_data = request.get_json(silent=True, force=True) try: name = post_data["name"] username = post_data["username"] email = post_data["email"] password = post_data["password"] password2 = post_data["password2"] 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 email user = find_by_email(email) if user: # There is an existing user. We don't want to register users twice # Return a msg to the user telling them that they they already # exist response = { "success": False, "msg": "Email already exists. Please login." } return make_response(jsonify(response)), 400 # Querying the database with requested username user = find_by_username(username) if user: # There is an existing username. We don't want to register users twice # Return a msg to the user telling them that the username already # exist response = { "success": False, "msg": "UserName already exists. Please choose a different one." } return make_response(jsonify(response)), 400 # There is no user so we'll try to register them # If passwords don't match, return error if password != password2: response = { "success": False, "msg": "Both passwords does not match" } return make_response(jsonify(response)), 400 """Save the new User.""" try: user = User(email=email, password=password, name=name, username=username) user = save(user) 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": "You registered successfully. Please log in.", "body": user } # return a response notifying the user that they registered # successfully return make_response(jsonify(response)), 201
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 wrap(*args, **kwargs): try: result = fun(*args, **kwargs) status_code = int(result[1]) # If unsuccessful request then no need to store log if status_code != 200 and status_code != 201: return result # Get user details user_id = get_jwt_identity() user = find_by_user_id(user_id) # Get url, method and project id url = request.path method = request.method project_id = kwargs.get('project_id') if kwargs.get( 'project_id') else -1 # Initialize with default values message = url category = 'misc' entity_type = None entity_id = None # Projects controller if url == f'/api/v1/project/project_info/{project_id}': if method == 'PUT': # Edit Project message = f'Project details edited by {user["username"]}' category = 'general' elif method == 'DELETE': # Delete Project # In this case, delete all project related logs delete_all_project_logs(project_id) return result if url == f'/api/v1/project/add_project_member/{project_id}': # Add Project Member data = request.get_json(silent=True, force=True) member_email = data['member_email'] member = find_by_email(member_email) message = f'{member.username} has been added to the project' category = 'general' if url == f'/api/v1/project/remove_project_member/{project_id}': data = request.get_json(silent=True, force=True) member_email = data['member_email'] member = find_by_email(member_email) message = f'{member.username} has been removed from the project' category = 'general' if url == f'/api/v1/project/leave/{project_id}': message = f'{user["username"]} has left the project' category = 'general' if url == f'/api/v1/project/make_admin/{project_id}': message = f'{user["username"]} has been made admin' category = 'general' if url == f'/api/v1/project/remove_admin/{project_id}': message = f'{user["username"]} has been removed as admin' category = 'general' # Teams controller if url.startswith(f'/api/v1/team/team_info/{project_id}/'): team_id = kwargs.get('team_id') team = find_team_by_id(team_id) if method == 'PUT': # Update Team message = f'Team {team["team_name"]} has been updated' category = 'general' elif method == 'DELETE': # Delete Team message = f'Team with id {team_id} has been deleted' category = 'general' if url.startswith(f'/api/v1/team/add_team_member/{project_id}'): team_id = kwargs.get('team_id') team = find_team_by_id(team_id) data = request.get_json(silent=True, force=True) member_email = data['member_email'] member = find_by_email(member_email) message = f'{member.username} has been added to team {team["team_name"]}' category = 'general' if url.startswith(f'/api/v1/team/remove_team_member/{project_id}'): team_id = kwargs.get('team_id') team = find_team_by_id(team_id) data = request.get_json(silent=True, force=True) member_email = data['member_email'] member = find_by_email(member_email) message = f'{member.username} has been removed from team {team["team_name"]}' category = 'general' # Images controller if url.startswith(f'/api/v1/image/create/{project_id}'): message = 'New image has been added to project' category = 'images' if url.startswith(f'/api/v1/image/delete/{project_id}'): message = 'Image(s) have been deleted from the project' category = 'images' if url.startswith(f'/api/v1/image/update/'): image_id = kwargs.get('image_id') data = request.get_json(silent=True, force=True) message = f'Labels for image with id {image_id} have been updated' project_id = data['project_id'] category = 'image labelling' entity_id = image_id entity_type = 'image' # Labels controller if url == f'/api/v1/label/create/{project_id}': message = 'New label has been created' category = 'labels' if url.startswith('/api/v1/label/label_info/'): label_id = kwargs.get('label_id') if method == 'DELETE': message = f'Label with id {label_id} has been deleted' category = 'labels' elif method == 'PUT': label = find_label_by_id(label_id) message = f'Label {label["label_name"]} has been updated' category = 'labels' entity_type = 'label' entity_id = label_id # MLClassifications controller if url == '/api/v1/mlclassifier': data = request.get_json(silent=True, force=True) project_id = data['projectId'] message = f'New model {data["name"]} has been created' category = 'models' ml_classifier_id = kwargs.get('mlclassifier_id') if kwargs.get( 'mlclassifier_id') else -1 if url == f'/api/v1/mlclassifier/{ml_classifier_id}': if method == 'PUT': ml_classifier = find_mlclassifer_by_id(ml_classifier_id) project_id = ml_classifier['project_id'] message = f'Model {ml_classifier["name"]} has been updated' category = 'models' entity_id = ml_classifier_id entity_type = 'model' elif method == 'DELETE': message = f'Model with id {ml_classifier_id} has been deleted' category = 'models' if url == f'/api/v1/mlclassifer/upload/{ml_classifier_id}': ml_classifier = find_mlclassifer_by_id(ml_classifier_id) project_id = ml_classifier['project_id'] message = f'Model {ml_classifier["name"]} has been uploaded' category = 'models' entity_id = ml_classifier_id entity_type = 'model' if url.startswith(f'/api/v1/mlclassifer/export'): ml_classifier = find_mlclassifer_by_id(ml_classifier_id) project_id = ml_classifier['project_id'] message = f'Model {ml_classifier["name"]} has been exported' category = 'models' entity_id = ml_classifier_id entity_type = 'model' if url == f'/api/v1/mlclassifer/test/{ml_classifier_id}': ml_classifier = find_mlclassifer_by_id(ml_classifier_id) project_id = ml_classifier['project_id'] message = f'Model {ml_classifier["name"]} is being tested' category = 'models' entity_id = ml_classifier_id entity_type = 'model' if url == f'/api/v1/mlclassifer/train/{ml_classifier_id}': ml_classifier = find_mlclassifer_by_id(ml_classifier_id) project_id = ml_classifier['project_id'] message = f'Model {ml_classifier["name"]} is being trained' category = 'models' entity_id = ml_classifier_id entity_type = 'model' log = Log( message=message, category=category, user_id=user_id, project_id=project_id, entity_type=entity_type, entity_id=entity_id, username=user['username'], ) save_log(log) return result except Exception as e: print('Error - ', str(e)) return fun(*args, **kwargs)
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