def get_export(export_type, domain, export_id=None, username=None): from corehq.apps.export.models import ( FormExportInstance, CaseExportInstance, SMSExportInstance, SMSExportDataSchema ) if export_type == 'form': try: return FormExportInstance.get(export_id) except ResourceNotFound: raise Http404() elif export_type == 'case': try: return CaseExportInstance.get(export_id) except ResourceNotFound: raise Http404() elif export_type == 'sms': if not username: raise Exception("Username needed to ensure permissions") include_metadata = MESSAGE_LOG_METADATA.enabled(username) return SMSExportInstance._new_from_schema( SMSExportDataSchema.get_latest_export_schema(domain, include_metadata) ) raise Exception("Unexpected export type received %s" % export_type)
def test_ignore_case_link_label(self): export_with_case_link = CaseExportInstance( is_odata_config=True, tables=[ TableConfiguration( selected=True, columns=[ ExportColumn( label='my_case_link', item=ExportItem( path=[ PathNode(name='_id') ], transform=CASE_ID_TO_LINK, ), selected=True, ) ] ) ] ) export_with_case_link.save() self.addCleanup(export_with_case_link.delete) cleaned_export = CaseExportInstance.get(export_with_case_link.get_id) self.assertEqual(cleaned_export.tables[0].columns[0].label, 'my_case_link')
def test_edit_daily_saved_export_filters(self, _): # Create an export # Update the filters # confirm that the filters on the export have been updated appropriately export_post_data = json.dumps({ "doc_type": "CaseExportInstance", "domain": self.domain.name, "xmlns": "http://openrosa.org/formdesigner/237B85C0-78B1-4034-8277-5D37E3EA7FD1", "last_updated": None, "legacy_saved_export_schema_id": None, "is_daily_saved_export": True, "tables": [], "transform_dates": True, "last_accessed": None, "app_id": "6a48b8838d06febeeabb28c8c9516ab6", "is_deidentified": False, "split_multiselects": False, "external_blobs": {}, "export_format": "csv", "include_errors": False, "type": "form", "name": "A Villager's Health > Registrationaa > Reg form: 2016-06-27" }) resp = self.client.post( reverse(CreateNewDailySavedCaseExport.urlname, args=[self.domain.name]), export_post_data, content_type="application/json", follow=True ) self.assertEqual(resp.status_code, 200) exports = get_case_export_instances(self.domain.name) self.assertEqual(len(exports), 1) export = exports[0] filter_form_data = { "emwf_case_filter": [], "type_or_group": "group", "date_range": "range", "start_date": "1992-01-30", "end_date": "2016-10-01", } resp = self.client.post( reverse(DailySavedExportListView.urlname, args=[self.domain.name]), json.dumps({ "export": {"id": export._id}, "form_data": filter_form_data }), content_type="application/json", HTTP_X_REQUESTED_WITH="XMLHttpRequest", HTTP_DJNG_REMOTE_METHOD='commit_filters', ) self.assertEqual(resp.status_code, 200) response_content = json.loads(resp.content) self.assertFalse("error" in response_content, response_content.get("error")) export = CaseExportInstance.get(export._id) self.assertEqual(export.filters.date_period.period_type, 'range')
def test_edit_daily_saved_export_filters(self, _): # Create an export # Update the filters # confirm that the filters on the export have been updated appropriately export_post_data = json.dumps({ "doc_type": "CaseExportInstance", "domain": self.domain.name, "xmlns": "http://openrosa.org/formdesigner/237B85C0-78B1-4034-8277-5D37E3EA7FD1", "last_updated": None, "legacy_saved_export_schema_id": None, "is_daily_saved_export": True, "tables": [], "transform_dates": True, "last_accessed": None, "app_id": "6a48b8838d06febeeabb28c8c9516ab6", "is_deidentified": False, "split_multiselects": False, "external_blobs": {}, "export_format": "csv", "include_errors": False, "type": "form", "name": "A Villager's Health > Registrationaa > Reg form: 2016-06-27" }) resp = self.client.post( reverse(CreateNewDailySavedCaseExport.urlname, args=[self.domain.name]), export_post_data, content_type="application/json", follow=True ) self.assertEqual(resp.status_code, 200) exports = get_case_export_instances(self.domain.name) self.assertEqual(len(exports), 1) export = exports[0] filter_form_data = { "emwf_case_filter": [], "date_range": "range", "start_date": "1992-01-30", "end_date": "2016-10-01", } resp = self.client.post( reverse('commit_filters', args=[self.domain.name]), { "export_id": export._id, "model_type": "case", "form_data": json.dumps(filter_form_data), }, ) self.assertEqual(resp.status_code, 200) response_content = json.loads(resp.content) self.assertFalse("error" in response_content, response_content.get("error")) export = CaseExportInstance.get(export._id) self.assertEqual(export.filters.date_period.period_type, 'range')
def test_caseid_column_label(self): export_with_modified_caseid_column = CaseExportInstance( is_odata_config=True, tables=[ TableConfiguration(columns=[ ExportColumn( label='modified_case_id_column', item=ExportItem(path=[PathNode(name='_id')]), selected=True, ) ]) ]) export_with_modified_caseid_column.save() self.addCleanup(export_with_modified_caseid_column.delete) cleaned_export = CaseExportInstance.get( export_with_modified_caseid_column.get_id) self.assertEqual(cleaned_export.tables[0].columns[0].label, 'caseid')
def get_export(export_type, domain, export_id=None, username=None): from corehq.apps.export.models import (FormExportInstance, CaseExportInstance, SMSExportInstance, SMSExportDataSchema) if export_type == 'form': try: return FormExportInstance.get(export_id) except ResourceNotFound: raise Http404() elif export_type == 'case': try: return CaseExportInstance.get(export_id) except ResourceNotFound: raise Http404() elif export_type == 'sms': if not username: raise Exception("Username needed to ensure permissions") include_metadata = MESSAGE_LOG_METADATA.enabled(username) return SMSExportInstance._new_from_schema( SMSExportDataSchema.get_latest_export_schema( domain, include_metadata)) raise Exception("Unexpected export type received %s" % export_type)
def to_json(self, data, options=None): # Convert bundled objects to JSON data['objects'] = [ bundle.obj for bundle in data['objects'] ] domain = data.pop('domain', None) config_id = data.pop('config_id', None) api_path = data.pop('api_path', None) assert all([domain, config_id, api_path]), [domain, config_id, api_path] data['@odata.context'] = '{}#{}'.format( absolute_reverse(ODataCaseMetadataView.urlname, args=[domain, config_id]), 'feed' ) next_link = self.get_next_url(data.pop('meta'), api_path) if next_link: data['@odata.nextLink'] = next_link config = CaseExportInstance.get(config_id) data['value'] = self.serialize_cases_using_config(data.pop('objects'), config) return json.dumps(data, cls=DjangoJSONEncoder, sort_keys=True)
def get_config(self, config_id): return CaseExportInstance.get(config_id)
def get_export(self, id=None): return CaseExportInstance.get(id)