159 lines
8.2 KiB
Python
159 lines
8.2 KiB
Python
from transfer.serializers.location import CountrySerializer, RegionSerializer, \
|
|
CitySerializer, AddressSerializer, \
|
|
Country
|
|
from transfer.models import Cities, Locations
|
|
from pprint import pprint
|
|
|
|
from requests import get
|
|
|
|
|
|
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.id, 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 cities.id,
|
|
cities.region_code,
|
|
cities.country_code_2,
|
|
cities.subregion_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 cities.id,
|
|
cities.region_code,
|
|
cities.country_code_2,
|
|
cities.subregion_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}")
|
|
|
|
|
|
def update_flags():
|
|
queryset = Country.objects.only("id", "code", "svg_image").filter(old_id__isnull=False)
|
|
link_to_request = "https://s3.eu-central-1.amazonaws.com/gm-test.com/media"
|
|
|
|
for query in queryset:
|
|
svg_link = f"/svg/country/10-31-2019/{query.code}.svg"
|
|
resp = get(f"{link_to_request}{svg_link}")
|
|
if resp.status_code == 200:
|
|
query.svg_image = svg_link
|
|
query.save()
|
|
|
|
|
|
data_types = {
|
|
"dictionaries": [
|
|
transfer_countries,
|
|
transfer_regions,
|
|
transfer_cities,
|
|
transfer_addresses
|
|
],
|
|
"update_country_flag": [
|
|
update_flags
|
|
]
|
|
}
|