From c2a8da7696670d8709654ef82be39462df80e1d6 Mon Sep 17 00:00:00 2001 From: Ben Webb Date: Wed, 25 Feb 2015 15:15:58 +0000 Subject: [PATCH] [IATI/IATI-Dashboard#297] Include 1.03 style elements in location calc (on the comprehensiveness page) --- stats/dashboard.py | 24 +++++++++++++-------- stats/tests/test_comprehensiveness.py | 30 +++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/stats/dashboard.py b/stats/dashboard.py index b4450972d02..a5a17dde2c4 100644 --- a/stats/dashboard.py +++ b/stats/dashboard.py @@ -157,13 +157,19 @@ def valid_value(value_element): return schema.validate(value_element) -def valid_coords(x): - coords = x.split(' ') - if len(coords) != 2: - return False +def valid_coords(el): try: - x = decimal.Decimal(coords[0]) - y = decimal.Decimal(coords[1]) + if el.tag == 'coordinates': + y = decimal.Decimal(el.attrib.get('latitude')) + x = decimal.Decimal(el.attrib.get('longitude')) + else: + if not el.text: + return False + coords = el.text.split(' ') + if len(coords) != 2: + return False + y = decimal.Decimal(coords[0]) + x = decimal.Decimal(coords[1]) if x == 0 and y ==0: return False else: @@ -561,8 +567,8 @@ def nonempty_text_element(tagname): 'transaction_traceability': all_and_not_empty(x.xpath('provider-org/@provider-activity-id') for x in self.element.xpath('transaction[transaction-type/@code="{}"]'.format(self._incoming_funds_code()))), 'budget': self.element.findall('budget'), 'contact-info': self.element.findall('contact-info/email'), - 'location': self.element.xpath('location/point/pos|location/name|location/description|location/location-administrative'), - 'location_point_pos': self.element.xpath('location/point/pos'), + 'location': self.element.xpath('location/point/pos|location/coordinates|location/name|location/description|location/location-administrative'), + 'location_point_pos': self.element.xpath('location/point/pos|location/coordinates'), 'sector_dac': self.element.xpath('sector[@vocabulary="{}" or @vocabulary="{}" or not(@vocabulary)]'.format(self._dac_5_code(), self._dac_3_code())), 'capital-spend': self.element.xpath('capital-spend/@percentage'), 'document-link': self.element.findall('document-link'), @@ -637,7 +643,7 @@ def empty_or_percentage_sum_is_100(path, by_vocab=False): valid_value(budget.find('value')) for budget in bools['budget'])), 'location_point_pos': all_and_not_empty( - valid_coords(x.text) for x in bools['location_point_pos']), + valid_coords(x) for x in bools['location_point_pos']), 'sector_dac': ( bools['sector_dac'] and all(x.attrib.get('code') in CODELISTS[self._major_version()]['Sector'] for x in self.element.xpath('sector[@vocabulary="{}" or not(@vocabulary)]'.format(self._dac_5_code()))) and diff --git a/stats/tests/test_comprehensiveness.py b/stats/tests/test_comprehensiveness.py index 0f71c1f05c6..2e919e0f90b 100644 --- a/stats/tests/test_comprehensiveness.py +++ b/stats/tests/test_comprehensiveness.py @@ -334,6 +334,9 @@ def test_comprehensiveness_other_passes(major_version): + + + ''' if major_version == '1' else ''' @@ -367,8 +370,8 @@ def test_comprehensiveness_other_passes(major_version): 'transaction_traceability': 0, 'budget': 0, 'contact-info': 0, - 'location': 0, - 'location_point_pos': 0, + 'location': 1 if major_version == '1' else 0, + 'location_point_pos': 1 if major_version == '1' else 0, 'sector_dac': 0, 'capital-spend': 0, 'document-link': 0, @@ -806,6 +809,29 @@ def test_valid_location(major_version): ''') assert activity_stats.comprehensiveness_with_validation()['location_point_pos'] == 0 + if major_version == '1': + activity_stats = MockActivityStats(major_version) + activity_stats.today = datetime.date(9990, 6, 1) + activity_stats.element = etree.fromstring(''' + + + + + + ''') + assert activity_stats.comprehensiveness_with_validation()['location_point_pos'] == 0 + + activity_stats = MockActivityStats(major_version) + activity_stats.today = datetime.date(9990, 6, 1) + activity_stats.element = etree.fromstring(''' + + + + + + ''') + assert activity_stats.comprehensiveness_with_validation()['location_point_pos'] == 1 + @pytest.mark.parametrize('major_version', ['1', '2']) def test_comprehensiveness_transaction_level_elements(major_version):