# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2017-09-29 18:12
from __future__ import unicode_literals

from django.db import migrations


def set_profiles_in_users_from_relation_in_profiles(apps, schema_editor):
    """
    Profiles maintain a relation to users. This function sets this up in the
    reverse direction. Users will now have a `profile` field that optionally
    points to a single profile.
    """
    UserProfile = apps.get_model('profiles', 'UserProfile')
    profiles_with_users = UserProfile.objects.filter(
        user__isnull=False
    ).select_related('user')

    for profile in profiles_with_users:
        user = profile.user
        user.profile = profile
        user.save()


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0007_emailuser_profile'),
    ]

    operations = [
        migrations.RunPython(set_profiles_in_users_from_relation_in_profiles)
    ]
