This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1671 from akatsoulas/re-add-data-migration
Re-add geodata migration due to failed release.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
from django.db.models import Q | ||
|
||
|
||
def migrate_countries(apps, schema_editor): | ||
UserProfile = apps.get_model('users', 'UserProfile') | ||
|
||
# cities_light data models | ||
Country = apps.get_model('cities_light', 'Country') | ||
|
||
for profile in UserProfile.objects.filter(geo_country__isnull=False): | ||
|
||
# Query countries based on `name` and `alternate_names` | ||
country_query = (Q(name=profile.geo_country.name) | | ||
Q(alternate_names__icontains=profile.geo_country.name)) | ||
cities_countries = Country.objects.filter(country_query) | ||
|
||
country = None | ||
if cities_countries.exists(): | ||
country = cities_countries[0] | ||
|
||
kwargs = { | ||
'country': country, | ||
'privacy_country': profile.privacy_geo_country | ||
} | ||
UserProfile.objects.filter(pk=profile.id).update(**kwargs) | ||
|
||
|
||
def migrate_cities_regions(apps, schema_editor): | ||
UserProfile = apps.get_model('users', 'UserProfile') | ||
City = apps.get_model('cities_light', 'City') | ||
|
||
for profile in UserProfile.objects.filter(country__isnull=False, geo_city__isnull=False): | ||
|
||
# Query cities based on `name`, `alternate_names` and `country` | ||
city_query = ((Q(name=profile.geo_city.name) | | ||
Q(alternate_names__icontains=profile.geo_city.name)) & | ||
Q(country=profile.country)) | ||
|
||
city = None | ||
region = None | ||
cities = City.objects.filter(city_query) | ||
if cities.exists(): | ||
city = cities[0] | ||
region = city.region | ||
|
||
kwargs = { | ||
'region': region, | ||
'city': city, | ||
'privacy_region': profile.privacy_geo_region, | ||
'privacy_city': profile.privacy_geo_city | ||
} | ||
|
||
UserProfile.objects.filter(pk=profile.id).update(**kwargs) | ||
|
||
|
||
def backwards(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0014_auto_20170322_0342'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(migrate_countries, backwards), | ||
migrations.RunPython(migrate_cities_regions, backwards), | ||
] |