<commit_before>"""Scripts to migrate legacy objects in existing databases."""
import logging  # pragma: no cover

from substanced.util import find_service
from zope.interface import alsoProvides
from zope.interface import directlyProvides

from adhocracy_core.interfaces import search_query


from adhocracy_core.evolution import log_migration
from adhocracy_core.evolution import migrate_new_sheet
from adhocracy_core.evolution import _search_for_interfaces
from adhocracy_core.utils import get_sheet
from adhocracy_meinberlin.resources.kiezkassen import IProposalVersion
import adhocracy_core.sheets
import adhocracy_meinberlin.sheets

logger = logging.getLogger(__name__)  # pragma: no cover


def use_adhocracy_core_title_sheet(root):  # pragma: no cover
    """Migrate kiezkassen proposal to adhocracy_core title sheet.

    Add title sheet.
    Remove title field from proposal sheet.
    """
    migrate_new_sheet(root, IProposalVersion,
                      adhocracy_core.sheets.title.ITitle,
                      adhocracy_meinberlin.sheets.kiezkassen.IProposal,
                      remove_isheet_old=False,
                      fields_mapping=[('title', 'title')])


def use_adhocracy_core_description_sheet(root):  # pragma: no cover
    """Migrate kiezkassen proposal to description sheet.

    Add description sheet.
    Remove detail field from proposal sheet.
    """
    migrate_new_sheet(root, IProposalVersion,
                      adhocracy_core.sheets.description.IDescription,
                      adhocracy_meinberlin.sheets.kiezkassen.IProposal,
                      remove_isheet_old=False,
                      fields_mapping=[('description', 'detail')])


@log_migration
def remove_meinberlin_workflow_assignment_sheets(root):  # pragma: no cover
    """Remove deprecated sheets.bplan/kiezkasse.IWorkflowAssignment."""
    from adhocracy_core.interfaces import IResource
    from adhocracy_core.sheets.workflow import IWorkflowAssignment
    from adhocracy_meinberlin import sheets
    migrate_new_sheet(root,
                      IResource,
                      IWorkflowAssignment,
                      sheets.bplan.IWorkflowAssignment,
                      remove_isheet_old=True,
                      )
    migrate_new_sheet(root,
                      IResource,
                      IWorkflowAssignment,
                      sheets.bplan.IPrivateWorkflowAssignment,
                      remove_isheet_old=True,
                      )
    migrate_new_sheet(root,
                      IResource,
                      IWorkflowAssignment,
                      sheets.kiezkassen.IWorkflowAssignment,
                      remove_isheet_old=True,
                      )


@log_migration
def migrate_stadtforum_proposals_to_ipolls(root):  # pragma: no cover
    """Migrate stadtforum proposals to ipolls."""
    from adhocracy_core.resources.proposal import IProposal
    from adhocracy_meinberlin.resources.stadtforum import IProcess
    from adhocracy_meinberlin.resources.stadtforum import IPoll
    from adhocracy_meinberlin.resources.stadtforum import poll_meta
    catalogs = find_service(root, 'catalogs')
    query = search_query._replace(interfaces=(IProcess,),
                                  resolve=True)
    stadtforums = catalogs.search(query).elements
    for stadtforum in stadtforums:
        proposals_query = search_query._replace(interfaces=(IProposal,),
                                                root=stadtforum,
                                                resolve=True)
        proposals = catalogs.search(proposals_query).elements
        for proposal in proposals:
            directlyProvides(proposal, IPoll)
            for sheet in poll_meta.basic_sheets + poll_meta.extended_sheets:
                alsoProvides(proposal, sheet)
            catalogs.reindex_index(proposal, 'interfaces')


def change_bplan_officeworker_email_representation(root):  # pragma: no cover
    """Change bplan officeworker email representation."""
    from substanced.util import find_objectmap
    from adhocracy_core.utils import find_graph
    from adhocracy_meinberlin.resources.bplan import IProcess
    from adhocracy_meinberlin.sheets.bplan import IProcessSettings
    from adhocracy_meinberlin.sheets.bplan import IProcessPrivateSettings
    from adhocracy_meinberlin.sheets.bplan import OfficeWorkerUserReference
    migrate_new_sheet(root, IProcess, IProcessPrivateSettings)
    catalogs = find_service(root, 'catalogs')
    bplaene = _search_for_interfaces(catalogs, IProcess)
    objectmap = find_objectmap(root)
    graph = find_graph(root)
    for bplan in bplaene:
        office_worker = graph.get_references_for_isheet(
            bplan,
            IProcessSettings)['office_worker'][0]
        private_settings = get_sheet(bplan, IProcessPrivateSettings)
        private_settings.set({'office_worker_email': office_worker.email})
        objectmap.disconnect(bplan, office_worker, OfficeWorkerUserReference)


def includeme(config):  # pragma: no cover
    """Register evolution utilities and add evolution steps."""
    config.add_evolution_step(use_adhocracy_core_title_sheet)<commit_msg>Make bplan officeworker email evolution reusable<commit_after>"""Scripts to migrate legacy objects in existing databases."""
import logging  # pragma: no cover

