<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals

from django.contrib import messages
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _
from django.views.generic import FormView

from casexml.apps.case.xml import V2
from casexml.apps.phone.data_providers.case.clean_owners import CleanOwnerSyncPayload
from casexml.apps.phone.restore import RestoreContent, RestoreResponse, RestoreState, RestoreParams
from casexml.apps.phone.xml import get_registration_element
from corehq.apps.domain.decorators import domain_admin_required
from corehq.apps.domain.models import Domain
from corehq.apps.domain.views import BaseDomainView
from corehq.util.timer import TimingContext
from corehq.util import reverse

from .forms import MigrationForm
from .migration import perform_migration


@method_decorator(domain_admin_required, name='dispatch')
class BaseMigrationView(BaseDomainView):
    section_name = "Case Migrations"

    @property
    def section_url(self):
        return reverse(MigrationView.urlname, args=(self.domain,))


# TODO feature flag
class MigrationView(BaseMigrationView, FormView):
    urlname = 'case_migration'
    template_name = 'case_migrations/migration.html'
    form_class = MigrationForm

    def form_valid(self, form):
        perform_migration(
            self.domain,
            form.cleaned_data['case_type'],
            form.cleaned_data['migration_xml'],
        )
        messages.add_message(self.request, messages.SUCCESS,
                             _('Migration submitted successfully!'))
        return HttpResponseRedirect(self.page_url)


def migration_restore(request, domain, case_id):
    domain_obj = Domain.get_by_name(domain)
    restore_user = request.couch_user
    restore_params = RestoreParams(device_id="case_migration", version=V2)
    restore_state = RestoreState(domain_obj, restore_user.to_ota_restore_user(domain), restore_params)
    restore_state.start_sync()
    timing_context = TimingContext('migration-restore-{}-{}'.format(domain, restore_user.username))
    with RestoreContent(restore_user.username) as content:
        content.append(get_registration_element(restore_user))

        sync_payload = CleanOwnerSyncPayload(timing_context, {case_id}, restore_state)
        sync_payload.extend_response(content)

        payload = content.get_fileobj()<commit_msg>Sync all related cases in restore<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals

from django.contrib import messages
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _
from django.views.generic import FormView

from casexml.apps.case.xml import V2
from casexml.apps.phone.data_providers.case.clean_owners import CleanOwnerSyncPayload
from casexml.apps.phone.restore import RestoreContent, RestoreResponse, RestoreState, RestoreParams
from casexml.apps.phone.xml import get_registration_element
from corehq.apps.domain.decorators import domain_admin_required
from corehq.apps.domain.models import Domain
from corehq.apps.domain.views import BaseDomainView
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from corehq.util.timer import TimingContext
from corehq.util import reverse

from .forms import MigrationForm
from .migration import perform_migration


@method_decorator(domain_admin_required, name='dispatch')
class BaseMigrationView(BaseDomainView):
    section_name = "Case Migrations"

    @property
    def section_url(self):
        return reverse(MigrationView.urlname, args=(self.domain,))


# TODO feature flag
class MigrationView(BaseMigrationView, FormView):
    urlname = 'case_migration'
    template_name = 'case_migrations/migration.html'
    form_class = MigrationForm

    def form_valid(self, form):
        perform_migration(
            self.domain,
            form.cleaned_data['case_type'],
            form.cleaned_data['migration_xml'],
        )
        messages.add_message(self.request, messages.SUCCESS,
                             _('Migration submitted successfully!'))
        return HttpResponseRedirect(self.page_url)


def get_related_case_ids(domain, case_id):
    from casexml.apps.case.templatetags.case_tags import get_case_hierarchy
    case = CaseAccessors(domain).get_case(case_id)
    child_cases = {c.case_id for c in get_case_hierarchy(case, {})['case_list']}
    indexed_cases = {index_info.referenced_id for index_info in case.indices}
    return child_cases | indexed_cases


def migration_restore(request, domain, case_id):
    domain_obj = Domain.get_by_name(domain)
    restore_user = request.couch_user
    restore_params = RestoreParams(device_id="case_migration", version=V2)
    restore_state = RestoreState(domain_obj, restore_user.to_ota_restore_user(domain), restore_params)
    restore_state.start_sync()
    timing_context = TimingContext('migration-restore-{}-{}'.format(domain, restore_user.username))
    case_ids = get_related_case_ids(domain, case_id)
    with RestoreContent(restore_user.username) as content:
        content.append(get_registration_element(restore_user))

        sync_payload = CleanOwnerSyncPayload(timing_context, case_ids, restore_state)
        sync_payload.extend_response(content)

        payload = content.get_fileobj()
{'fraction_non_alphanumeric': 0.07555474050981112, 'fraction_numerical': 0.0007335411699981661, 'mean_word_length': 4.864516129032258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 15}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2215356', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1546, 'n_words': 328}