def get_entity_list_by_type(request, entity_type): entity_type_list = [entity_type] if entity_type is None: return [] manager = get_database_manager(request.user) entities = get_all_entities(manager, entity_type_list) return convert_to_json_response([entity.short_code for entity in entities])
def geo_json_for_project(request, project_id, entity_type=None): dbm = get_database_manager(request.user) location_list = [] try: if entity_type: first_geocode_field = _get_first_geocode_field_for_entity_type( dbm, entity_type) if first_geocode_field: unique_ids = get_all_entities(dbm, [entity_type], limit=1000) location_list.extend( get_location_list_for_entities(first_geocode_field, unique_ids)) else: questionnaire = Project.get(dbm, project_id) unique_ids = by_short_codes(dbm, questionnaire.data_senders, ["reporter"], limit=1000) location_list.extend(get_location_list_for_datasenders(unique_ids)) except DataObjectNotFound: pass location_geojson = {"type": "FeatureCollection", "features": location_list} return HttpResponse(json.dumps(location_geojson))
def _load_entities_to_entity_questions(dbm, enrichable_questions): for question in enrichable_questions["entity_questions"]: all_entities = get_all_entities(dbm, [question.unique_id_type]) setattr( question, "entities", { entity.short_code: entity.data.get("name")["value"] for entity in all_entities })
def migration_to_add_location_field_to_datasenders(db_name): logger = logging.getLogger(db_name) dbm = get_db_manager(db_name) mark_as_completed(db_name) logger.info('Migration started for database %s' % db_name) all_ds = get_all_entities(dbm, ['reporter']) for ds in all_ds: _add_location_field_if_absent(ds, dbm, logger)
def load_subject_registration_data(manager): rows = get_all_entities(dbm=manager, include_docs=True) data = [] for row in rows: type = _get_entity_type_from_row(row) entity, short_code = _get_entity_for_row(manager, row, type) type = '.'.join(type) if type.lower() != 'reporter': data.append(_tabulate_data(entity, row, short_code, type)) return data
def load_all_subjects_of_type(request, entity_type="reporter"): manager = get_database_manager(request) rows = get_all_entities(dbm=manager, include_docs=True) data = [] for row in rows: type = _get_entity_type_from_row(row) entity, short_code = _get_entity_for_row(manager, row, type) type = '.'.join(type) if type.lower() == entity_type: data.append(_tabulate_data(entity, row, short_code, type)) return data
def get_DS_with_ca(dbname): dbm = get_db_manager(server="http://*****:*****@172.18.9.6:5984", database=dbname, credentials=('admin', 'admin')) all_ds = get_all_entities(dbm, ['reporter']) for ds in all_ds: if 'short_code' in ds.data.keys(): short_code = ds.data['short_code']['value'] if re.search('[A-Z]', short_code): print 'short_code is :' + short_code print 'database is :' + dbname
def load_entity_registration_data(manager, type=REPORTER, tabulate_function=tabulate_data): entity_type = entity_type_as_sequence('registration' if type == REPORTER else type) form_model = get_form_model_by_entity_type(manager, entity_type) fields, labels, codes = get_entity_type_fields(manager, form_model.form_code) entities = get_all_entities(dbm=manager, entity_type=entity_type_as_sequence(type)) data = [] for entity in entities: data.append(tabulate_function(entity, form_model, codes)) return data, fields, labels
def test_should_get_all_entities(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Mumbai"], short_code='cli002') e.save() e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Jalgaon"], short_code='cli003') e.save() e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Nasik"], short_code='cli004') uuid = e.save() all_entities = get_all_entities(self.dbm) self.assertEqual(4, len(all_entities)) self.assertEqual([uuid], [e['id'] for e in all_entities if e['id'] == uuid])
def delete_subject_types(request): manager = get_database_manager(request.user) subject_types = request.POST.get("all_ids") subject_types = subject_types.split(";") delete_registration_form(manager, subject_types) delete_type(manager, subject_types) for subject_type in subject_types: delete_mapping(manager.database_name, subject_type) ent = get_all_entities(manager, [subject_type]) for entities in ent: entities.delete() messages.success(request, _("Identification Number Type(s) successfully deleted.")) return HttpResponse(json.dumps({'success': True}))
def load_subject_registration_data(manager, filter_entities=exclude_of_type, type=REPORTER, tabulate_function=_tabulate_data): if type == REPORTER: form_model = get_form_model_by_code(manager, 'reg') else: form_model = get_form_model_by_entity_type( manager, _entity_type_as_sequence(type)) fields, labels, codes = get_entity_type_fields(manager, type) entities = get_all_entities(dbm=manager) data = [] for entity in entities: if filter_entities(entity, type): data.append(tabulate_function(entity, form_model, codes)) return data, fields, labels
def migration_to_create_search_indxes_for_datasenders(db_name): logger = logging.getLogger(db_name) dbm = get_db_manager(db_name) all_ds = get_all_entities(dbm, ['reporter']) try: mark_as_completed(db_name) for ds in all_ds: if 'short_code' in ds.data.keys(): short_code = ds.data['short_code']['value'] if re.search('[A-Z]', short_code): ds.data['short_code']['value'] = short_code.lower() ds.save() logger.info('Migrated short_code:%s' % short_code) except Exception as e: logger.exception("Failed DB: %s with message %s" % (db_name, e.message)) logger.info('Completed Migration')
def _geo_json(dbm, entity_type, entity_fields, filters, details): location_list = [] try: forward_filters, reverse_filters = _transform_filters( filters, entity_fields) first_geocode_field = get_first_geocode_field_for_entity_type( entity_fields) if first_geocode_field: unique_ids = get_all_entities(dbm, [entity_type], 1000, forward_filters, reverse_filters) details.extend(['q2']) fields_to_show = filter(lambda field: field['code'] in details, entity_fields) details_to_show_combined = [ json.loads(d.replace("'", '"')) for d in details if is_json(d.replace("'", '"')) ] field_label_combined = None if len(details_to_show_combined): field_label_combined = [] for dtsc in details_to_show_combined: field_label_combined_simple = {} field_label_combined_simple['list'] = filter( lambda field: field['code'] in dtsc['list'], entity_fields) field_label_combined_simple['list'] = _get_field_labels( field_label_combined_simple['list']) field_label_combined_simple['label'] = dtsc['label'] field_label_combined.append(field_label_combined_simple) field_label = _get_field_labels(fields_to_show) lst_for_entity = _get_detail_list_for_entities( field_label, first_geocode_field, unique_ids, field_label_combined) location_list.extend(lst_for_entity) except DataObjectNotFound: pass return {"type": "FeatureCollection", "features": location_list}
def migration_to_convert_subject_ids_to_lowercase(db_name): try: logger = logging.getLogger(db_name) dbm = get_db_manager(db_name) entity_types = get_entity_types(dbm) for entity_type in entity_types: all_entities = get_all_entities(dbm, [entity_type]) try: for entity in all_entities: if 'short_code' in entity.data.keys(): short_code = entity.data['short_code']['value'] if re.search('[A-Z]', short_code): entity.data['short_code'][ 'value'] = short_code.lower() entity.save() logger.info('Migrated short_code:%s' % short_code) except Exception as e: logger.exception(e) logger.info('Completed Migration') mark_as_completed(db_name) except Exception as e: logger.exception(e)
def _geo_json(dbm, entity_type, entity_fields, filters, details): location_list = [] try: forward_filters, reverse_filters = _transform_filters(filters, entity_fields) first_geocode_field = get_first_geocode_field_for_entity_type(entity_fields) if first_geocode_field: unique_ids = get_all_entities( dbm, [entity_type], 1000, forward_filters, reverse_filters ) details.extend(['q2']) fields_to_show = filter(lambda field: field['code'] in details, entity_fields) location_list.extend(_get_detail_list_for_entities( _get_field_labels(fields_to_show), first_geocode_field, unique_ids )) except DataObjectNotFound: pass return {"type": "FeatureCollection", "features": location_list}
def options(self): return [(entity.short_code, escape(entity.data['name']['value'])) for entity in get_all_entities(self.dbm, [self.unique_id_type]) ]
def _get_entity_options(dbm, entity_type): return [(entity.short_code, escape(entity.data['name']['value'])) for entity in get_all_entities(dbm, [entity_type])]
def _populate_index(dbm): for entity in get_all_entities(dbm, entity_type=REPORTER_ENTITY_TYPE): update_datasender_index(entity, dbm)