from collective.ftw.upgrade import UpgradeStep
from plone import api

# boosts from 1.60 down, 11 days old gets a boost of ca 1.0
old_default = "recip(ms(NOW/HOUR,modified),3.16e-11,0.08,0.05)"
# keeps the modified boost, except for userprofiles and contacts
# which get a boost equivalent to "modified 11 days ago"
# note that a space in this function is not allowed
new_default = "if(or(termfreq(portal_type,'ploneintranet.userprofile.userprofile'),termfreq(portal_type,'ploneintranet.contacts.contact')),1.0,recip(ms(NOW/HOUR,modified),3.16e-11,0.08,0.05))"  # noqa: E501


class DoNotSortUsersOnModified(UpgradeStep):
    """Do not sort users on modified."""

    def __call__(self):

        if (
            api.portal.get_registry_record(
                "ploneintranet.search.solr.function_query_boost", default=old_default
            )
            == old_default
        ):
            api.portal.set_registry_record(
                "ploneintranet.search.solr.function_query_boost", new_default
            )
