def handle(self, **options):
     self.options = options
     user_ids = with_progress_bar(self.get_user_ids())
     iter_update(CouchUser.get_db(),
                 self.migrate_user,
                 user_ids,
                 verbose=True)
Beispiel #2
0
    def handle(self,
               domain=None,
               app_id=None,
               dry_run=False,
               ignore_deleted=False,
               verbose=False,
               **options):
        self.dry_run = dry_run
        if verbose:
            logger.setLevel(logging.DEBUG)
        else:
            logger.setLevel(logging.ERROR)

        if domain and app_id:
            app = get_app(
                domain,
                app_id)  # Sanity check, will 404 if domain doesn't match
            assert (app.doc_type == 'LinkedApplication'
                    or app.doc_type == 'LinkedApplication-Deleted')
            app_ids = set([
                v.build_id
                for v in get_all_built_app_ids_and_versions(domain, app_id)
            ])
            app_ids.add(app_id)  # in case linked app has no builds yet
        else:
            app_ids = get_doc_ids_by_class(LinkedApplication)
            if not ignore_deleted:
                app_ids += get_deleted_doc_ids_by_class(LinkedApplication)
        iter_update(LinkedApplication.get_db(),
                    self._add_overrides_for_build,
                    with_progress_bar(app_ids),
                    chunksize=1)
Beispiel #3
0
def _pop_deprecated_field(apps, schema_editor):
    app_ids = (get_doc_ids_by_class(LinkedApplication) +
               get_deleted_doc_ids_by_class(LinkedApplication))
    iter_update(LinkedApplication.get_db(),
                _pop_field,
                with_progress_bar(app_ids),
                chunksize=1)
Beispiel #4
0
 def handle(self, **options):
     roles = UserRole.view('users/roles_by_domain',
                           include_docs=False,
                           reduce=False).all()
     role_ids = [role['id'] for role in roles]
     iter_update(UserRole.get_db(),
                 _copy_permissions,
                 with_progress_bar(role_ids),
                 chunksize=1)
    def handle(self, username, **options):
        def update_username(event_dict):
            event_dict['user'] = new_username
            return DocUpdate(doc=event_dict)

        new_username = "******"
        event_ids = navigation_event_ids_by_user(username)
        iter_update(NavigationEventAudit.get_db(), update_username,
                    with_progress_bar(event_ids, len(event_ids)))
def migrate_auth_field(apps, schema_editor):
    repeater_ids = [row['id'] for row in Repeater.view(
        'repeaters/repeaters',
        include_docs=False,
        reduce=False,
        wrap_doc=False
    )]
    iter_update(
        db=Repeater.get_db(),
        fn=migrate_repeater,
        ids=repeater_ids,
    )
def migrate_auth_field(apps, schema_editor):
    repeater_ids = [
        row['id'] for row in Repeater.view('repeaters/repeaters',
                                           include_docs=False,
                                           reduce=False,
                                           wrap_doc=False)
    ]
    iter_update(
        db=Repeater.get_db(),
        fn=migrate_repeater,
        ids=repeater_ids,
    )
Beispiel #8
0
def _archive_users(location_type):
    def archive(user_doc):
        if user_doc['is_active']:
            user_doc['is_active'] = False
            return DocUpdate(user_doc)

    user_ids = (SQLLocation.objects.filter(
        location_type=location_type).values_list('user_id', flat=True))
    iter_update(CommCareUser.get_db(), archive, user_ids)

    for loc in SQLLocation.objects.filter(location_type=location_type):
        loc.user_id = ''
        loc.save()
Beispiel #9
0
def _archive_users(location_type):

    def archive(user_doc):
        if user_doc['is_active']:
            user_doc['is_active'] = False
            return DocUpdate(user_doc)

    user_ids = (SQLLocation.objects
                .filter(location_type=location_type)
                .values_list('user_id', flat=True))
    iter_update(CommCareUser.get_db(), archive, user_ids)

    for loc in SQLLocation.objects.filter(location_type=location_type):
        loc.user_id = ''
        loc.save()
