Add city info migration
This commit is contained in:
parent
99e2f30499
commit
4cbb8ed291
|
|
@ -1,5 +1,5 @@
|
||||||
from transfer.serializers.location import CountrySerializer, RegionSerializer, \
|
from transfer.serializers.location import CountrySerializer, RegionSerializer, \
|
||||||
CitySerializer, AddressSerializer, \
|
CitySerializer, AddressSerializer, CityMapSerializer, \
|
||||||
Country
|
Country
|
||||||
from transfer.models import Cities, Locations
|
from transfer.models import Cities, Locations
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
@ -145,6 +145,23 @@ def update_flags():
|
||||||
query.save()
|
query.save()
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_city_map_situation():
|
||||||
|
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 != ""
|
||||||
|
""")
|
||||||
|
|
||||||
|
serialized_data = CityMapSerializer(data=queryset, many=True)
|
||||||
|
if serialized_data.is_valid():
|
||||||
|
serialized_data.save()
|
||||||
|
else:
|
||||||
|
pprint(f"City info serializer errors: {serialized_data.errors}")
|
||||||
|
|
||||||
|
|
||||||
data_types = {
|
data_types = {
|
||||||
"dictionaries": [
|
"dictionaries": [
|
||||||
transfer_countries,
|
transfer_countries,
|
||||||
|
|
@ -154,5 +171,9 @@ data_types = {
|
||||||
],
|
],
|
||||||
"update_country_flag": [
|
"update_country_flag": [
|
||||||
update_flags
|
update_flags
|
||||||
|
],
|
||||||
|
"update_city_info": [
|
||||||
|
migrate_city_map_situation()
|
||||||
]
|
]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,8 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
LONG_DATA_TYPES = [
|
LONG_DATA_TYPES = [
|
||||||
'update_country_flag',
|
'update_country_flag',
|
||||||
'comment'
|
'comment',
|
||||||
|
'update_city_info'
|
||||||
]
|
]
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from location.models import Country, Region, City, Address
|
from location.models import Country, Region, City, Address
|
||||||
from django.contrib.gis.geos import Point
|
from django.contrib.gis.geos import Point
|
||||||
from django.contrib.gis.geos import GEOSGeometry
|
from django.core.exceptions import MultipleObjectsReturned
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
class CountrySerializer(serializers.ModelSerializer):
|
class CountrySerializer(serializers.ModelSerializer):
|
||||||
|
|
@ -68,7 +67,6 @@ class RegionSerializer(serializers.ModelSerializer):
|
||||||
return region
|
return region
|
||||||
|
|
||||||
def set_code(self, data):
|
def set_code(self, data):
|
||||||
print(data)
|
|
||||||
if "subregion_code" in data and data["subregion_code"] is not None and data["subregion_code"].strip() != "":
|
if "subregion_code" in data and data["subregion_code"] is not None and data["subregion_code"].strip() != "":
|
||||||
try:
|
try:
|
||||||
parent_region = Region.objects.filter(code=str(data['region_code'])).first()
|
parent_region = Region.objects.filter(code=str(data['region_code'])).first()
|
||||||
|
|
@ -266,3 +264,58 @@ class AddressSerializer(serializers.ModelSerializer):
|
||||||
del(data['longitude'])
|
del(data['longitude'])
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
class CityMapSerializer(serializers.ModelSerializer):
|
||||||
|
id = serializers.IntegerField()
|
||||||
|
map1 = serializers.CharField(allow_blank=True, allow_null=True)
|
||||||
|
map2 = serializers.CharField(allow_blank=True, allow_null=True)
|
||||||
|
map_ref = serializers.CharField(allow_blank=True, allow_null=True)
|
||||||
|
situation = serializers.CharField(allow_blank=True, allow_null=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = City
|
||||||
|
fields = (
|
||||||
|
"id",
|
||||||
|
"map1",
|
||||||
|
"map2",
|
||||||
|
"map_ref",
|
||||||
|
"situation",
|
||||||
|
)
|
||||||
|
|
||||||
|
def validate(self, data):
|
||||||
|
data = self.set_old_id(data)
|
||||||
|
data = self.set_city(data)
|
||||||
|
data = self.set_null_fields(data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
city = validated_data.pop('city')
|
||||||
|
city.update(
|
||||||
|
map1=validated_data['map1'],
|
||||||
|
map2=validated_data['map2'],
|
||||||
|
map_ref=validated_data['map_ref'],
|
||||||
|
situation=validated_data['situation']
|
||||||
|
)
|
||||||
|
|
||||||
|
def set_old_id(self, data):
|
||||||
|
data['old_id'] = data.pop("id")
|
||||||
|
return data
|
||||||
|
|
||||||
|
def set_city(self, data):
|
||||||
|
try:
|
||||||
|
city = City.objects.get(old_id=data['old_id'])
|
||||||
|
except City.DoesNotExist as e:
|
||||||
|
raise ValueError(f"Cannot find city with id = {data['old_id']}: {e}")
|
||||||
|
except MultipleObjectsReturned as e:
|
||||||
|
raise ValueError(f"Find multiple cities with id = {data['old_id']}: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
data['city'] = city
|
||||||
|
return data
|
||||||
|
|
||||||
|
def set_null_fields(self, data):
|
||||||
|
for field in ["map1", "map2", "map_ref", "situation"]:
|
||||||
|
if field not in data or data[field] is None:
|
||||||
|
data[field] = ""
|
||||||
|
return data
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user