124 lines
6.1 KiB
Python
124 lines
6.1 KiB
Python
from django.db.models import Q, QuerySet
|
|
|
|
from transfer.serializers.location import CountrySerializer, RegionSerializer, CitySerializer, AddressSerializer
|
|
from transfer.models import Cities, Locations
|
|
from pprint import pprint
|
|
|
|
|
|
def transfer_countries():
|
|
queryset = Cities.objects.raw("""SELECT cities.id, cities.country_code_2
|
|
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]
|
|
|
|
serialized_data = CountrySerializer(data=queryset, many=True)
|
|
if serialized_data.is_valid():
|
|
serialized_data.save()
|
|
else:
|
|
pprint(f"Country serializer errors: {serialized_data.errors}")
|
|
|
|
|
|
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
|
|
(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]
|
|
|
|
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.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]
|
|
|
|
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():
|
|
queryset = Cities.objects.raw("""SELECT cities.id, cities.name, cities.country_code_2, cities.zip_code,
|
|
cities.is_island, cities.region_code, cities.subregion_code
|
|
FROM cities WHERE
|
|
region_code IS NOT NULL AND
|
|
region_code != "" AND
|
|
country_code_2 IS NOT NULL AND
|
|
country_code_2 != ""
|
|
""")
|
|
|
|
queryset = [vars(query) for query in queryset]
|
|
|
|
serialized_data = CitySerializer(data=queryset, many=True)
|
|
if serialized_data.is_valid():
|
|
serialized_data.save()
|
|
else:
|
|
pprint(f"City serializer errors: {serialized_data.errors}")
|
|
|
|
|
|
def transfer_addresses():
|
|
queryset = Locations.objects.raw("""SELECT locations.id, locations.zip_code, locations.longitude,
|
|
locations.latitude, locations.address, locations.city_id
|
|
FROM locations WHERE
|
|
locations.address != "" AND
|
|
locations.address IS NOT NULL AND
|
|
locations.city_id IS NOT NULL AND
|
|
locations.city_id IN (SELECT cities.id
|
|
FROM cities WHERE
|
|
region_code IS NOT NULL AND
|
|
region_code != "" AND
|
|
country_code_2 IS NOT NULL AND
|
|
country_code_2 != "")""")
|
|
|
|
queryset = [vars(query) for query in queryset]
|
|
|
|
serialized_data = AddressSerializer(data=queryset, many=True)
|
|
if serialized_data.is_valid():
|
|
serialized_data.save()
|
|
else:
|
|
pprint(f"Address serializer errors: {serialized_data.errors}")
|
|
|
|
|
|
|
|
data_types = {
|
|
"dictionaries": [
|
|
transfer_countries,
|
|
transfer_regions,
|
|
transfer_cities,
|
|
transfer_addresses
|
|
]
|
|
}
|