def generate_schema_from_builds(domain, case_type, force_rebuild=False): """Builds a schema from Application builds for a given identifier :param domain: The domain that the export belongs to :param unique_form_id: The unique identifier of the item being exported :returns: Returns a CaseExportDataSchema instance """ original_id, original_rev = None, None current_case_schema = get_latest_case_export_schema(domain, case_type) if current_case_schema and not force_rebuild: # Save the original id an rev so we can later save the document under the same _id original_id, original_rev = current_case_schema._id, current_case_schema._rev else: current_case_schema = CaseExportDataSchema() app_build_ids = CaseExportDataSchema._get_app_build_ids_to_process( domain, current_case_schema.last_app_versions, ) for app_doc in iter_docs(Application.get_db(), app_build_ids): app = Application.wrap(app_doc) case_property_mapping = get_case_properties( app, [case_type], include_parent_properties=False ) case_schema = CaseExportDataSchema._generate_schema_from_case_property_mapping( case_property_mapping, app.copy_of, app.version, ) case_history_schema = CaseExportDataSchema._generate_schema_for_case_history( case_property_mapping, app.copy_of, app.version, ) current_case_schema = CaseExportDataSchema._merge_schemas( current_case_schema, case_schema, case_history_schema ) current_case_schema.record_update(app.copy_of, app.version) if original_id and original_rev: current_case_schema._id = original_id current_case_schema._rev = original_rev current_case_schema.domain = domain current_case_schema.case_type = case_type current_case_schema.save() return current_case_schema
def get_case_type_to_properties(domain): case_type_to_properties = defaultdict(list) case_types = get_case_types_for_domain_es(domain) for case_type in case_types: if not case_type: # TODO - understand why a case can have a blank case type and handle appropriately continue case_export_schema = (get_latest_case_export_schema(domain, case_type) or CaseExportDataSchema.generate_schema_from_builds( domain, None, case_type)) for export_group_schema in case_export_schema.group_schemas[0].items: cleaned_case_property = export_group_schema.label.replace('_', '') case_type_to_properties[case_type].append(cleaned_case_property) return dict(case_type_to_properties)
def get_case_type_to_properties(domain): case_type_to_properties = defaultdict(list) case_types = get_case_types_for_domain_es(domain) for case_type in case_types: if not case_type: # TODO - understand why a case can have a blank case type and handle appropriately continue case_export_schema = ( get_latest_case_export_schema(domain, case_type) or CaseExportDataSchema.generate_schema_from_builds(domain, None, case_type) ) for export_group_schema in case_export_schema.group_schemas[0].items: cleaned_case_property = export_group_schema.label.replace('_', '') case_type_to_properties[case_type].append(cleaned_case_property) return dict(case_type_to_properties)
def test_get_latest_case_export_schema_empty(self): schema = get_latest_case_export_schema(self.domain, 'not-found') self.assertEqual(schema, None)
def test_get_latest_case_export_schema(self): schema = get_latest_case_export_schema(self.domain, self.case_type) self.assertEqual(schema._id, self.case_schema._id)