예제 #1
0
    def get(self, project_id):
        try:
            user_id = get_jwt_identity()
            user = find_by_user_id(user_id)

            project = find_by_project_id(project_id)
            if user_id == project['admin_id']:
                response = {
                    'success': False,
                    'msg': 'Cannot leave own project',
                }
                return make_response(jsonify(response)), 401

            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': 'Project left',
            }
            return make_response(jsonify(response)), 200
        except Exception as err:
            print(err)
            response = {
                'success': False,
                'msg': 'Something went wrong',
            }
            return make_response(jsonify(response)), 500
예제 #2
0
    def get(self):
        current_user = get_jwt_identity()

        user = find_by_user_id(current_user)

        if user is None:
            response = {"success": False, "msg": "User not found."}
            return make_response(jsonify(response)), 404

        response = {"success": True, "msg": "User found.", "body": user}
        return make_response(jsonify(response)), 200
예제 #3
0
def handle_send_message_event(data):
    try:
        body = data['body']
        team_id = data['team_id']
        user_id = data['user_id']
    except KeyError as err:
        socketio.emit('message_error', f'${str(err)} key is missing')
    user = find_by_user_id(user_id)
    username = user['username']
    message = save_message(body, team_id, user_id, username)
    socketio.emit('receive_message', message)
예제 #4
0
    def put(self):
        current_user = get_jwt_identity()

        user = find_by_user_id(current_user)

        if user is None:
            response = {"success": False, "msg": "User not found."}
            return make_response(jsonify(response)), 404

        # getting JSON data from request
        put_data = request.get_json(silent=True, force=True)

        # If no data is sent at all
        if not put_data:
            response = {
                "success": False,
                "msg": 'Please provide username to update'
            }
            return make_response(jsonify(response)), 400

        # if some data is sent but it doesn't contain username
        try:
            username = put_data.get('username')
            if not username:
                response = {
                    "success": False,
                    "msg": 'username key is not present'
                }
                return make_response(jsonify(response)), 400
        except KeyError as err:
            response = {
                "success": False,
                "msg": f'{str(err)} key is not present'
            }
            return make_response(jsonify(response)), 400

        existing_user = find_by_username(username)

        if existing_user and username != user['username']:
            # There is an existing user with that username. So we can't let the current user use this username
            response = {"success": False, "msg": "Username already taken"}
            return make_response(jsonify(response)), 400

        # Update user with new info
        updated_user = update_by_id(user['id'], {"username": username})

        response = {
            "success": True,
            "msg": "User updated successfully.",
            "body": updated_user
        }
        return make_response(jsonify(response)), 200
예제 #5
0
    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)
예제 #6
0
    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