def drop_stray_directories(apps, schema_editor):
 Directory = apps.get_model(u'pootle_app.Directory')
 TP = apps.get_model(u'pootle_translationproject.TranslationProject')
 Language = apps.get_model(u'pootle_language.Language')
 dirs = Directory.objects.exclude(pootle_path=u'/').exclude(pootle_path__startswith=u'/goals/').exclude(pootle_path__startswith=u'/projects/')
 language_codes = Language.objects.values_list(u'code', flat=True)
 for directory in dirs:
  directory_path_parts = directory.pootle_path.split(u'/')
  if (len(directory_path_parts) < 4):
   continue
  directory_lang_code = directory_path_parts[1]
  if (directory_lang_code in language_codes):
   tp_path = u'/'.join((directory_path_parts[:3] + [u'']))
   has_tp = TP.objects.filter(pootle_path=tp_path).exists()
   if (not has_tp):
    Directory.objects.filter(pootle_path__startswith=directory.pootle_path).delete()

def drop_stray_directories ( apps , schema_editor ) : Directory = apps . get_model ( u'pootle_app.Directory' ) TP = apps . get_model ( u'pootle_translationproject.TranslationProject' ) Language = apps . get_model ( u'pootle_language.Language' ) dirs = Directory . objects . exclude ( pootle_path = u'/' ) . exclude ( pootle_path__startswith = u'/goals/' ) . exclude ( pootle_path__startswith = u'/projects/' ) language_codes = Language . objects . values_list ( u'code' , flat = True ) for directory in dirs : directory_path_parts = directory . pootle_path . split ( u'/' ) if ( len ( directory_path_parts ) < 4 ) : continue directory_lang_code = directory_path_parts [ 1 ] if ( directory_lang_code in language_codes ) : tp_path = u'/' . join ( ( directory_path_parts [ : 3 ] + [ u'' ] ) ) has_tp = TP . objects . filter ( pootle_path = tp_path ) . exists ( ) if ( not has_tp ) : Directory . objects . filter ( pootle_path__startswith = directory . pootle_path ) . delete ( )
drop directories that belong to no existing tp .