From 17c5e7e702e0e69c974f3161de615bf545f2ac3b Mon Sep 17 00:00:00 2001 From: Tasos Katsoulas Date: Thu, 23 Mar 2017 21:09:08 +0200 Subject: [PATCH] Re-add geodata migration due to failed release. --- .../migrations/0015_auto_20170323_1202.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 mozillians/users/migrations/0015_auto_20170323_1202.py diff --git a/mozillians/users/migrations/0015_auto_20170323_1202.py b/mozillians/users/migrations/0015_auto_20170323_1202.py new file mode 100644 index 000000000..ad93a87ac --- /dev/null +++ b/mozillians/users/migrations/0015_auto_20170323_1202.py @@ -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), + ]