def get(self, request, *args, **kwargs): domain = self.kwargs['domain'] districts = FixtureDataItem.get_item_list(domain, 'district') cbos = FixtureDataItem.get_item_list(domain, 'cbo') clienttypes = FixtureDataItem.get_item_list(domain, 'clienttype') userpls = FixtureDataItem.get_item_list(domain, 'userpl') def to_filter_format(data, parent_key=None): locations = [dict(id='', text='All')] for row in data: loc_id = row.fields['id'].field_list[0].field_value loc = dict(id=loc_id, text=loc_id) if parent_key: parent_id = row.fields[parent_key].field_list[ 0].field_value loc.update({'parent_id': parent_id}) locations.append(loc) return locations hierarchy = { 'districts': to_filter_format(districts), 'cbos': to_filter_format(cbos, 'district_id'), 'clienttypes': to_filter_format(clienttypes, 'cbo_id'), 'userpls': to_filter_format(userpls, 'clienttype_id') } return JsonResponse(data=hierarchy)
def get(self, request, *args, **kwargs): domain = self.kwargs['domain'] districts = FixtureDataItem.get_item_list(domain, 'district') cbos = FixtureDataItem.get_item_list(domain, 'cbo') clienttypes = FixtureDataItem.get_item_list(domain, 'clienttype') userpls = FixtureDataItem.get_item_list(domain, 'userpl') def to_filter_format(data, parent_key=None): locations = [dict( id='', text='All' )] for row in data: loc_id = row.fields['id'].field_list[0].field_value loc = dict( id=loc_id, text=loc_id ) if parent_key: parent_id = row.fields[parent_key].field_list[0].field_value loc.update({'parent_id': parent_id}) locations.append(loc) return locations hierarchy = { 'districts': to_filter_format(districts), 'cbos': to_filter_format(cbos, 'district_id'), 'clienttypes': to_filter_format(clienttypes, 'cbo_id'), 'userpls': to_filter_format(userpls, 'clienttype_id') } return JsonResponse(data=hierarchy)
def get_fixture_items(self, attribute): # return list of 'attribute' values of fixture table 'things' fixtures = [] for fixture in FixtureDataItem.get_item_list(self.domain, 'things'): fixtures.append( fixture.fields.get(attribute).field_list[0].field_value) return fixtures
def get_doses_data(self): # return 'doses_per_week' by 'schedule_id' from the Fixture data fixtures = FixtureDataItem.get_item_list(self.domain, DAILY_SCHEDULE_FIXTURE_NAME) doses_per_week_by_schedule_id = {} for f in fixtures: schedule_id = f.fields[SCHEDULE_ID_FIXTURE].field_list[0].field_value doses_per_week = int(f.fields["doses_per_week"].field_list[0].field_value) doses_per_week_by_schedule_id[schedule_id] = doses_per_week return doses_per_week_by_schedule_id
def handle(self, *args, **options): self.stdout.write("Pulling stuff\n") beneficiaries = CommCareCase.get_all_cases('opm', include_docs=True) users = CommCareUser.by_domain('opm') fixtures = FixtureDataItem.get_item_list('opm', 'condition_amounts') # you won't be able to get the fixtures without the FixtureDataType fixtures.append(FixtureDataType.by_domain_tag('opm', 'condition_amounts').one()) forms = [] for b in beneficiaries: forms += b.get_forms() test_data = [] month, year = test_month_year for report_class in [IncentivePaymentReport, BeneficiaryPaymentReport]: self.stdout.write("Running %s\n" % report_class.__name__) report = get_report(report_class, month, year) snapshot = OpmReportSnapshot( domain=DOMAIN, month=month, year=year, report_class=report.report_class.__name__, headers=report.headers, slugs=report.slugs, rows=report.rows, ) test_data.append(snapshot.to_json()) self.stdout.write("Saving raw data\n") test_data += [form.to_json() for form in forms] test_data += [u.to_json() for u in users] test_data += [b.to_json() for b in beneficiaries] test_data += [f.to_json() for f in fixtures] doc_ids = set() docs = [] for doc in test_data: if doc.get('_id') not in doc_ids: for attrib in ['_rev', '_attachments']: try: del doc[attrib] except KeyError: pass doc_ids.add(doc.get('_id', 'null')) docs.append(doc) else: self.stdout.write("Ignoring duplicates\n") with open(test_data_location, 'w') as f: f.write(json.dumps(docs, indent=2)) self.stdout.write("Pulled stuff, let's hope it worked\n")
def test_row_addition(self): # upload and then reupload with addition of a new fixture-item should create new items initial_rows = [(None, 'N', 'apple')] rows = self.make_rows(initial_rows) workbook = self._get_workbook_from_data(self.headers, rows) _run_fixture_upload(self.domain, workbook) self.assertListEqual(self.get_fixture_items('name'), ['apple']) # reupload with additional row apple_id = FixtureDataItem.get_item_list(self.domain, 'things')[0]._id new_rows = [(apple_id, 'N', 'apple'), (None, 'N', 'orange')] workbook = self._get_workbook_from_data(self.headers, self.make_rows(new_rows)) _run_fixture_upload(self.domain, workbook) self.assertItemsEqual(self.get_fixture_items('name'), ['apple', 'orange'])
def test_row_addition(self): # upload and then reupload with addition of a new fixture-item should create new items initial_rows = [(None, 'N', 'apple')] rows = self.make_rows(initial_rows) workbook = self._get_workbook_from_data(self.headers, rows) _run_fixture_upload(self.domain, workbook) self.assertListEqual( self.get_fixture_items('name'), ['apple'] ) # reupload with additional row apple_id = FixtureDataItem.get_item_list(self.domain, 'things')[0]._id new_rows = [(apple_id, 'N', 'apple'), (None, 'N', 'orange')] workbook = self._get_workbook_from_data(self.headers, self.make_rows(new_rows)) _run_fixture_upload(self.domain, workbook) self.assertItemsEqual( self.get_fixture_items('name'), ['apple', 'orange'] )
def get_itemlist(domain): return FixtureDataItem.get_item_list(domain, DAILY_SCHEDULE_FIXTURE_NAME)
def all(self): for item in FixtureDataItem.get_item_list(self.domain, self.tag): fields = {k: to_field_value(v) for k, v in item.fields.iteritems()} yield self.model_class(_fixture_id=item.get_id, **fields)
def get_fixture_items(self, attribute): # return list of 'attribute' values of fixture table 'things' fixtures = [] for fixture in FixtureDataItem.get_item_list(self.domain, 'things'): fixtures.append(fixture.fields.get(attribute).field_list[0].field_value) return fixtures