diff --git a/apps/location/transfer_data.py b/apps/location/transfer_data.py index 6b001e46..657a8853 100644 --- a/apps/location/transfer_data.py +++ b/apps/location/transfer_data.py @@ -8,12 +8,12 @@ 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))) " - "GROUP BY cities.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))) + GROUP BY cities.country_code_2""") queryset = [vars(query) for query in queryset] @@ -25,34 +25,40 @@ def transfer_countries(): def transfer_regions(): - regions_without_subregion_queryset = Cities.objects.\ - exclude(Q(subregion_code__isnull=False) | - Q(region_code__isnull=True) | - Q(country_code_2__isnull=True) | - Q(region_code="") | - Q(country_code_2="")).\ - values('id', 'region_code', 'country_code_2', 'subregion_code').distinct() + 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))) + GROUP BY region_code""") - serialized_without_subregion = RegionSerializer(data=list(regions_without_subregion_queryset.values()), many=True) + regions_without_subregion_queryset = [vars(query) for query in regions_without_subregion_queryset] + + serialized_without_subregion = RegionSerializer(data=regions_without_subregion_queryset, many=True) if serialized_without_subregion.is_valid(): serialized_without_subregion.save() else: pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}") - - regions_with_subregion_queryset = Cities.objects. \ - exclude(Q(subregion_code__isnull=True) | - Q(subregion_code="") | - Q(region_code__isnull=True) | - Q(country_code_2__isnull=True) | - Q(region_code="") | - Q(country_code_2="")). \ - values('region_code', 'country_code_2', 'subregion_code').distinct() - - serialized_with_subregion = RegionSerializer(data=list(regions_with_subregion_queryset.values()), many=True) - if serialized_with_subregion.is_valid(): - serialized_with_subregion.save() - else: - pprint(f"Child regions serializer errors: {serialized_with_subregion.errors}") + # + # regions_with_subregion_queryset = Cities.objects. \ + # exclude(Q(subregion_code__isnull=True) | + # Q(subregion_code="") | + # Q(region_code__isnull=True) | + # Q(country_code_2__isnull=True) | + # Q(region_code="") | + # Q(country_code_2="")). \ + # values('region_code', 'country_code_2', 'subregion_code').distinct() + # + # serialized_with_subregion = RegionSerializer(data=list(regions_with_subregion_queryset.values()), many=True) + # if serialized_with_subregion.is_valid(): + # serialized_with_subregion.save() + # else: + # pprint(f"Child regions serializer errors: {serialized_with_subregion.errors}") def transfer_cities(): @@ -71,10 +77,10 @@ def transfer_cities(): data_types = { "dictionaries": [ + transfer_countries, ], "tmp": [ - transfer_countries, - # transfer_regions, + transfer_regions, # transfer_cities ] } diff --git a/apps/transfer/serializers/location.py b/apps/transfer/serializers/location.py index c9795957..4895b85e 100644 --- a/apps/transfer/serializers/location.py +++ b/apps/transfer/serializers/location.py @@ -49,27 +49,9 @@ class RegionSerializer(serializers.ModelSerializer): ) def validate(self, data): - if "subregion" in data and data["subregion"] is not None: - try: - parent_region = Region.objects.get(code=str(data['region_code'])) - except Exception as e: - raise ValueError(f"Parent region error with {data}: {e}") - data['parent_region'] = parent_region - data['code'] = str(data.pop('subregion_code')) - - else: - data['code'] = str(data.pop('region_code')) - - try: - country = Country.objects.get(code=data['country_code_2']) - except Exception as e: - raise ValueError(f"Country error with {data}: {e}") - data["country"] = country - - del (data['country_code_2']) - del(data['subregion_code']) - - data['old_id'] = data.pop("id") + data = self.set_code(data) + data = self.set_country(data) + data = self.set_old_id(data) return data @@ -81,6 +63,37 @@ class RegionSerializer(serializers.ModelSerializer): region = Region.objects.create(**validated_data) return region + def set_code(self, data): + if "subregion_code" in data and data["subregion_code"] is not None: + try: + parent_region = Region.objects.get(code=str(data['region_code'])) + except Exception as e: + raise ValueError(f"Parent region error with {data}: {e}") + + data['parent_region'] = parent_region + data['code'] = data.pop('subregion_code') + + else: + data['code'] = data.pop('region_code') + del(data['subregion_code']) + + return data + + def set_country(self, data): + try: + country = Country.objects.get(code=data['country_code_2']) + except Exception as e: + raise ValueError(f"Country error with {data}: {e}") + + data["country"] = country + del(data['country_code_2']) + + return data + + def set_old_id(self, data): + data['old_id'] = data.pop("id") + return data + class CitySerializer(serializers.ModelSerializer): country_code_2 = serializers.CharField()