def relink_or_drop_orphan_translationprojects(apps, schema_editor):
 Project = apps.get_model(u'pootle_project.Project')
 TP = apps.get_model(u'pootle_translationproject.TranslationProject')
 for proj_key in set(TP.objects.values_list(u'project_id', flat=True)):
  if (not Project.objects.filter(pk=proj_key).exists()):
   for tp in TP.objects.filter(project_id=proj_key):
    proj_code = tp.pootle_path.split(u'/')[2]
    projects = Project.objects.filter(code=proj_code)
    if projects.exists():
     tp.project = projects.first()
     tp.save()
    else:
     tp.delete()

def relink_or_drop_orphan_translationprojects ( apps , schema_editor ) : Project = apps . get_model ( u'pootle_project.Project' ) TP = apps . get_model ( u'pootle_translationproject.TranslationProject' ) for proj_key in set ( TP . objects . values_list ( u'project_id' , flat = True ) ) : if ( not Project . objects . filter ( pk = proj_key ) . exists ( ) ) : for tp in TP . objects . filter ( project_id = proj_key ) : proj_code = tp . pootle_path . split ( u'/' ) [ 2 ] projects = Project . objects . filter ( code = proj_code ) if projects . exists ( ) : tp . project = projects . first ( ) tp . save ( ) else : tp . delete ( )
relink or drop tps with no project .