new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.10.5 on 2017-01-11 09:46
  from __future__ import unicode_literals
  
  from django.db import migrations
  
  
  def old_recent_messages_lookup(FlowStep, step_uuid, rule_uuid, next_uuid):
      """
      The old way of looking up recent messages from flow step table
      """
      recent_messages = []
  
      if rule_uuid:
          recent_steps = FlowStep.objects.filter(step_uuid=step_uuid, rule_uuid=rule_uuid, next_uuid=next_uuid)
          msg_direction_filter = 'I'
  
      else:
          recent_steps = FlowStep.objects.filter(step_uuid=step_uuid, rule_uuid=None, next_uuid=next_uuid)
          msg_direction_filter = 'O'
  
      recent_steps = recent_steps.prefetch_related('messages', 'contact', 'run').order_by('-left_on')[:20]
  
      flow_id = None
  
      for step in recent_steps:
          if not step.contact.is_test:
              for msg in step.messages.all():
                  if msg.visibility == 'V' and msg.direction == msg_direction_filter:
                      recent_messages.append(msg)
  
          if not flow_id:
              flow_id = step.run.flow_id
          elif flow_id != step.run.flow_id:
              raise ValueError("Flow steps with same node UUIDs for different flows!")
  
      return flow_id, set(recent_messages[:5])
  
  
  def do_populate(FlowStep, FlowPathRecentMessage):
      # fetch all of the node->node path segments
      segments = list(FlowStep.objects.values_list('step_uuid', 'rule_uuid', 'next_uuid').distinct())
      if not segments:
          return
  
      print("Fetched %d unique flow path segments" % len(segments))
      num_segments = 0
      num_messages = 0
  
      for segment in segments:
          step_uuid, rule_uuid, next_uuid = segment
          flow_id, messages = old_recent_messages_lookup(FlowStep, step_uuid, rule_uuid, next_uuid)
  
          from_uuid = rule_uuid or step_uuid
          to_uuid = next_uuid
  
          for msg in messages:
              # some might already have been created by new model code
              FlowPathRecentMessage.objects.get_or_create(flow_id=flow_id, from_uuid=from_uuid, to_uuid=to_uuid, message=msg)
  
          num_segments += 1
          num_messages += len(messages)
  
          if num_segments % 500 == 0:
              print(" > Created %d recent message records for %d of %d segments" % (num_messages, num_segments, len(segments)))
  
  
  def apply_as_migration(apps, schema_editor):
      FlowStep = apps.get_model('flows', 'FlowStep')
      FlowPathRecentMessage = apps.get_model('flows', 'FlowPathRecentMessage')
  
      do_populate(FlowStep, FlowPathRecentMessage)
  
  
  def apply_manual():
      from temba.flows.models import FlowStep, FlowPathRecentMessage
  
      do_populate(FlowStep, FlowPathRecentMessage)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('flows', '0082_auto_20170111_0943'),
      ]
  
      operations = [
          migrations.RunPython(apply_as_migration)
      ]
  
subject: Add migration from other branch