def _get_custom_context(self): api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain, 'resource_name': 'location', 'api_name': 'v0.3'}) selected_loc_id = self.request.GET.get('location_id') user = WebUser.get_by_username(str(self.request.user)) domain = Domain.get_by_name(self.domain) context = {} from corehq.apps.commtrack.util import get_commtrack_location_id location_id = get_commtrack_location_id(user, domain) if location_id: selected_loc_id = location_id if domain.location_restriction_for_users: context.update({'restriction': domain.location_restriction_for_users}) context.update({ 'api_root': api_root, 'control_name': self.name, 'control_slug': self.slug, 'loc_id': selected_loc_id, 'locations': json.dumps(load_locs_json(self.domain, selected_loc_id)), 'hierarchy': location_hierarchy_config(self.domain), }) return context
def __call__(self, user, version, last_sync=None): if user.domain in M4CHANGE_DOMAINS: domain = Domain.get_by_name(user.domain) location_id = get_commtrack_location_id(user, domain) if location_id is not None: fixture = self.get_fixture(user, location_id) if fixture is None: return [] return [fixture] else: return [] else: return []
def generator(user, version, last_sync=None): if user.domain in M4CHANGE_DOMAINS: domain = Domain.get_by_name(user.domain) location_id = get_commtrack_location_id(user, domain) if location_id is not None: fixture = LocationFixtureProvider('user-locations', user, domain, location_id).to_fixture() if fixture is None: return [] return [fixture] else: return [] else: return []
def generator(user, version, synclog, last_sync): if user.domain in M4CHANGE_DOMAINS: domain = Domain.get_by_name(user.domain) location_id = get_commtrack_location_id(user, domain) if location_id is not None: fixture = ReportFixtureProvider('reports:m4change-mobile', user, domain, location_id).to_fixture() if fixture is None: return [] return [fixture] else: return [] else: return []
def __call__(self, user, version, last_sync=None): if user.domain in M4CHANGE_DOMAINS: domain = Domain.get_by_name(user.domain) location_id = get_commtrack_location_id(user, domain) if location_id is not None: fixture = self.get_fixture(user, domain, location_id) if fixture is None: return [] return [fixture] else: return [] else: return []
def generator(user, version, last_sync): if user.domain in M4CHANGE_DOMAINS: domain = Domain.get_by_name(user.domain) location_id = get_commtrack_location_id(user, domain) if location_id is not None: fixture = ReportFixtureProvider('reports:m4change-mobile', user, domain, location_id).to_fixture() if fixture is None: return [] return [fixture] else: return [] else: return []
def get_commtrack_location_id(self): from corehq.apps.commtrack.util import get_commtrack_location_id return get_commtrack_location_id(self._couch_user, self.project)
def to_fixture(self): """ Generate a fixture representation of the indicator set. Something like the following: <fixture id="indicators:m4change-mobile" user_id="4ce8b1611c38e953d3b3b84dd3a7ac18"> <monthly-report startdate="2013-02-01" enddate="2013-03-01"> <facility id="4ce8b1611c38e953d3b3b84dd3a7ac19" name="Facility 1"> <report id="facility_anc_hmis_report" name="Facility ANC HMIS Report"> <columns> <column name="HMIS Code" /> <column name="Data Point" /> <column name="Total" /> </columns> <rows> <row> <field value="03" /> <field value="Antenatal Attendance - Total" /> <field value="0" /> </row> <!-- ... --> </rows> </report> <!-- ... --> </facility> <!-- ... --> </monthly-report> <!-- ... --> </fixture> """ def _reports_to_fixture(report_data, facility_id, facility_element): for dictionary in sorted(report_data): columns_added = False for report_key in sorted(dictionary): data = dictionary.get(report_key, {}) name = data.get('name', None) report_element = ElementTree.Element('report', attrib={ 'id': report_key, 'name': name }) columns_element = ElementTree.Element('columns') rows_element = ElementTree.Element('rows') report_rows = data.get('data', {}) for row_key in sorted(report_rows): row_data = report_rows.get(row_key, {}) if not columns_added: for column_key in sorted(row_data): column = ElementTree.Element('column', attrib={ 'name': column_key }) columns_element.append(column) columns_added = True row_element = ElementTree.Element('row') for field_key in sorted(row_data): field_element = ElementTree.Element('field', attrib={ 'value': str(row_data.get(field_key, '')) }) row_element.append(field_element) rows_element.append(row_element) report_element.append(columns_element) report_element.append(rows_element) return report_element def _facility_to_fixture(facility, startdate, enddate): facility_id = facility.get_id facility_element = ElementTree.Element('facility', attrib={ 'id': facility_id, 'name': _(facility.name) }) report_data = M4ChangeReportDataSource(config={ 'startdate': startdate, 'enddate': enddate, 'location_id': facility_id, 'domain': self.domain.name }).get_data() facility_element.append(_reports_to_fixture(report_data, facility_id, facility_element)) return facility_element def _month_to_fixture(date_tuple, locations): monthly_report_element = ElementTree.Element('monthly-report', attrib={ 'startdate': date_tuple[0].strftime('%Y-%m-%d'), 'enddate': date_tuple[1].strftime('%Y-%m-%d') }) for location in locations: monthly_report_element.append(_facility_to_fixture(location, date_tuple[0], date_tuple[1])) return monthly_report_element root = ElementTree.Element('fixture', attrib={ 'user_id': self.user._id }) user_location = Location.get(get_commtrack_location_id(self.user, self.domain)) locations = [user_location] + [descendant for descendant in user_location.descendants] for date_tuple in self.dates: root.append(_month_to_fixture(date_tuple, locations)) return root
def _get_location_by_user_id(user_id, domain): user = CommCareUser.get(user_id) if user is not None: location_id = get_commtrack_location_id(user, Domain.get_by_name(domain)) return str(location_id) if location_id is not None else NO_LOCATION_VALUE_STRING return NO_LOCATION_VALUE_STRING