Add unused subregions

This commit is contained in:
littlewolf 2019-12-20 19:35:59 +03:00
parent 3dd76503a5
commit 215f36eb9b
3 changed files with 65 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@ -235,7 +235,7 @@ def migrate_city_photos():
# Update location models with ruby library
# Utils functions defined before transfer functions
def get_ruby_socket(params):
url = 'http://172.29.0.1:5678' # docker host
url = 'http://172.21.0.1:5678' # docker host
response = get(url, params=params)
try:
data = json.loads(response.text)
@ -318,6 +318,32 @@ def get_ruby_data():
return ruby_data
def get_unused_data():
ruby_data = {}
error_file = open(f"{settings.PROJECT_ROOT}/ruby_unused_error.txt", "w")
countries = Country.objects.all()
for country in countries:
ruby_params = {}
ruby_response = get_ruby_socket({"country_regions": country.code})
if ruby_response is None:
continue
if "error" in ruby_response.keys():
error_file.write(f"{json.dumps(ruby_params)}: {json.dumps(ruby_response)}\n")
continue
ruby_data[country.code] = ruby_response
error_file.close()
with open(f"{settings.PROJECT_ROOT}/apps/location/ruby_unused_data.py", "w") as ruby_data_file:
ruby_data_file.write(json.dumps(ruby_data))
# Add correct objects of Country, Region and City with mysql_ids array (Country, Region) and mysql_id (City)
def add_correct_location_models(ruby_data):
for mysql_id, city_object in tqdm(ruby_data.items()):
@ -727,6 +753,41 @@ def setup_clean_db():
transfer_city_gallery()
def set_unused_regions():
ruby_data_file = open(f"{settings.PROJECT_ROOT}/apps/location/ruby_unused_data.py", "r")
ruby_data = json.loads(ruby_data_file.read())
for country_code, regions in ruby_data.items():
try:
country = Country.objects.get(code=country_code)
except Country.DoesNotExist:
print(f"Country with code {country_code} does not exists")
continue
for region_code, region_obj in regions.items():
try:
region = Region.objects.get(code=region_code, country=country)
except Region.DoesNotExist:
region = Region.objects.create(
name=region_obj['name'],
code=region_code,
country=country
)
if "subregions" in region_obj:
for subregion_code, subregion in region_obj['subregions'].items():
try:
subregion = Region.objects.get(code=subregion, country=country)
except Region.DoesNotExist:
subregion = Region.objects.create(
name=subregion,
code=subregion_code,
country=country,
parent_region=region
)
data_types = {
"dictionaries": [
# transfer_countries,
@ -761,4 +822,5 @@ data_types = {
],
"setup_clean_db": [setup_clean_db],
"set_unused_regions": [set_unused_regions]
}

View File

@ -57,6 +57,7 @@ class Command(BaseCommand):
'add_fake_country',
'setup_clean_db',
'languages', # №4 - перенос языков
'set_unused_regions',
]
def handle(self, *args, **options):