Beispiel #10
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Usage is ./manage.py migrate_domain_bu [migration_file]!")

        updates = {}
        with open(args[0], 'rb') as csvfile:
            reader = csv.reader(csvfile, delimiter=',')
            for row in reader:
                updates[Domain.get_by_name(row[0])['_id']] = row[1]

        def update_domain(doc):
            Domain.wrap(doc).internal.business_unit = updates[doc['_id']]
            return DocUpdate(doc)

        iter_update(Domain.get_db(), update_domain, updates.keys(), verbose=True)
Beispiel #11
0
 def handle(self, **options):
     self.options = options
     if self.options['domain']:
         domains = [self.options['domain']]
     else:
         domains = self.get_domains() or [None]
     for domain in domains:
         app_ids = self.get_app_ids(domain)
         logger.info('migrating {} apps{}'.format(
             len(app_ids), f" in {domain}" if domain else ""))
         iter_update(Application.get_db(),
                     self._migrate_app,
                     app_ids,
                     verbose=True,
                     chunksize=self.chunk_size)
     logger.info('done')
Beispiel #12
0
 def handle(self, **options):
     self.options = options
     app_ids = self.get_app_ids()
     logger.info('migrating {} apps'.format(len(app_ids)))
     results = iter_update(Application.get_db(), self._migrate_app, app_ids, verbose=True)
     self.results_callback(results)
     logger.info('done')
Beispiel #13
0
 def handle(self, *args, **options):
     self.options = options
     app_ids = self.get_app_ids()
     logger.info('migrating {} apps'.format(len(app_ids)))
     results = iter_update(Application.get_db(), self._migrate_app, app_ids, verbose=True)
     self.results_callback(results)
     logger.info('done')
Beispiel #14
0
 def handle(self, **options):
     self.options = options
     app_ids = self.get_app_ids()
     domain = self.options.get('domain')
     logger.info('migrating {} apps{}'.format(len(app_ids), f" in {domain}" if domain else ""))
     results = iter_update(Application.get_db(), self._migrate_app, app_ids,
                           verbose=True, chunksize=self.chunk_size)
     self.results_callback(results)
     logger.info('done')
Beispiel #15
0
    def handle(self, migration_file, **options):
        name_by_map = {}
        with open(migration_file, 'rb') as csvfile:
            reader = csv.reader(csvfile, delimiter=',')
            for row in reader:
                name_by_map[row[0]] = row[1]

        domain_ids = get_domain_ids_by_names(name_by_map.keys())

        def update_domain(doc):
            domain = Domain.wrap(doc)
            new_bu = name_by_map[domain.name]
            if new_bu not in BUSINESS_UNITS:
                print('Unknown BU: domain={}, BU={}'.format(domain.name, new_bu))
                return
            domain.internal.business_unit = new_bu
            return DocUpdate(doc)

        iter_update(Domain.get_db(), update_domain, domain_ids, verbose=True)
Beispiel #16
0
    def scrub_legacy_couch_events(self, username):
        from couchdbkit.ext.django.loading import get_db
        from corehq.util.couch import DocUpdate, iter_update
        from corehq.util.log import with_progress_bar

        def update_username(event_dict):
            event_dict['user'] = self.new_username
            return DocUpdate(doc=event_dict)

        db = get_db("auditcare")
        results = db.view(
            'auditcare/urlpath_by_user_date',
            startkey=[username],
            endkey=[username, {}],
            reduce=False,
            include_docs=False,
        )
        doc_ids = {r["id"] for r in results}
        iter_update(db, update_username, with_progress_bar(doc_ids))
Beispiel #17
0
    def handle(self, *args, **options):
        def update_domain(doc):
            doc['location_restriction_for_users'] = False
            return DocUpdate(doc)
        domain_ids = [d['id'] for d in Domain.get_all(include_docs=False)]
        res = iter_update(Domain.get_db(), update_domain, domain_ids)

        print "number of domains updated:", len(res.updated_ids)
        print "Domains that already had it disabled:", res.ignored_ids
        print "Not Found:", res.not_found_ids
        print "Deleted (sure as shit better be empty):", res.deleted_ids
        print "Errored:", res.error_ids
