def create_defaults_for(session, user, only_for=None, detail_values=None):
    """ Create a sizable amount of defaults for a new user. """

    detail_values = detail_values or {}

    if not user.openid.endswith('.fedoraproject.org'):
        log.warn("New user not from fedoraproject.org.  No defaults set.")
        return

    # the openid is of the form USERNAME.id.fedoraproject.org
    nick = user.openid.split('.')[0]

    # TODO -- make the root here configurable.
    valid_paths = fmn.lib.load_rules(root='fmn.rules')

    def rule_maker(path, **kw):
        """ Shorthand function, used inside loops below. """
        return fmn.lib.models.Rule.create_from_code_path(
            session, valid_paths, path, **kw)

    def contexts():
        names = ['email', 'irc', 'sse']
        if only_for:
            names = [only_for.name]

        for name in names:
            context = fmn.lib.models.Context.get(session, name)
            if context:
                yield context
            else:
                log.warn("No such context %r is in the DB." % name)

    # For each context, build one little and two big filters
    for context in contexts():
        pref = fmn.lib.models.Preference.load(session, user, context)
        if not pref:
            value = detail_values.get(context.name)
            pref = fmn.lib.models.Preference.create(
                session, user, context, detail_value=value)

        # Add a filter that looks for packages of this user
        filt = fmn.lib.models.Filter.create(
            session, "Events on packages that I own")
        filt.add_rule(session, valid_paths,
                      "fmn.rules:user_package_filter", fasnick=nick)

        # If this is a message about a package of mine, **and** i'm responsible
        # for it, then don't trigger this filter.  Rely on the previous one.
        filt.add_rule(session, valid_paths,
                      "fmn.rules:user_filter",
                      fasnick=nick,
                      negated=True)

        # Right off the bat, ignore all messages from non-primary kojis.
        filt.add_rule(session, valid_paths,
                      "fmn.rules:koji_instance",
                      instance="ppc,s390,arm",
                      negated=True)

        # And furthermore, exclude lots of message types
        for code_path in exclusion_packages + exclusion_mutual:
            filt.add_rule(
                session, valid_paths, "fmn.rules:%s" % code_path, negated=True)

        pref.add_filter(session, filt, notify=True)
        # END "packages I own"

        # Add a filter that looks for this user
        filt = fmn.lib.models.Filter.create(
            session, "Events referring to my username")
        filt.add_rule(session, valid_paths,
                      "fmn.rules:user_filter", fasnick=nick)

        # Right off the bat, ignore all messages from non-primary kojis.
        filt.add_rule(session, valid_paths,
                      "fmn.rules:koji_instance",
                      instance="ppc,s390,arm",
                      negated=True)

        # And furthermore exclude lots of message types
        for code_path in exclusion_username + exclusion_mutual:
            filt.add_rule(
                session, valid_paths, "fmn.rules:%s" % code_path, negated=True)

        pref.add_filter(session, filt, notify=True)
        # END "events references my username"

        # Add a taskotron filter
        filt = fmn.lib.models.Filter.create(
            session, "Critical taskotron tasks on my packages")
        filt.add_rule(session, valid_paths,
                      "fmn.rules:user_package_filter",
                      fasnick=nick)
        filt.add_rule(session, valid_paths,
                      "fmn.rules:taskotron_release_critical_task")
        filt.add_rule(session, valid_paths,
                      "fmn.rules:taskotron_task_particular_or_changed_outcome",
                      outcome='FAILED')
        pref.add_filter(session, filt, notify=True)
Create a sizable amount of defaults for a new user.