def get(self, request, user, notification_type):
        try:
            notification_type = FineTuningAPIKey(notification_type)
        except ValueError:
            return Response(
                {"detail": "Unknown notification type: %s." % notification_type},
                status=status.HTTP_404_NOT_FOUND,
            )

        notifications = UserNotificationsSerializer()
        return Response(
            serialize(
                user,
                request.user,
                notifications,
                notification_type=notification_type,
            )
        )
Esempio n. 2
0
    def put(self, request, user, notification_type):
        """
        Update user notification options
        ````````````````````````````````

        Updates user's notification options on a per project or organization
        basis. Expected payload is a map/dict whose key is a project or org id
        and value varies depending on `notification_type`.

        For `alerts`, `workflow`, `email` it expects a key of projectId
        For `deploy` and `reports` it expects a key of organizationId

        For `alerts`, `workflow`, `deploy`, it expects a value of:
            - "-1" = for "default" value (i.e. delete the option)
            - "0"  = disabled
            - "1"  = enabled
        For `reports` it is only a boolean.
        For `email` it is a verified email (string).

        :auth required:
        :pparam string notification_type:  One of:  alerts, workflow, reports, deploy, email
        :param map: Expects a map of id -> value (enabled or email)
        """
        try:
            notification_type = FineTuningAPIKey(notification_type)
        except ValueError:
            return Response(
                {
                    "detail":
                    "Unknown notification type: %s." % notification_type
                },
                status=status.HTTP_404_NOT_FOUND,
            )

        if notification_type == FineTuningAPIKey.REPORTS:
            return self._handle_put_reports(user, request.data)

        # Validate that all of the IDs are integers.
        try:
            ids_to_update = {int(i) for i in request.data.keys()}
        except ValueError:
            return Response(
                {
                    "detail":
                    "Invalid id value provided. Id values should be integers."
                },
                status=status.HTTP_400_BAD_REQUEST,
            )

        # Make sure that the IDs we are going to update are a subset of the
        # user's list of organizations or projects.
        parents = (user.get_orgs() if notification_type
                   == FineTuningAPIKey.DEPLOY else user.get_projects())
        parent_ids = {parent.id for parent in parents}
        if not ids_to_update.issubset(parent_ids):
            bad_ids = ids_to_update - parent_ids
            return Response(
                {
                    "detail":
                    "At least one of the requested projects is not \
                    available to this user, because the user does not belong \
                    to the necessary teams. (ids of unavailable projects: %s)"
                    % bad_ids
                },
                status=status.HTTP_403_FORBIDDEN,
            )

        if notification_type == FineTuningAPIKey.EMAIL:
            return self._handle_put_emails(user, request.data)

        return self._handle_put_notification_settings(user, notification_type,
                                                      parents, request.data)