Fix regions without subregion
This commit is contained in:
parent
0db2638eb8
commit
07f226604e
|
|
@ -8,12 +8,12 @@ from pprint import pprint
|
||||||
def transfer_countries():
|
def transfer_countries():
|
||||||
# queryset = Cities.objects.exclude(Q(country_code_2__isnull=True) | Q(country_code_2="")) \
|
# 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")
|
# .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 "
|
queryset = Cities.objects.raw("""SELECT cities.id, cities.country_code_2
|
||||||
"FROM cities "
|
FROM cities
|
||||||
"WHERE NOT ((cities.country_code_2 IS NULL "
|
WHERE NOT ((cities.country_code_2 IS NULL
|
||||||
"OR (cities.country_code_2 = '' "
|
OR (cities.country_code_2 = ''
|
||||||
"AND cities.country_code_2 IS NOT NULL))) "
|
AND cities.country_code_2 IS NOT NULL)))
|
||||||
"GROUP BY cities.country_code_2")
|
GROUP BY cities.country_code_2""")
|
||||||
|
|
||||||
queryset = [vars(query) for query in queryset]
|
queryset = [vars(query) for query in queryset]
|
||||||
|
|
||||||
|
|
@ -25,34 +25,40 @@ def transfer_countries():
|
||||||
|
|
||||||
|
|
||||||
def transfer_regions():
|
def transfer_regions():
|
||||||
regions_without_subregion_queryset = Cities.objects.\
|
regions_without_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code,
|
||||||
exclude(Q(subregion_code__isnull=False) |
|
cities.country_code_2, cities.subregion_code
|
||||||
Q(region_code__isnull=True) |
|
FROM cities
|
||||||
Q(country_code_2__isnull=True) |
|
WHERE NOT ((cities.subregion_code IS NOT NULL
|
||||||
Q(region_code="") |
|
OR cities.region_code IS NULL
|
||||||
Q(country_code_2="")).\
|
OR cities.country_code_2 IS NULL
|
||||||
values('id', 'region_code', 'country_code_2', 'subregion_code').distinct()
|
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():
|
if serialized_without_subregion.is_valid():
|
||||||
serialized_without_subregion.save()
|
serialized_without_subregion.save()
|
||||||
else:
|
else:
|
||||||
pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}")
|
pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}")
|
||||||
|
#
|
||||||
regions_with_subregion_queryset = Cities.objects. \
|
# regions_with_subregion_queryset = Cities.objects. \
|
||||||
exclude(Q(subregion_code__isnull=True) |
|
# exclude(Q(subregion_code__isnull=True) |
|
||||||
Q(subregion_code="") |
|
# Q(subregion_code="") |
|
||||||
Q(region_code__isnull=True) |
|
# Q(region_code__isnull=True) |
|
||||||
Q(country_code_2__isnull=True) |
|
# Q(country_code_2__isnull=True) |
|
||||||
Q(region_code="") |
|
# Q(region_code="") |
|
||||||
Q(country_code_2="")). \
|
# Q(country_code_2="")). \
|
||||||
values('region_code', 'country_code_2', 'subregion_code').distinct()
|
# values('region_code', 'country_code_2', 'subregion_code').distinct()
|
||||||
|
#
|
||||||
serialized_with_subregion = RegionSerializer(data=list(regions_with_subregion_queryset.values()), many=True)
|
# serialized_with_subregion = RegionSerializer(data=list(regions_with_subregion_queryset.values()), many=True)
|
||||||
if serialized_with_subregion.is_valid():
|
# if serialized_with_subregion.is_valid():
|
||||||
serialized_with_subregion.save()
|
# serialized_with_subregion.save()
|
||||||
else:
|
# else:
|
||||||
pprint(f"Child regions serializer errors: {serialized_with_subregion.errors}")
|
# pprint(f"Child regions serializer errors: {serialized_with_subregion.errors}")
|
||||||
|
|
||||||
|
|
||||||
def transfer_cities():
|
def transfer_cities():
|
||||||
|
|
@ -71,10 +77,10 @@ def transfer_cities():
|
||||||
|
|
||||||
data_types = {
|
data_types = {
|
||||||
"dictionaries": [
|
"dictionaries": [
|
||||||
|
transfer_countries,
|
||||||
],
|
],
|
||||||
"tmp": [
|
"tmp": [
|
||||||
transfer_countries,
|
transfer_regions,
|
||||||
# transfer_regions,
|
|
||||||
# transfer_cities
|
# transfer_cities
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,27 +49,9 @@ class RegionSerializer(serializers.ModelSerializer):
|
||||||
)
|
)
|
||||||
|
|
||||||
def validate(self, data):
|
def validate(self, data):
|
||||||
if "subregion" in data and data["subregion"] is not None:
|
data = self.set_code(data)
|
||||||
try:
|
data = self.set_country(data)
|
||||||
parent_region = Region.objects.get(code=str(data['region_code']))
|
data = self.set_old_id(data)
|
||||||
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")
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
@ -81,6 +63,37 @@ class RegionSerializer(serializers.ModelSerializer):
|
||||||
region = Region.objects.create(**validated_data)
|
region = Region.objects.create(**validated_data)
|
||||||
return region
|
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):
|
class CitySerializer(serializers.ModelSerializer):
|
||||||
country_code_2 = serializers.CharField()
|
country_code_2 = serializers.CharField()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user