<commit_before>from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from corehq.apps.app_manager.models import Application
from corehq.apps.locations.models import LocationFixtureConfiguration
from corehq.apps.userreports.dbaccessors import get_report_configs_for_domain, get_datasources_for_domain
from corehq.blobs.mixin import BlobMixin
from corehq.apps.userreports.models import (
    CUSTOM_REPORT_PREFIX,
    STATIC_PREFIX,
    StaticDataSourceConfiguration,
    StaticReportConfiguration,
)

types = [
    "feature_flags",
    'fixtures',
    'locations',
    'products',
    'ucr',
    'apps',
    'user_fields',
    'user_roles',
    'auto_case_updates',
]


class Command(BaseCommand):
    args = "<existing_domain> <new_domain>"
    help = """Clone a domain and it's data (settings, fixtures, locations, products, UCR, apps)"""

    option_list = (
        make_option("-i", "--include", dest="include", action="append", choices=types),
        make_option("-e", "--exclude", dest="exclude", action="append", choices=types),
        make_option("--check", dest="nocommit", action="store_true", default=False),
    )

    _report_map = None

    def _clone_type(self, options, type_):
        return (
                   not options['include'] or type_ in options['include']
               ) and type_ not in (options['exclude'] or [])

    def handle(self, *args, **options):
        self.no_commmit = options['nocommit']

        self.existing_domain, self.new_domain = args
        self.clone_domain_and_settings()

        if self._clone_type(options, 'feature_flags'):
            self.set_flags()

        if self._clone_type(options, 'fixtures'):
            self.copy_fixtures()

        if self._clone_type(options, 'locations'):
            self.copy_locations()

        if self._clone_type(options, 'products'):
            self.copy_products()

        if self._clone_type(options, 'ucr'):
            self.copy_ucr_data()

        if self._clone_type(options, 'apps'):
            self.copy_applications()

        if self._clone_type(options, 'user_fields'):
            from corehq.apps.users.views.mobile import UserFieldsView
            self._copy_custom_data(UserFieldsView.field_type)

        if self._clone_type(options, 'user_roles'):
            from corehq.apps.users.models import UserRole
            self._copy_all_docs_of_type(UserRole)

        if self._clone_type(options, 'auto_case_updates'):
            self.copy_auto_case_update_rules()

    def clone_domain_and_settings(self):
        from corehq.apps.domain.models import Domain
        new_domain_obj = Domain.get_by_name(self.new_domain)
        if new_domain_obj:
            if raw_input(
                '{} domain already exists. Do you still want to continue? [y/n]'.format(self.new_domain)
            ).lower() == 'y':
                return
            else:
                raise CommandError('abort')

        domain = Domain.get_by_name(self.existing_domain)
        domain.name = self.new_domain
        self.save_couch_copy(domain)

        from corehq.apps.commtrack.models import CommtrackConfig
        commtrack_config = CommtrackConfig.for_domain(self.existing_domain)
        if commtrack_config:
            self.save_couch_copy(commtrack_config, self.new_domain)

    def set_flags(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from corehq.feature_previews import all_previews
        from corehq.apps.hqwebapp.templatetags.hq_shared_tags import toggle_js_domain_cachebuster

        for toggle in all_toggles():
            if toggle.enabled(self.existing_domain):
                self.stdout.write('Setting flag: {}'.format(toggle.slug))
                if not self.no_commmit:
                    toggle.set(self.new_domain, True, NAMESPACE_DOMAIN)

        for preview in all_previews():
            if preview.enabled(self.existing_domain):
                self.stdout.write('Setting preview: {}'.format(preview.slug))
                if not self.no_commmit:
                    preview.set(self.new_domain, True, NAMESPACE_DOMAIN)
                    if preview.save_fn is not None:
                        preview.save_fn(self.new_domain, True)

        toggle_js_domain_cachebuster.clear(self.new_domain)

    def copy_fixtures(self):
        from corehq.apps.fixtures.models import FixtureDataItem
        from corehq.apps.fixtures.dbaccessors import get_fixture_data_types_in_domain

        fixture_types = get_fixture_data_types_in_domain(self.existing_domain)
        for fixture_type in fixture_types:
            old_id, new_id = self.save_couch_copy(fixture_type, self.new_domain)
            for item in FixtureDataItem.by_data_type(self.existing_domain, old_id):
                item.data_type_id = new_id
                self.save_couch_copy(item, self.new_domain)

        # TODO: FixtureOwnership - requires copying users & groups

    def copy_locations(self):
        from corehq.apps.locations.models import LocationType, SQLLocation
        from corehq.apps.locations.views import LocationFieldsView

        self._copy_custom_data(LocationFieldsView.field_type)

        location_types = LocationType.objects.by_domain(self.existing_domain)
        location_types_map = {}
        for location_type in location_types:
            if location_type.parent_type_id:
                location_type.parent_type_id = location_types_map[location_type.parent_type_id]
            old_id, new_id = self.save_sql_copy(location_type, self.new_domain)
            location_types_map[old_id] = new_id

        # MPTT sorts this queryset so we can just save in the same order
        new_loc_ids_by_code = {}
        for loc in SQLLocation.active_objects.filter(domain=self.existing_domain):
            loc.location_id = ''
            loc.parent_id = new_loc_ids_by_code[loc.parent.site_code] if loc.parent_id else None
            _, new_id = self.save_sql_copy(loc, self.new_domain)
            new_loc_ids_by_code[loc.site_code] = new_id

        existing_fixture_config = LocationFixtureConfiguration.for_domain(self.existing_domain)
        self.save_sql_copy(existing_fixture_config, self.new_domain, domain_is_pk=True)

    def copy_products(self):
        from corehq.apps.products.models import Product<commit_msg>copy calendar fixture<commit_after>from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from corehq.apps.app_manager.models import Application
from corehq.apps.calendar_fixture.models import CalendarFixtureSettings
from corehq.apps.locations.models import LocationFixtureConfiguration
from corehq.apps.userreports.dbaccessors import get_report_configs_for_domain, get_datasources_for_domain
from corehq.blobs.mixin import BlobMixin
from corehq.apps.userreports.models import (
    CUSTOM_REPORT_PREFIX,
    STATIC_PREFIX,
    StaticDataSourceConfiguration,
    StaticReportConfiguration,
)

types = [
    "feature_flags",
    'fixtures',
    'locations',
    'products',
    'ucr',
    'apps',
    'user_fields',
    'user_roles',
    'auto_case_updates',
]


class Command(BaseCommand):
    args = "<existing_domain> <new_domain>"
    help = """Clone a domain and it's data (settings, fixtures, locations, products, UCR, apps)"""

    option_list = (
        make_option("-i", "--include", dest="include", action="append", choices=types),
        make_option("-e", "--exclude", dest="exclude", action="append", choices=types),
        make_option("--check", dest="nocommit", action="store_true", default=False),
    )

    _report_map = None

    def _clone_type(self, options, type_):
        return (
                   not options['include'] or type_ in options['include']
               ) and type_ not in (options['exclude'] or [])

    def handle(self, *args, **options):
        self.no_commmit = options['nocommit']

        self.existing_domain, self.new_domain = args
        self.clone_domain_and_settings()

        if self._clone_type(options, 'feature_flags'):
            self.set_flags()

        if self._clone_type(options, 'fixtures'):
            self.copy_fixtures()

        if self._clone_type(options, 'locations'):
            self.copy_locations()

        if self._clone_type(options, 'products'):
            self.copy_products()

        if self._clone_type(options, 'ucr'):
            self.copy_ucr_data()

        if self._clone_type(options, 'apps'):
            self.copy_applications()

        if self._clone_type(options, 'user_fields'):
            from corehq.apps.users.views.mobile import UserFieldsView
            self._copy_custom_data(UserFieldsView.field_type)

        if self._clone_type(options, 'user_roles'):
            from corehq.apps.users.models import UserRole
            self._copy_all_docs_of_type(UserRole)

        if self._clone_type(options, 'auto_case_updates'):
            self.copy_auto_case_update_rules()

    def clone_domain_and_settings(self):
        from corehq.apps.domain.models import Domain
        new_domain_obj = Domain.get_by_name(self.new_domain)
        if new_domain_obj:
            if raw_input(
                '{} domain already exists. Do you still want to continue? [y/n]'.format(self.new_domain)
            ).lower() == 'y':
                return
            else:
                raise CommandError('abort')

        domain = Domain.get_by_name(self.existing_domain)
        domain.name = self.new_domain
        self.save_couch_copy(domain)

        from corehq.apps.commtrack.models import CommtrackConfig
        commtrack_config = CommtrackConfig.for_domain(self.existing_domain)
        if commtrack_config:
            self.save_couch_copy(commtrack_config, self.new_domain)

    def set_flags(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from corehq.feature_previews import all_previews
        from corehq.apps.hqwebapp.templatetags.hq_shared_tags import toggle_js_domain_cachebuster

        for toggle in all_toggles():
            if toggle.enabled(self.existing_domain):
                self.stdout.write('Setting flag: {}'.format(toggle.slug))
                if not self.no_commmit:
                    toggle.set(self.new_domain, True, NAMESPACE_DOMAIN)

        for preview in all_previews():
            if preview.enabled(self.existing_domain):
                self.stdout.write('Setting preview: {}'.format(preview.slug))
                if not self.no_commmit:
                    preview.set(self.new_domain, True, NAMESPACE_DOMAIN)
                    if preview.save_fn is not None:
                        preview.save_fn(self.new_domain, True)

        toggle_js_domain_cachebuster.clear(self.new_domain)

    def copy_fixtures(self):
        from corehq.apps.fixtures.models import FixtureDataItem
        from corehq.apps.fixtures.dbaccessors import get_fixture_data_types_in_domain

        fixture_types = get_fixture_data_types_in_domain(self.existing_domain)
        for fixture_type in fixture_types:
            old_id, new_id = self.save_couch_copy(fixture_type, self.new_domain)
            for item in FixtureDataItem.by_data_type(self.existing_domain, old_id):
                item.data_type_id = new_id
                self.save_couch_copy(item, self.new_domain)

        # TODO: FixtureOwnership - requires copying users & groups

        existing_fixture_config = CalendarFixtureSettings.for_domain(self.existing_domain)
        self.save_sql_copy(existing_fixture_config, self.new_domain, domain_is_pk=True)

    def copy_locations(self):
        from corehq.apps.locations.models import LocationType, SQLLocation
        from corehq.apps.locations.views import LocationFieldsView

        self._copy_custom_data(LocationFieldsView.field_type)

        location_types = LocationType.objects.by_domain(self.existing_domain)
        location_types_map = {}
        for location_type in location_types:
            if location_type.parent_type_id:
                location_type.parent_type_id = location_types_map[location_type.parent_type_id]
            old_id, new_id = self.save_sql_copy(location_type, self.new_domain)
            location_types_map[old_id] = new_id

        # MPTT sorts this queryset so we can just save in the same order
        new_loc_ids_by_code = {}
        for loc in SQLLocation.active_objects.filter(domain=self.existing_domain):
            loc.location_id = ''
            loc.parent_id = new_loc_ids_by_code[loc.parent.site_code] if loc.parent_id else None
            _, new_id = self.save_sql_copy(loc, self.new_domain)
            new_loc_ids_by_code[loc.site_code] = new_id

        existing_fixture_config = LocationFixtureConfiguration.for_domain(self.existing_domain)
        self.save_sql_copy(existing_fixture_config, self.new_domain, domain_is_pk=True)

    def copy_products(self):
        from corehq.apps.products.models import Product
{'fraction_non_alphanumeric': 0.08800803585226395, 'fraction_numerical': 0.0, 'mean_word_length': 2.862429125634139, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7146771', 'n_tokens_mistral': 3922, 'n_tokens_neox': 3814, 'n_words': 710}