def add_target_element(is_dgis_project, period, period_element): if (is_dgis_project or has_data(period, ['target_value', 'target_comment', ]) or has_qs_data(period, ['target_locations', 'target_dimensions', ])): target_element = etree.SubElement(period_element, "target") if period.target_value: target_element.attrib['value'] = period.target_value elif is_dgis_project: target_element.attrib['value'] = NOT_AVAILABLE for target_location in period.target_locations.all(): target_location_element = etree.SubElement(target_element, "location") target_location_element.attrib['ref'] = target_location.location for target_dimension in period.target_dimensions.all(): if target_dimension.name or target_dimension.value: target_dimension_element = etree.SubElement(target_element, "dimension") if target_dimension.name: target_dimension_element.attrib['name'] = target_dimension.name if target_dimension.value: target_dimension_element.attrib['value'] = target_dimension.value if period.target_comment: comment_element = etree.SubElement(target_element, "comment") narrative_element = etree.SubElement(comment_element, "narrative") narrative_element.text = period.target_comment
def test_queryset_has_data(self): # Given project = Project.objects.create() Result.objects.create(project=project) # When/Then self.assertTrue(has_qs_data(project, ['results']))
def add_actual_element(is_dgis_project, period, period_element): if (is_dgis_project or has_data(period, [ 'actual_value', 'narrative', 'actual_comment', ]) or has_qs_data(period, ['actual_locations'])): actual_element = etree.SubElement(period_element, "actual") if period.indicator.type == QUANTITATIVE: if period.actual_value: actual_element.attrib['value'] = period.actual_value elif is_dgis_project: actual_element.attrib['value'] = NOT_AVAILABLE else: if period.narrative: actual_element.attrib['value'] = period.narrative for actual_location in period.actual_locations.all(): actual_location_element = etree.SubElement(actual_element, "location") actual_location_element.attrib['ref'] = actual_location.location if period.actual_comment: comment_element = etree.SubElement(actual_element, "comment") narrative_element = etree.SubElement(comment_element, "narrative") narrative_element.text = period.actual_comment
def result(project): """ Generate the result elements. :param project: Project object :return: A list of Etree elements """ result_elements = [] DGIS_PROJECT = project.validations.filter(name=DGIS_VALIDATION_SET_NAME).count() == 1 for res in project.results.all(): if (has_data(res, ['type', 'title', 'description', ]) or res.aggregation_status is not None or res.indicators.all()): element = etree.Element("result") if res.type: element.attrib['type'] = res.type if res.aggregation_status is not None: element.attrib['aggregation-status'] = '1' if res.aggregation_status else '0' if res.title: title_element = etree.SubElement(element, "title") narrative_element = etree.SubElement(title_element, "narrative") narrative_element.text = res.title if res.description: description_element = etree.SubElement(element, "description") narrative_element = etree.SubElement(description_element, "narrative") narrative_element.text = res.description for indicator in res.indicators.all(): if (has_data(indicator, ['measure', 'title', 'description', 'baseline_year', 'baseline_value', 'baseline_comment', ]) or indicator.ascending is not None or has_qs_data(indicator, ['references', 'periods', ])): add_indicator_element(element, indicator, DGIS_PROJECT) result_elements.append(element) return result_elements
def add_period_element(is_dgis_project, indicator_element, period): if (has_data(period, ['period_start', 'period_end', 'target_value', 'target_comment', 'actual_value', 'narrative', 'actual_comment']) or has_qs_data(period, ['target_locations', 'target_dimensions', 'actual_locations', 'actual_dimensions', ])): period_element = etree.SubElement(indicator_element, "period") if period.period_start: period_start_element = etree.SubElement(period_element, "period-start") period_start_element.attrib['iso-date'] = str(period.period_start) if period.period_end: period_end_element = etree.SubElement(period_element, "period-end") period_end_element.attrib['iso-date'] = str(period.period_end) add_target_element(is_dgis_project, period, period_element) add_actual_element(is_dgis_project, period, period_element)
def add_actual_dimension_elements(is_dgis_project, period, period_element): if (is_dgis_project or has_data(period, [ 'actual_value', 'narrative', 'actual_comment', ]) or has_qs_data(period, ['actual_locations'])): qs = period.disaggregations.select_related('dimension_value', 'dimension_value__name') for period_disaggregation in qs.all(): actual_element = etree.SubElement(period_element, "actual") if period_disaggregation.value: actual_element.attrib['value'] = str( period_disaggregation.value) elif is_dgis_project: actual_element.attrib['value'] = NOT_AVAILABLE dimension_element = etree.SubElement(actual_element, 'dimension') dimension_element.attrib[ 'value'] = period_disaggregation.dimension_value.value dimension_element.attrib[ 'name'] = period_disaggregation.dimension_value.name.name
def test_has_queryset_attribute(self): # Given project = Project.objects.create() # When/Then self.assertFalse(has_qs_data(project, ['results']))