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 put(self, project_id, team_id):
        """Handle PUT request for this view. Url --> /api/v1/team/team_info/<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:
            team_name = post_data["teamname"]
            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

            if not (team_id):
                response = {"success": False, "msg": "Team id is not provided"}
                return make_response(jsonify(response)), 400

            team = find_by_id(team_id)

            if not team:
                response = {"success": False, "msg": "Team not present."}
                return make_response(jsonify(response)), 404

            data = {"team_name": team_name, "role": role}

            team_new = update_team(team_id, data)

            response = {
                "success": True,
                "msg": "Team updated!!",
                "body": team_new
            }
            return make_response(jsonify(response)), 201
        except Exception as err:
            print("Error occured: ", err)
            response = {"success": False, "msg": "Something went wrong!!"}
            return make_response(jsonify(response)), 500
    def get(self, project_id, team_id):
        """Handle GET request for this view. Url --> /api/v1/team/team_info/<int:project_id>/<int:team_id>"""
        try:
            if not team_id:
                response = {"success": False, "msg": "Team id not provided"}
                return make_response(jsonify(response)), 400
            team = find_by_id(team_id)
            if not team:
                response = {"success": False, "msg": "Team not found."}
                return make_response(jsonify(response)), 404

            response = {"success": True, "msg": "Team found.", "body": team}
            return make_response(jsonify(response)), 200
        except Exception:
            response = {"success": False, "msg": "Something went wrong!!"}
            return make_response(jsonify(response)), 500
Example #4
0
    def get(self, team_id):
        try:
            team = find_by_id(team_id)
            if not team:
                response = {'success': False, 'msg': 'Team does not exist'}
                return make_response(jsonify(response)), 404

            messages = get_all_messages(team_id)
            response = {
                'success': True,
                'body': messages,
            }
            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