Пример #1
0
    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
Пример #2
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