new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.18 on 2019-07-05 13:10
  from __future__ import unicode_literals
  
  from django.db import migrations
  from django.db.models import Q
  
  from accelerator_abstract.models.base_startup_role import BaseStartupRole
  from accelerator_abstract.models.base_user_role import BaseUserRole
  
  
  def remove_eit_only_finalists_from_alumni_program(apps, schema_editor):
      ProgramRoleGrant = apps.get_model('accelerator', 'ProgramRoleGrant')
      ProgramFamily = apps.get_model('accelerator', 'ProgramFamily')
      StartupTeamMember = apps.get_model('accelerator', 'StartupTeamMember')
      StartupStatus = apps.get_model('accelerator', 'StartupStatus')
  
      alum_program_family = ProgramFamily.objects.filter(
          name="Global Alumni Program").first()
      eit_program_family = ProgramFamily.objects.filter(
          name="EIT Food Accelerator").first()
  
      eit_finalist_startups = _get_eit_finalist_startups(
          StartupStatus, eit_program_family)
  
      eit_alum_startups = _get_eit_alum_startups(
          StartupStatus, eit_finalist_startups, alum_program_family)
  
      eit_startups_in_other_programs = _get_eit_startups_in_other_programs(
          StartupStatus,
          [alum_program_family, eit_program_family],
          eit_finalist_startups
      )
  
      eit_only_startups = set(eit_alum_startups) - set(
          eit_startups_in_other_programs)
  
      eit_alums = StartupTeamMember.objects.filter(
          startup__in=eit_alum_startups
      ).values_list('user', flat=True)
  
      eit_alums_in_other_programs = _get_eit_alums_in_other_programs(
          ProgramRoleGrant,
          [alum_program_family, eit_program_family],
          eit_alums)
  
      eit_only_alums = set(eit_alums) - set(eit_alums_in_other_programs)
  
      alum_pf_filter = Q(
          program_startup_status__program__program_family=alum_program_family
      )
  
      if eit_only_startups and eit_only_alums:
          StartupStatus.objects.filter(
              alum_pf_filter,
              startup_id__in=eit_only_startups,
          ).delete()
          ProgramRoleGrant.objects.filter(
              person_id__in=eit_only_alums,
              program_role__program__program_family=alum_program_family,
          ).delete()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('accelerator', '0061_remove_eventbrite_organization_id_field'),
      ]
  
      operations = [
          migrations.RunPython(
              remove_eit_only_finalists_from_alumni_program,
              migrations.RunPython.noop)
      ]
  
  
  def _get_eit_finalist_startups(startup_status_model, eit_program_family):
      return startup_status_model.objects.filter(
          program_startup_status__program__program_family=eit_program_family,
          program_startup_status__startup_role__name=BaseStartupRole.FINALIST
          ).values_list("startup", flat=True)
  
  
  def _get_eit_alum_startups(
          startup_status_model, eit_finalist_startups, alum_program_family):
      return startup_status_model.objects.filter(
          startup_id__in=eit_finalist_startups,
          program_startup_status__program__program_family=alum_program_family
          ).values_list("startup", flat=True)
  
  
  def _get_eit_alums_in_other_programs(
          program_role_grant_model, exclude_programs, eit_alums):
      return program_role_grant_model.objects.filter(
          ~Q(program_role__program__program_family__in=exclude_programs),
          program_role__user_role__name=BaseUserRole.FINALIST,
          person_id__in=eit_alums,
      ).values_list("person", flat=True).distinct()
  
  
  def _get_eit_startups_in_other_programs(
          startup_status_model, exclude_programs, eit_finalist_startups):
      finalist_filter = Q(
          program_startup_status__startup_role__name=BaseStartupRole.FINALIST
      )
      program_family_filter = Q(
          program_startup_status__program__program_family__in=exclude_programs
      )
      return startup_status_model.objects.filter(
          program_family_filter,
          finalist_filter,
          startup_id__in=eit_finalist_startups,
          ).values_list('startup', flat=True)
  
subject: remove eit from global alumni program