new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.15 on 2019-06-07 07:14
  from __future__ import unicode_literals
  
  from django.contrib.gis.geos import Point
  from django.db import migrations
  
  from bluebottle.clients import properties
  
  
  def map_status(status):
  
      mapping = {
          'plan-new': 'draft',
          'plan-submitted': 'submitted',
          'plan-needs-work': 'needs_work',
          'voting': 'approved',
          'voting-done': 'approved',
          'campaign': 'approved',
          'to-be-continued': 'approved',
          'done-complete': 'approved',
          'done-incomplete': 'approved',
          'closed': 'closed',
          'refunded': 'approved',
      }
      return mapping[status.slug]
  
  
  def truncate(number, limit):
      return int(number * pow(10, limit)) / 10 ^ pow(10, limit)
  
  
  def migrate_projects(apps, schema_editor):
      Project = apps.get_model('projects', 'Project')
      Initiative = apps.get_model('initiatives', 'Initiative')
      Geolocation = apps.get_model('geo', 'Geolocation')
      Country = apps.get_model('geo', 'Country')
      Image = apps.get_model('files', 'Image')
  
      for project in Project.objects.all():
  
          if hasattr(project, 'projectlocation') and project.projectlocation.country:
              if project.projectlocation.latitude and project.projectlocation.longitude:
                  point = Point(
                      truncate(project.projectlocation.longitude, 12),
                      truncate(project.projectlocation.latitude, 12)
                  )
              else:
                  point = None
  
              country = project.country
  
              if not country and project.projectlocation.country and Country.objects.filter(
                      translations__name=project.projectlocation.country
                  ).count():
                  country = Country.objects.filter(
                      translations__name__contains=project.projectlocation.country
                  ).first()
  
              if not country:
                  country = Country.objects.filter(alpha2_code=properties.DEFAULT_COUNTRY_CODE).first()
  
              if country:
                  place = Geolocation.objects.create(
                      street=project.projectlocation.street,
                      postal_code=project.projectlocation.postal_code,
                      country=country,
                      locality=project.projectlocation.city,
                      position=point
                  )
              else:
                  place = None
          else:
              if project.country:
                  place = Geolocation.objects.create(
                      country=project.country,
                      position=Point(0, 0)
                  )
              else:
                  place = None
  
          initiative = Initiative.objects.create(
              title=project.title,
              slug=project.slug,
              pitch=project.pitch or '',
              story=project.story or '',
              theme=project.theme,
              video_url=project.video_url,
              place=place,
              location=project.location,
              owner=project.owner,
              reviewer=project.reviewer,
              status=map_status(project.status)
  
          )
          if project.image:
              try:
                  image = Image.objects.create(owner=project.owner)
                  image.file.save(project.image.name, project.image.file, save=True)
                  initiative.image = image
              except IOError:
                  pass
          initiative.created = project.created
          initiative.categories = project.categories.all()
          initiative.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('projects', '0090_merge_20190222_1101'),
          ('initiatives', '0013_auto_20190527_1131'),
      ]
  
      operations = [
          migrations.RunPython(migrate_projects, migrations.RunPython.noop)
      ]
  
subject: Add project -> initiative migration