def serialize(
        self,
        obj: Union[User, Team],
        attrs: Mapping[str, Iterable[Any]],
        user: User,
        **kwargs: Any,
    ) -> Mapping[str, Mapping[str, Mapping[int, Mapping[str, str]]]]:
        """
        Convert a user or team's NotificationSettings to a python object
        comprised of primitives. This will backfill all possible notification
        settings with the appropriate defaults.

        Example: {
            "workflow": {
                "project": {
                    1: {
                        "email": "always",
                        "slack": "always"
                    },
                    2: {
                        "email": "subscribe_only",
                        "slack": "subscribe_only"
                    }
                }
            }
        }

        :param obj: A user or team.
        :param attrs: The `obj` target's NotificationSettings
        :param user: The user who will be viewing the NotificationSettings.
        :param kwargs: The same `kwargs` as `get_attrs`.
        :returns A mapping. See example.
        """
        type_option: Optional[NotificationSettingTypes] = kwargs.get("type")
        types_to_serialize = {type_option} if type_option else set(
            VALID_VALUES_FOR_KEY.keys())
        user = obj if type(obj) == User else None

        project_ids = {_.id for _ in attrs["projects"]}
        organization_ids = {_.id for _ in attrs["organizations"]}

        data = get_fallback_settings(types_to_serialize, project_ids,
                                     organization_ids, user)

        # Forgive the variable name, I wanted the following lines to be legible.
        for n in attrs["settings"]:
            # Filter out invalid notification settings.
            if (n.scope_str == "project"
                    and n.scope_identifier not in project_ids) or (
                        n.scope_str == "organization"
                        and n.scope_identifier not in organization_ids):
                continue

            # Override the notification settings.
            data[n.type_str][n.scope_str][n.scope_identifier][
                n.provider_str] = n.value_str

        return data
Beispiel #2
0
 def disable_settings_for_users(
     self, provider: ExternalProviders, users: Sequence["User"]
 ) -> None:
     """
     Given a list of users, overwrite all of their parent-independent
     notification settings to NEVER.
     TODO(mgaeta): Django 3 has self.bulk_create() which would allow us to do
      this in a single query.
     """
     for user in users:
         for type in VALID_VALUES_FOR_KEY.keys():
             self.update_or_create(
                 provider=provider.value,
                 type=type.value,
                 scope_type=NotificationScopeType.USER.value,
                 scope_identifier=user.id,
                 target_id=user.actor_id,
                 defaults={"value": NotificationSettingOptionValues.NEVER.value},
             )
Beispiel #3
0
def validate(type: NotificationSettingTypes,
             value: NotificationSettingOptionValues) -> bool:
    """ :returns boolean. True if the "value" is valid for the "type". """
    return value in VALID_VALUES_FOR_KEY.get(type, {})