Repository: akvo/akvo-rsr

Issue:
Location id '6178' missing iso country code - 'gh'
**Context**

When trying to add the F4W project updates to their Akvo Site via the RSR API, the update https://f4winternational.akvoapp.org/en/project/6178/update/21247/ would not appear whereas project update https://f4winternational.akvoapp.org/en/project/6177/update/21236/ would.


Example API call used:
https://rsr.akvo.org/rest/v1/project_update/?filter={%27project__partners__in%27:[f4waterids]}&project__primary_location__country__iso_code=gh&format=json&image_thumb_name=big

Did not provide data for project :
https://f4winternational.akvoapp.org/en/project/6178/update/21247/

**Problem or idea**

It seemed that location '6178' didn't have a country specified. Even though lat/long exists. It should be iso country code 'gh' for Ghana.

**Solution or next step**

Apparently a similar issue was fixed recently but this one was missed.

@punchagan fixed this on live RSR to allow for the Akvo Site to show the F4W project updates correctly. This still needs to be fixed permanently.


Discussion:

Resolving patch:
diff --git a/akvo/rsr/management/commands/fix_locations.py b/akvo/rsr/management/commands/fix_locations.py
new file mode 100644
--- /dev/null
+++ b/akvo/rsr/management/commands/fix_locations.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+
+# Akvo Reporting is covered by the GNU Affero General Public License.
+# See more details in the license.txt file located at the root folder of the Akvo RSR module.
+# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
+
+from django.core.management.base import BaseCommand
+
+from akvo.rsr.models import Country, OrganisationLocation, ProjectLocation, ProjectUpdateLocation
+from akvo.utils import get_country
+
+
+def add_country_to_locations(locations):
+    """Populate the country field in the given queryset of locations."""
+
+    print 'Setting country for {} {}s'.format(locations.count(), locations.model.__name__)
+    for location in locations.only('latitude', 'longitude'):
+        country, iso_code = get_country(float(location.latitude), float(location.longitude))
+        if iso_code is not None:
+            location.country = Country.objects.filter(iso_code__iexact=iso_code).first()
+            if location.country is None:
+                print 'Could not set country {} - {}'.format(country, iso_code)
+
+            else:
+                location.save(update_fields=['country'])
+
+        else:
+            print 'Could not compute country for {l.latitude},{l.longitude}'.format(l=location)
+
+
+def get_locations_for_model(model):
+    return model.objects.filter(country=None)\
+                        .exclude(latitude=None)\
+                        .exclude(longitude=None)\
+                        .exclude(latitude=0, longitude=0)
+
+
+class Command(BaseCommand):
+    help = "Add countries to locations with missing countries"
+
+    def handle(self, *args, **options):
+        """Add missing country information to locations.
+
+        Locations automatically get assigned countries, based on latitude and
+        longitude. But, due to various bugs, some locations do not have
+        countries. This is a script to fix such locations.
+
+        """
+
+        locations = get_locations_for_model(ProjectLocation)
+        add_country_to_locations(locations)
+
+        locations = get_locations_for_model(ProjectUpdateLocation)
+        add_country_to_locations(locations)
+
+        locations = get_locations_for_model(OrganisationLocation)
+        add_country_to_locations(locations)
diff --git a/akvo/rsr/models/partnership.py b/akvo/rsr/models/partnership.py
--- a/akvo/rsr/models/partnership.py
+++ b/akvo/rsr/models/partnership.py
@@ -261,7 +261,7 @@ def allow_project_access_if_restrictions_disabled(sender, **kwargs):
             return
         org = reporting_org
 
-    if org.enable_restrictions:
+    if org is None or org.enable_restrictions:
         return
 
     from akvo.rsr.models.user_projects import unrestrict_projects


Test patch: