diff --git a/apps/location/transfer.py b/apps/location/transfer.py index 9ad1c15a..fe4e8084 100644 --- a/apps/location/transfer.py +++ b/apps/location/transfer.py @@ -101,18 +101,10 @@ card = { "dependencies": ("Country", "Region"), "fields": { "Cities": { - "coordinates": (("lat", "latitude"), ("long", "longitude")), + "name": "name", "code": "country_code_2", "postal_code": "zip_code", "is_island": ("is_island", "is_island", "django.db.models.Boolean") - }, - "relations": { - "CityNames": { - "key": "city", - "fields": { - "name": "name", - } - } } }, "relations": { diff --git a/apps/location/transfer_data.py b/apps/location/transfer_data.py index 3dc52c05..6d6861cc 100644 --- a/apps/location/transfer_data.py +++ b/apps/location/transfer_data.py @@ -6,13 +6,10 @@ from pprint import pprint def transfer_countries(): - # queryset = Cities.objects.exclude(Q(country_code_2__isnull=True) | Q(country_code_2="")) \ - # .only("id", "country_code_2").annotate(Count("country_code_2")).order_by("country_code_2") queryset = Cities.objects.raw("""SELECT cities.id, cities.country_code_2 - FROM cities - WHERE NOT ((cities.country_code_2 IS NULL - OR (cities.country_code_2 = '' - AND cities.country_code_2 IS NOT NULL))) + FROM cities WHERE + country_code_2 IS NOT NULL AND + country_code_2 != "" GROUP BY cities.country_code_2""") queryset = [vars(query) for query in queryset] @@ -27,14 +24,13 @@ def transfer_countries(): def transfer_regions(): regions_without_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code, cities.country_code_2, cities.subregion_code - FROM cities - WHERE NOT ((cities.subregion_code IS NOT NULL - OR cities.region_code IS NULL - OR cities.country_code_2 IS NULL - OR (cities.region_code = '' - AND cities.region_code IS NOT NULL) - OR (cities.country_code_2 = '' - AND cities.country_code_2 IS NOT NULL))) + FROM cities WHERE + (subregion_code IS NULL OR + subregion_code = "") AND + region_code IS NOT NULL AND + region_code != "" AND + country_code_2 IS NOT NULL AND + country_code_2 != "" GROUP BY region_code""") regions_without_subregion_queryset = [vars(query) for query in regions_without_subregion_queryset] @@ -45,18 +41,24 @@ def transfer_regions(): else: pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}") - regions_with_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code, - cities.country_code_2, cities.subregion_code - FROM cities - WHERE NOT ((cities.subregion_code IS NULL - OR (cities.subregion_code = '' - OR cities.region_code IS NULL - OR cities.country_code_2 IS NULL - OR (cities.region_code = '' - AND cities.region_code IS NOT NULL) - OR (cities.country_code_2 = '' - AND cities.country_code_2 IS NOT NULL)))) - AND cities.subregion_code in (SELECT region_code FROM cities) + regions_with_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code, + cities.country_code_2, cities.subregion_code + FROM cities WHERE + subregion_code IS NOT NULL AND + subregion_code != "" AND + region_code IS NOT NULL AND + region_code != "" AND + country_code_2 IS NOT NULL AND + country_code_2 != "" + AND cities.subregion_code in ( + SELECT region_code FROM cities WHERE + (subregion_code IS NULL OR + subregion_code = "") AND + region_code IS NOT NULL AND + region_code != "" AND + country_code_2 IS NOT NULL AND + country_code_2 != "" + ) GROUP BY region_code""") regions_with_subregion_queryset = [vars(query) for query in regions_with_subregion_queryset] @@ -72,12 +74,13 @@ def transfer_cities(): queryset = Cities.objects.exclude(Q(region_code__isnull=True) | Q(country_code_2__isnull=True) | Q(region_code="") | - Q(country_code_2="")).only("id", "name", "latitude", "longitude", "subregion_code", - "country_code_2", "zip_code", "is_island") + Q(country_code_2="")).only("id", "name", "region_code", + "country_code_2", "zip_code", "is_island").distinct() serialized_data = CitySerializer(data=list(queryset.values()), many=True) if serialized_data.is_valid(): - serialized_data.save() + # serialized_data.save() + print(serialized_data.validated_data) else: pprint(f"City serializer errors: {serialized_data.errors}") diff --git a/apps/transfer/serializers/location.py b/apps/transfer/serializers/location.py index 00c2e834..5a95e223 100644 --- a/apps/transfer/serializers/location.py +++ b/apps/transfer/serializers/location.py @@ -35,7 +35,7 @@ class CountrySerializer(serializers.ModelSerializer): class RegionSerializer(serializers.ModelSerializer): region_code = serializers.CharField() - subregion_code = serializers.CharField(allow_null=True) + subregion_code = serializers.CharField(allow_null=True, allow_blank=True) country_code_2 = serializers.CharField() id = serializers.IntegerField() @@ -52,7 +52,6 @@ class RegionSerializer(serializers.ModelSerializer): data = self.set_old_id(data) data = self.set_code(data) data = self.set_country(data) - return data def create(self, validated_data): @@ -66,7 +65,8 @@ class RegionSerializer(serializers.ModelSerializer): return region def set_code(self, data): - if "subregion_code" in data and data["subregion_code"] is not None: + print(data) + if "subregion_code" in data and data["subregion_code"] is not None and data["subregion_code"].strip() != "": try: parent_region = Region.objects.get(code=str(data['region_code'])) except Exception as e: @@ -75,7 +75,6 @@ class RegionSerializer(serializers.ModelSerializer): data['parent_region'] = parent_region data['code'] = data.pop('subregion_code') del(data['region_code']) - else: data['code'] = data.pop('region_code') del(data['subregion_code']) @@ -100,7 +99,6 @@ class RegionSerializer(serializers.ModelSerializer): class CitySerializer(serializers.ModelSerializer): country_code_2 = serializers.CharField() - subregion_code = serializers.CharField() zip_code = serializers.CharField() is_island = serializers.IntegerField() name = serializers.CharField() @@ -110,7 +108,6 @@ class CitySerializer(serializers.ModelSerializer): model = City fields = ( "country_code_2", - "subregion_code", "zip_code", "is_island", "name", @@ -118,11 +115,11 @@ class CitySerializer(serializers.ModelSerializer): ) def validate(self, data): + data = self.set_old_id(data) + data = self.set_relations(data) data = self.set_is_island(data) data = self.set_code(data) data = self.set_zip_code(data) - data = self.set_relations(data) - data = self.set_old_id(data) return data def create(self, validated_data): @@ -141,18 +138,14 @@ class CitySerializer(serializers.ModelSerializer): def set_relations(self, data): try: - region = Region.objects.get(code=data['code']) - except: - try: - region = Region.objects.get(code=data['subregion_code']) - except Exception as e: - raise ValueError(f"Region not found with {data}: {e}") + region = Region.objects.get(old_id=data['old_id']) + except Region.DoesNotExist as e: + raise ValueError(f"Region not found with {data}: {e}") data['region'] = region - del(data['subregion_code']) try: - country = Country.objects.get(code=data['code']) - except Exception as e: + country = Country.objects.get(old_id=data['old_id']) + except Country.DoesNotExist as e: raise ValueError(f"Region not found with {data}: {e}") data['country'] = country @@ -165,3 +158,4 @@ class CitySerializer(serializers.ModelSerializer): def set_old_id(self, data): data['old_id'] = data.pop('id') + return data