Fix regions with subregions
This commit is contained in:
parent
07f226604e
commit
0d30543664
|
|
@ -25,16 +25,16 @@ 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)))
|
||||
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""")
|
||||
|
||||
regions_without_subregion_queryset = [vars(query) for query in regions_without_subregion_queryset]
|
||||
|
|
@ -44,21 +44,28 @@ def transfer_regions():
|
|||
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.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)
|
||||
GROUP BY region_code""")
|
||||
|
||||
regions_with_subregion_queryset = [vars(query) for query in regions_with_subregion_queryset]
|
||||
|
||||
serialized_with_subregion = RegionSerializer(data=regions_with_subregion_queryset, 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():
|
||||
|
|
|
|||
|
|
@ -49,9 +49,9 @@ class RegionSerializer(serializers.ModelSerializer):
|
|||
)
|
||||
|
||||
def validate(self, data):
|
||||
data = self.set_old_id(data)
|
||||
data = self.set_code(data)
|
||||
data = self.set_country(data)
|
||||
data = self.set_old_id(data)
|
||||
|
||||
return data
|
||||
|
||||
|
|
@ -61,6 +61,8 @@ class RegionSerializer(serializers.ModelSerializer):
|
|||
region = Region.objects.get(old_id=validated_data['old_id'])
|
||||
except Region.DoesNotExist:
|
||||
region = Region.objects.create(**validated_data)
|
||||
except Exception as e:
|
||||
raise ValueError(f"REGION ERROR: {validated_data}: {e}")
|
||||
return region
|
||||
|
||||
def set_code(self, data):
|
||||
|
|
@ -72,6 +74,7 @@ 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')
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user