Beispiel #18
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Usage is ./manage.py migrate_domain_bu [migration_file]!")

        name_by_map = {}
        with open(args[0], 'rb') as csvfile:
            reader = csv.reader(csvfile, delimiter=',')
            for row in reader:
                name_by_map[row[0]] = row[1]

        domain_ids = get_domain_ids_by_names(name_by_map.keys())

        def update_domain(doc):
            domain = Domain.wrap(doc)
            new_bu = name_by_map[domain.name]
            if new_bu not in BUSINESS_UNITS:
                print 'Unknown BU: domain={}, BU={}'.format(domain.name, new_bu)
                return
            domain.internal.business_unit = new_bu
            return DocUpdate(doc)

        iter_update(Domain.get_db(), update_domain, domain_ids, verbose=True)
Beispiel #19
0
 def handle(self, dry_run=False, *args, **options):
     self.dry_run = dry_run
     self.reports_using_transform = set()
     report_ids = get_doc_ids_by_class(ReportConfiguration)
     res = iter_update(
         db=ReportConfiguration.get_db(),
         fn=self.migrate_report,
         ids=with_progress_bar(report_ids),
         verbose=True,
     )
     print "Found {} reports using the transform:".format(len(self.reports_using_transform))
     print "\n".join(self.reports_using_transform)
     print "Updated the following reports:"
     print "\n".join(res.updated_ids)
    def handle(self, dry_run=False, **options):
        def _add_field(doc):
            updated = False
            log_prefix = "{} Domain {}, form unique_id {}".format(
                "[DRY RUN]" if dry_run else "", doc['domain'],
                doc['form_unique_id'])

            if doc.get('form_unique_id', None) and not doc.get('app_id', None):
                doc['app_id'] = get_app_id_from_form_unique_id(
                    doc['domain'], doc['form_unique_id'])
                if doc['app_id']:
                    updated = True
                    logger.info("{}: Updated {} to use app id {}".format(
                        log_prefix, doc['_id'], doc['app_id']))
                else:
                    logger.info("{}: Could not find app".format(log_prefix))

            for action in doc.get('actions', []):
                if action.get('form_unique_id',
                              None) and not action.get('app_id', None):
                    action['app_id'] = get_app_id_from_form_unique_id(
                        doc['domain'], action['form_unique_id'])
                    if action['app_id']:
                        updated = True
                        logger.info(
                            "{}: Updated action in {} to use app id {}".format(
                                log_prefix, doc['_id'], action['app_id']))
                    else:
                        logger.info(
                            "{}: Could not find app".format(log_prefix))

            if updated and not dry_run:
                return DocUpdate(doc)

        doc_ids = get_doc_ids_by_class(
            SurveyKeyword) + get_deleted_doc_ids_by_class(SurveyKeyword)
        iter_update(SurveyKeyword.get_db(), _add_field, doc_ids)
Beispiel #21
0
 def handle(self, dry_run=False, **options):
     self.dry_run = dry_run
     self.reports_using_transform = set()
     report_ids = get_doc_ids_by_class(ReportConfiguration)
     res = iter_update(
         db=ReportConfiguration.get_db(),
         fn=self.migrate_report,
         ids=with_progress_bar(report_ids),
         verbose=True,
     )
     print("Found {} reports using the transform:".format(
         len(self.reports_using_transform)))
     print("\n".join(self.reports_using_transform))
     print("Updated the following reports:")
     print("\n".join(res.updated_ids))
 def handle(self, **options):
     role_ids = get_all_role_ids()
     iter_update(UserRole.get_db(),
                 _copy_permissions,
                 with_progress_bar(role_ids),
                 chunksize=1)
def clean_users():
    all_ids = with_progress_bar(get_bad_user_ids())
    iter_update(CommCareUser.get_db(), clean_user, all_ids, verbose=True)
Beispiel #24
0
def update_all_roles():
    all_role_ids = get_doc_ids_by_class(UserRole)
    iter_update(UserRole.get_db(), update_role, with_progress_bar(all_role_ids), verbose=True)
def clean_users():
    all_ids = with_progress_bar(get_bad_user_ids())
    iter_update(CommCareUser.get_db(), clean_user, all_ids, verbose=True)
Beispiel #26
0
def fix_users(apps, schema_editor):
    user_ids = with_progress_bar(_get_admins_with_roles())
    iter_update(WebUser.get_db(), _remove_role, user_ids, verbose=True)
 def handle(self, **options):
     self.options = options
     user_ids = with_progress_bar(self.get_user_ids())
     iter_update(CouchUser.get_db(), self.migrate_user, user_ids, verbose=True)