Tmp commit
This commit is contained in:
parent
8bcf4c9ca0
commit
7eb241ffed
|
|
@ -21,6 +21,7 @@ class Country(TranslatedFieldsMixin, SVGImageMixin, ProjectBaseMixin):
|
|||
low_price = models.IntegerField(default=25, verbose_name=_('Low price'))
|
||||
high_price = models.IntegerField(default=50, verbose_name=_('High price'))
|
||||
languages = models.ManyToManyField(Language, verbose_name=_('Languages'))
|
||||
old_id = models.IntegerField(null=True, blank=True, default=None)
|
||||
|
||||
@property
|
||||
def country_id(self):
|
||||
|
|
|
|||
|
|
@ -112,12 +112,6 @@ card = {
|
|||
"fields": {
|
||||
"name": "name",
|
||||
}
|
||||
},
|
||||
"CityPhotos": {
|
||||
"key": "city",
|
||||
"fields": {
|
||||
"coordinates": "geometries"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from django.db.models import Q
|
||||
from django.db.models import Q, F
|
||||
|
||||
from transfer.serializers.location import CountrySerializer, RegionSerializer, CitySerializer
|
||||
from transfer.models import Cities
|
||||
|
|
@ -47,17 +47,26 @@ def transfer_regions():
|
|||
pprint(f"Child regions serializer errors: {serialized_with_subregion.errors}")
|
||||
|
||||
|
||||
|
||||
def transfer_cities():
|
||||
pass
|
||||
queryset = Cities.objects.exclude(Q(region_code__isnull=True) |
|
||||
Q(country_code_2__isnull=True) |
|
||||
Q(region_code="") |
|
||||
Q(country_code_2="")).only("name", "latitude", "longitude", "subregion_code",
|
||||
"country_code_2", "zip_code", "is_island")
|
||||
|
||||
serialized_data = CitySerializer(data=list(queryset.values()), many=True)
|
||||
if serialized_data.is_valid():
|
||||
serialized_data.save()
|
||||
else:
|
||||
pprint(f"City serializer errors: {serialized_data.errors}")
|
||||
|
||||
|
||||
data_types = {
|
||||
"dictionaries": [
|
||||
transfer_countries,
|
||||
transfer_regions,
|
||||
],
|
||||
"tmp": [
|
||||
transfer_regions,
|
||||
transfer_countries,
|
||||
# transfer_cities
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,33 @@
|
|||
from rest_framework import serializers
|
||||
from location.models import Country, Region
|
||||
from location.models import Country, Region, City
|
||||
|
||||
|
||||
class CountrySerializer(serializers.ModelSerializer):
|
||||
country_code_2 = serializers.CharField()
|
||||
id = serializers.IntegerField()
|
||||
|
||||
class Meta:
|
||||
model = Country
|
||||
fields = (
|
||||
"id",
|
||||
"country_code_2",
|
||||
)
|
||||
|
||||
def validate(self, data):
|
||||
data["code"] = self.get_country_code(data)
|
||||
del(data['country_code_2'])
|
||||
|
||||
data['old_id'] = data.pop('id')
|
||||
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
# Some countries already in database
|
||||
country, _ = Country.objects.get_or_create(**validated_data)
|
||||
return Country
|
||||
try:
|
||||
country = Country.objects.get(code=validated_data['code'])
|
||||
except Country.DoesNotExists:
|
||||
country = Country.objects.create(**validated_data)
|
||||
return country
|
||||
|
||||
def get_country_code(self, obj):
|
||||
return obj.get("country_code_2")
|
||||
|
|
@ -41,14 +49,15 @@ class RegionSerializer(serializers.ModelSerializer):
|
|||
def validate(self, data):
|
||||
if "subregion" in data and data["subregion"] is not None:
|
||||
try:
|
||||
parent_region = Region.objects.get(code=data['region_code'])
|
||||
parent_region = Region.objects.get(code=str(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')
|
||||
data['code'] = str(data.pop('subregion_code'))
|
||||
|
||||
else:
|
||||
data['code'] = data.pop('region_code')
|
||||
data['code'] = str(data.pop('region_code'))
|
||||
|
||||
try:
|
||||
country = Country.objects.get(code=data['country_code_2'])
|
||||
except Exception as e:
|
||||
|
|
@ -70,4 +79,64 @@ class RegionSerializer(serializers.ModelSerializer):
|
|||
|
||||
|
||||
class CitySerializer(serializers.ModelSerializer):
|
||||
pass
|
||||
country_code_2 = serializers.CharField()
|
||||
subregion_code = serializers.CharField()
|
||||
zip_code = serializers.CharField()
|
||||
is_island = serializers.IntegerField()
|
||||
name = serializers.CharField()
|
||||
|
||||
class Meta:
|
||||
model = City
|
||||
fields = (
|
||||
"country_code_2",
|
||||
"subregion_code",
|
||||
"zip_code",
|
||||
"is_island",
|
||||
"name",
|
||||
)
|
||||
|
||||
def validate(self, data):
|
||||
data = self.set_is_island(data)
|
||||
data = self.set_code(data)
|
||||
data = self.set_zip_code(data)
|
||||
data = self.set_relations(data)
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
return City.objects.create(**validated_data)
|
||||
|
||||
def set_is_island(self, data):
|
||||
data['is_island'] = True if "is_island" in data \
|
||||
and data['is_island'] is not None \
|
||||
and data['is_island'] > 0 \
|
||||
else False
|
||||
return data
|
||||
|
||||
def set_code(self, data):
|
||||
data['code'] = data.pop('country_code_2')
|
||||
return data
|
||||
|
||||
def set_relations(self, data):
|
||||
try:
|
||||
region = Region.objects.get(code=data['code'])
|
||||
except:
|
||||
try:
|
||||
region = Region.objects.get(code=data['subregion_code'])
|
||||
except Exception as e:
|
||||
raise ValueError(f"Region not found with {data}: {e}")
|
||||
data['region'] = region
|
||||
del(data['subregion_code'])
|
||||
|
||||
try:
|
||||
country = Country.objects.get(code=data['code'])
|
||||
except Exception as e:
|
||||
raise ValueError(f"Region not found with {data}: {e}")
|
||||
|
||||
data['country'] = country
|
||||
|
||||
return data
|
||||
|
||||
def set_zip_code(self, data):
|
||||
data['postal_code'] = data.pop('zip_code')
|
||||
return data
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user