From 5e34f22b7814d95c27056be6ab85ab7739652e38 Mon Sep 17 00:00:00 2001 From: Lev Lybin Date: Tue, 25 Sep 2018 01:28:04 +0700 Subject: [PATCH 1/2] command cities_light fix warnings --- .../management/commands/cities_light.py | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/cities_light/management/commands/cities_light.py b/cities_light/management/commands/cities_light.py index 86763112..bd550180 100644 --- a/cities_light/management/commands/cities_light.py +++ b/cities_light/management/commands/cities_light.py @@ -313,9 +313,10 @@ def region_import(self, items): except InvalidItems: return + force_insert = False + force_update = False + try: - force_insert = False - force_update = False region = Region.objects.get(geoname_id=items[IRegion.geonameid]) force_update = True except Region.DoesNotExist: @@ -370,17 +371,6 @@ def city_import(self, items): except InvalidItems: return - try: - force_insert = False - force_update = False - city = City.objects.get(geoname_id=items[ICity.geonameid]) - force_update = True - except City.DoesNotExist: - if self.noinsert: - return - city = City(geoname_id=items[ICity.geonameid]) - force_insert = True - try: country_id = self._get_country_id(items[ICity.countryCode]) except Country.DoesNotExist: @@ -397,6 +387,23 @@ def city_import(self, items): except Region.DoesNotExist: region_id = None + force_insert = False + force_update = False + + try: + city = City.objects.get(geoname_id=items[ICity.geonameid]) + force_update = True + except City.DoesNotExist: + try: + city = City.objects.get(name=items[ICity.name], + region_id=region_id) + force_update = True + except City.DoesNotExist: + if self.noinsert: + return + city = City(geoname_id=items[ICity.geonameid]) + force_insert = True + save = False if city.country_id != country_id: city.country_id = country_id From 5cc7505bf9b4e9bfe251f31c683daad0d13fc3dd Mon Sep 17 00:00:00 2001 From: Lev Lybin Date: Tue, 25 Sep 2018 01:35:45 +0700 Subject: [PATCH 2/2] command cities_light ignore duplicates cities --- cities_light/management/commands/cities_light.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cities_light/management/commands/cities_light.py b/cities_light/management/commands/cities_light.py index bd550180..52268418 100644 --- a/cities_light/management/commands/cities_light.py +++ b/cities_light/management/commands/cities_light.py @@ -395,9 +395,11 @@ def city_import(self, items): force_update = True except City.DoesNotExist: try: + # check on duplicate by unique key city = City.objects.get(name=items[ICity.name], region_id=region_id) - force_update = True + if city: + return except City.DoesNotExist: if self.noinsert: return