Пример #1
0
def add_country_to_locations(apps, schema_editor):
    """Populate the country field in the subclasses of BaseLocation."""

    model_names = ('OrganisationLocation', 'ProjectLocation',
                   'ProjectUpdateLocation')
    location_models = [apps.get_model('rsr', name) for name in model_names]
    Country = apps.get_model('rsr', 'Country')

    for model in location_models:
        locations = model.objects.filter(country=None)
        print 'Setting country for {} {}s'.format(locations.count(),
                                                  model.__name__)
        for location in locations:
            # Country is automatically set in the save method
            if location.latitude is None or location.longitude is None:
                continue

            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()
Пример #2
0
    def get_country_from_lat_lon(self):
        """Get the country based on the location's latitude and longitude."""

        if self.latitude is None or self.longitude is None:
            return None

        try:
            country, iso_code = get_country(float(self.latitude), float(self.longitude))
        except ValueError:
            iso_code = None

        if iso_code is not None:
            # FIXME: We have one too many country models!
            Country = models.get_model('rsr', 'Country')
            return Country.objects.filter(iso_code=iso_code).first()
Пример #3
0
    def get_country_from_lat_lon(self):
        """Get the country based on the location's latitude and longitude."""

        if self.latitude is None or self.longitude is None:
            return None

        try:
            country, iso_code = get_country(float(self.latitude),
                                            float(self.longitude))
        except ValueError:
            iso_code = None

        if iso_code is not None:
            # FIXME: We have one too many country models!
            Country = models.get_model('rsr', 'Country')
            return Country.objects.filter(iso_code=iso_code).first()
Пример #4
0
def get_country_from_lat_lon(Country, location):
    """Get the country based on the location's latitude and longitude.

    NOTE: This code is duplicated from the BaseLocation model, since the method
    is not available in a migration.

    """

    try:
        country, iso_code = get_country(float(location.latitude), float(location.longitude))

    except ValueError:
        iso_code = None

    if iso_code is not None:
        return Country.objects.filter(iso_code=iso_code).first()
Пример #5
0
def get_country_from_lat_lon(Country, location):
    """Get the country based on the location's latitude and longitude.

    NOTE: This code is duplicated from the BaseLocation model, since the method
    is not available in a migration.

    """

    try:
        country, iso_code = get_country(float(location.latitude),
                                        float(location.longitude))

    except ValueError:
        iso_code = None

    if iso_code is not None:
        return Country.objects.filter(iso_code=iso_code).first()
Пример #6
0
    def test_countries(self):
        """Test retrieving country name from (lat, long)."""

        LOCATIONS = [
            ((0.313611, 32.581111), 'Uganda'),
            ((15.184898, 75.060385), 'India'),
            ((10.54, 14.36), 'Cameroon'),
            ((6.167031, 8.660059), 'Nigeria'),
            ((7.116944, -9.400053), 'Liberia'),
            ((-1.292066, 36.821946), 'Kenya'),
            ((27.305846, 85.404534), 'Nepal'),
            ((10.314919, 1.680908), 'Benin'),
            ((-1.292066, 36.821946), 'Kenya'),
            ((7.346927, 2.06652), 'Benin'),
        ]

        for (lat, lon), country in LOCATIONS:
            self.assertEqual(country, get_country(lat, lon)[0])
Пример #7
0
def add_country_to_locations(apps, schema_editor):
    """Populate the country field in the subclasses of BaseLocation."""

    model_names = ('OrganisationLocation', 'ProjectLocation', 'ProjectUpdateLocation')
    location_models = [apps.get_model('rsr', name) for name in model_names]
    Country = apps.get_model('rsr', 'Country')

    for model in location_models:
        locations = model.objects.filter(country=None)
        print 'Setting country for {} {}s'.format(locations.count(), model.__name__)
        for location in locations:
            # Country is automatically set in the save method
            if location.latitude is None or location.longitude is None:
                continue

            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()