def finish_handle(self): filepath = os.path.join(settings.FILEPATH, 'corehq','pillows','mappings','user_mapping.py') userpillow = UserPillow(create_index=False) #current index #check current index aliased_indices = userpillow.check_alias() current_index = userpillow.es_index #regenerate the mapping dict m = DEFAULT_MAPPING_WRAPPER m['properties'] = dynamic.set_properties(self.doc_class) m['_meta']['comment'] = "Autogenerated [%s] mapping from ptop_generate_mapping %s" % (self.doc_class_str, datetime.utcnow().strftime('%m/%d/%Y')) userpillow.default_mapping = m if hasattr(userpillow, '_calc_meta'): delattr(userpillow, '_calc_meta') output = [] output.append('USER_INDEX="%s_%s"' % (userpillow.es_index_prefix, userpillow.calc_meta())) output.append('USER_MAPPING=%s' % m) newcalc_index = "%s_%s" % (userpillow.es_index_prefix, userpillow.calc_meta()) print "Writing new user index and mapping: %s" % output[0] with open(filepath, 'w') as outfile: outfile.write('\n'.join(output)) if newcalc_index not in aliased_indices and newcalc_index != current_index: sys.stderr.write("\n\tWarning, current index %s is not aliased at the moment\n" % current_index) sys.stderr.write("\tCurrent live aliased index: %s\n\n" % (','.join(aliased_indices))) sys.stderr.write("File written to %s\n" % filepath)
def handle(self, *args, **options): source_uri = getattr(settings, 'PRODUCTION_COUCHDB_URI', None) target_uri = XFormInstance.get_db().uri if source_uri is None: print "\n\tNo production URI to replicate from, we're done here.\n" print "\n\tNo settings.PRODUCTION_COUCHDB_URI has been set\n" sys.exit() input_query = options['query_string'] if not input_query: print "\tRunning default query for user.is_superuser" query_string = BASE_QUERY else: query_string = input_query print "\n\tRunning user query: %s" % query_string user_pillow = UserPillow() user_es = user_pillow.get_es() doc_ids = [ res['_id'] for res in get_query_results(user_es, query_string) ] do_replicate = options['makeitso'] repl_params = {'doc_ids': doc_ids} if 'cancel' in args: repl_params['cancel'] = True print "\n\tSending a replication cancel notification to server" else: print "\n\tStarting staging replication from prod" if do_replicate: server = CommCareUser.get_db().server server.replicate(source_uri, target_uri, **repl_params) AuditEvent.audit_command() else: print "\n\tReplication dry run with params: %s" % repl_params
def handle(self, *args, **options): source_uri = getattr(settings, 'PRODUCTION_COUCHDB_URI', None) target_uri = XFormInstance.get_db().uri if source_uri is None: print "\n\tNo production URI to replicate from, we're done here.\n" print "\n\tNo settings.PRODUCTION_COUCHDB_URI has been set\n" sys.exit() input_query = options['query_string'] if not input_query: print "\tRunning default query for user.is_superuser" query_string = BASE_QUERY else: query_string = input_query print "\n\tRunning user query: %s" % query_string user_pillow = UserPillow() user_es = user_pillow.get_es() doc_ids = [res['_id'] for res in get_query_results(user_es, query_string)] do_replicate = options['makeitso'] repl_params = { 'doc_ids': doc_ids } if 'cancel' in args: repl_params['cancel'] = True print "\n\tSending a replication cancel notification to server" else: print "\n\tStarting staging replication from prod" if do_replicate: server = CommCareUser.get_db().server server.replicate(source_uri, target_uri, **repl_params) AuditEvent.audit_command() else: print "\n\tReplication dry run with params: %s" % repl_params
def finish_handle(self): filepath = os.path.join(settings.FILEPATH, 'corehq','pillows','mappings','user_mapping.py') userpillow = UserPillow(create_index=False) #current index #check current index aliased_indices = userpillow.check_alias() current_index = userpillow.es_index #regenerate the mapping dict m = DEFAULT_MAPPING_WRAPPER user_special_types = { "domain": type_exact_match_string("domain", dual=True), "username": type_exact_match_string("username", dual=True), "user_data": {"dynamic": True, "type": "object"}, } user_special_types["username"]["fields"]["exact"]["include_in_all"] = False user_special_types["username"]["fields"]["username"]["analyzer"] = "simple" m['properties'] = dynamic.set_properties(self.doc_class, custom_types=user_special_types) m['_meta']['comment'] = "Autogenerated [%s] mapping from ptop_generate_mapping %s" % (self.doc_class_str, datetime.utcnow().strftime('%m/%d/%Y')) m['_all'] = {"analyzer": "simple"} userpillow.default_mapping = m if hasattr(userpillow, '_calc_meta'): delattr(userpillow, '_calc_meta') output = [] output.append('USER_INDEX="%s_%s"' % (userpillow.es_index_prefix, userpillow.calc_meta())) output.append('USER_MAPPING=%s' % pprint.pformat(m)) newcalc_index = "%s_%s" % (userpillow.es_index_prefix, userpillow.calc_meta()) print "Writing new user index and mapping: %s" % output[0] with open(filepath, 'w') as outfile: outfile.write('\n'.join(output)) if newcalc_index not in aliased_indices and newcalc_index != current_index: sys.stderr.write("\n\tWarning, current index %s is not aliased at the moment\n" % current_index) sys.stderr.write("\tCurrent live aliased index: %s\n\n" % (','.join(aliased_indices))) sys.stderr.write("File written to %s\n" % filepath)
def finish_handle(self): filepath = os.path.join(settings.FILEPATH, 'corehq', 'pillows', 'mappings', 'user_mapping.py') userpillow = UserPillow(create_index=False) #current index #check current index aliased_indices = userpillow.check_alias() current_index = userpillow.es_index #regenerate the mapping dict m = DEFAULT_MAPPING_WRAPPER user_special_types = { "domain": type_exact_match_string("domain", dual=True), "username": type_exact_match_string("username", dual=True), "user_data": { "dynamic": True, "type": "object" }, } user_special_types["username"]["fields"]["exact"][ "include_in_all"] = False user_special_types["username"]["fields"]["username"][ "analyzer"] = "simple" m['properties'] = dynamic.set_properties( self.doc_class, custom_types=user_special_types) m['_meta'][ 'comment'] = "Autogenerated [%s] mapping from ptop_generate_mapping %s" % ( self.doc_class_str, datetime.utcnow().strftime('%m/%d/%Y')) m['_all'] = {"analyzer": "simple"} userpillow.default_mapping = m if hasattr(userpillow, '_calc_meta'): delattr(userpillow, '_calc_meta') output = [] output.append('USER_INDEX="%s_%s"' % (userpillow.es_index_prefix, userpillow.calc_meta())) output.append('USER_MAPPING=%s' % pprint.pformat(m)) newcalc_index = "%s_%s" % (userpillow.es_index_prefix, userpillow.calc_meta()) print "Writing new user index and mapping: %s" % output[0] with open(filepath, 'w') as outfile: outfile.write('\n'.join(output)) if newcalc_index not in aliased_indices and newcalc_index != current_index: sys.stderr.write( "\n\tWarning, current index %s is not aliased at the moment\n" % current_index) sys.stderr.write("\tCurrent live aliased index: %s\n\n" % (','.join(aliased_indices))) sys.stderr.write("File written to %s\n" % filepath)
class GroupToUserPillowTest(SimpleTestCase): domain = 'grouptouser-pillowtest-domain' def setUp(self): ensure_index_deleted(USER_INDEX) self.user_pillow = UserPillow() self.es_client = get_es_new() self.user_id = 'user1' self._create_es_user() def _create_es_user(self): user = CommCareUser( _id=self.user_id, domain=self.domain, username='******', first_name='Harry', last_name='Casual', ) self.user_pillow.change_transport(user.to_json()) self.es_client.indices.refresh(USER_INDEX) return user def tearDown(self): ensure_index_deleted(USER_INDEX) def _check_es_user(self, group_ids=None, group_names=None): self.es_client.indices.refresh(USER_INDEX) es_user = self.es_client.get(USER_INDEX, self.user_id) user_doc = es_user['_source'] if group_ids is None: self.assertTrue('__group_ids' not in user_doc) else: self.assertEqual(set(user_doc['__group_ids']), set(group_ids)) if group_names is None: self.assertTrue('__group_names' not in user_doc) else: self.assertEqual(set(user_doc['__group_names']), set(group_names)) def test_update_es_user_with_groups(self): group_doc = { 'name': 'g1', '_id': 'group1', 'users': [] } # no change if user not in group update_es_user_with_groups(group_doc) self._check_es_user(None, None) # user added to group group_doc['users'] = [self.user_id] update_es_user_with_groups(group_doc) self._check_es_user(['group1'], ['g1']) # re-process group with no change update_es_user_with_groups(group_doc) self._check_es_user(['group1'], ['g1']) # user added to new group new_group = { 'name': 'g2', '_id': 'group2', 'users': [self.user_id] } update_es_user_with_groups(new_group) self._check_es_user(['group1', 'group2'], ['g1', 'g2'])
def setUp(self): ensure_index_deleted(USER_INDEX) self.user_pillow = UserPillow() self.es_client = get_es_new() self.user_id = 'user1' self._create_es_user()