Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1671 from akatsoulas/re-add-data-migration
Browse files Browse the repository at this point in the history
Re-add geodata migration due to failed release.
  • Loading branch information
johngian authored Mar 23, 2017
2 parents 1d37071 + 17c5e7e commit 108f2b0
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions mozillians/users/migrations/0015_auto_20170323_1202.py
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),
]

0 comments on commit 108f2b0

Please sign in to comment.