Add child regions
This commit is contained in:
parent
cd724de65f
commit
8bcf4c9ca0
|
|
@ -1,6 +1,6 @@
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
from transfer.serializers.location import CountrySerializer, RegionSerializer
|
from transfer.serializers.location import CountrySerializer, RegionSerializer, CitySerializer
|
||||||
from transfer.models import Cities
|
from transfer.models import Cities
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
|
|
@ -31,10 +31,33 @@ def transfer_regions():
|
||||||
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. \
|
||||||
|
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}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def transfer_cities():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
data_types = {
|
data_types = {
|
||||||
"dictionaries": [
|
"dictionaries": [
|
||||||
transfer_countries,
|
transfer_countries,
|
||||||
|
],
|
||||||
|
"tmp": [
|
||||||
transfer_regions,
|
transfer_regions,
|
||||||
|
# transfer_cities
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,11 +39,20 @@ class RegionSerializer(serializers.ModelSerializer):
|
||||||
)
|
)
|
||||||
|
|
||||||
def validate(self, data):
|
def validate(self, data):
|
||||||
|
if "subregion" in data and data["subregion"] is not None:
|
||||||
|
try:
|
||||||
|
parent_region = Region.objects.get(code=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')
|
data['code'] = data.pop('region_code')
|
||||||
try:
|
try:
|
||||||
country = Country.objects.get(code=data['country_code_2'])
|
country = Country.objects.get(code=data['country_code_2'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError(f"{data}: {e}")
|
raise ValueError(f"Country error with {data}: {e}")
|
||||||
data["country"] = country
|
data["country"] = country
|
||||||
|
|
||||||
del (data['country_code_2'])
|
del (data['country_code_2'])
|
||||||
|
|
@ -58,3 +67,7 @@ class RegionSerializer(serializers.ModelSerializer):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError(f"{validated_data}: {e}")
|
raise ValueError(f"{validated_data}: {e}")
|
||||||
return region
|
return region
|
||||||
|
|
||||||
|
|
||||||
|
class CitySerializer(serializers.ModelSerializer):
|
||||||
|
pass
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user