from substanced.util import find_service
from zope.interface import alsoProvides
from zope.interface import directlyProvides

from adhocracy_core.interfaces import search_query


from adhocracy_core.evolution import log_migration
from adhocracy_core.evolution import migrate_new_sheet
from adhocracy_core.evolution import _search_for_interfaces
from adhocracy_core.utils import get_sheet
from adhocracy_meinberlin.resources.kiezkassen import IProposalVersion
import adhocracy_core.sheets
import adhocracy_meinberlin.sheets

logger = logging.getLogger(__name__)  # pragma: no cover


def use_adhocracy_core_title_sheet(root):  # pragma: no cover
    """Migrate kiezkassen proposal to adhocracy_core title sheet.

    Add title sheet.
    Remove title field from proposal sheet.
    """
    migrate_new_sheet(root, IProposalVersion,
                      adhocracy_core.sheets.title.ITitle,
                      adhocracy_meinberlin.sheets.kiezkassen.IProposal,
                      remove_isheet_old=False,
                      fields_mapping=[('title', 'title')])


def use_adhocracy_core_description_sheet(root):  # pragma: no cover
    """Migrate kiezkassen proposal to description sheet.

    Add description sheet.
    Remove detail field from proposal sheet.
    """
    migrate_new_sheet(root, IProposalVersion,
                      adhocracy_core.sheets.description.IDescription,
                      adhocracy_meinberlin.sheets.kiezkassen.IProposal,
                      remove_isheet_old=False,
                      fields_mapping=[('description', 'detail')])


@log_migration
def remove_meinberlin_workflow_assignment_sheets(root):  # pragma: no cover
    """Remove deprecated sheets.bplan/kiezkasse.IWorkflowAssignment."""
    from adhocracy_core.interfaces import IResource
    from adhocracy_core.sheets.workflow import IWorkflowAssignment
    from adhocracy_meinberlin import sheets
    migrate_new_sheet(root,
                      IResource,
                      IWorkflowAssignment,
                      sheets.bplan.IWorkflowAssignment,
                      remove_isheet_old=True,
                      )
    migrate_new_sheet(root,
                      IResource,
                      IWorkflowAssignment,
                      sheets.bplan.IPrivateWorkflowAssignment,
                      remove_isheet_old=True,
                      )
    migrate_new_sheet(root,
                      IResource,
                      IWorkflowAssignment,
                      sheets.kiezkassen.IWorkflowAssignment,
                      remove_isheet_old=True,
                      )


@log_migration
def migrate_stadtforum_proposals_to_ipolls(root):  # pragma: no cover
    """Migrate stadtforum proposals to ipolls."""
    from adhocracy_core.resources.proposal import IProposal
    from adhocracy_meinberlin.resources.stadtforum import IProcess
    from adhocracy_meinberlin.resources.stadtforum import IPoll
    from adhocracy_meinberlin.resources.stadtforum import poll_meta
    catalogs = find_service(root, 'catalogs')
    query = search_query._replace(interfaces=(IProcess,),
                                  resolve=True)
    stadtforums = catalogs.search(query).elements
    for stadtforum in stadtforums:
        proposals_query = search_query._replace(interfaces=(IProposal,),
                                                root=stadtforum,
                                                resolve=True)
        proposals = catalogs.search(proposals_query).elements
        for proposal in proposals:
            directlyProvides(proposal, IPoll)
            for sheet in poll_meta.basic_sheets + poll_meta.extended_sheets:
                alsoProvides(proposal, sheet)
            catalogs.reindex_index(proposal, 'interfaces')


def change_bplan_officeworker_email_representation(root):  # pragma: no cover
    """Change bplan officeworker email representation."""
    from substanced.util import find_objectmap
    from adhocracy_core.utils import find_graph
    from adhocracy_meinberlin.resources.bplan import IProcess
    from adhocracy_meinberlin.sheets.bplan import IProcessSettings
    from adhocracy_meinberlin.sheets.bplan import IProcessPrivateSettings
    from adhocracy_meinberlin.sheets.bplan import OfficeWorkerUserReference
    migrate_new_sheet(root, IProcess, IProcessPrivateSettings)
    catalogs = find_service(root, 'catalogs')
    bplaene = _search_for_interfaces(catalogs, IProcess)
    objectmap = find_objectmap(root)
    graph = find_graph(root)
    for bplan in bplaene:
        process_settings_ref = graph.get_references_for_isheet(
            bplan,
            IProcessSettings)
        if 'office_worker' in process_settings_ref:
            office_worker = process_settings_ref['office_worker'][0]
            private_settings = get_sheet(bplan, IProcessPrivateSettings)
            private_settings.set({'office_worker_email': office_worker.email})
            objectmap.disconnect(bplan, office_worker,
                                 OfficeWorkerUserReference)


def includeme(config):  # pragma: no cover
    """Register evolution utilities and add evolution steps."""
    config.add_evolution_step(use_adhocracy_core_title_sheet)
{'fraction_non_alphanumeric': 0.060887708649468894, 'fraction_numerical': 0.00018968133535660092, 'mean_word_length': 2.7986311239193085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6961381', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2884, 'n_words': 580}