def add_syncable_models():
import django.apps<EOL>from morango.models import SyncableModel<EOL>from morango.manager import SyncableModelManager<EOL>from morango.query import SyncableModelQuerySet<EOL>model_list = []<EOL>for model_class in django.apps.apps.get_models():<EOL><INDENT>if issubclass(model_class, SyncableModel):<EOL><INDENT>name = model_class.__name__<EOL>if _multiple_self_ref_fk_check(model_class):<EOL><INDENT>raise InvalidMorangoModelConfiguration("<STR_LIT>")<EOL><DEDENT>try:<EOL><INDENT>from mptt import models<EOL>from morango.utils.morango_mptt import MorangoMPTTModel, MorangoMPTTTreeManager, MorangoTreeQuerySet<EOL>if issubclass(model_class, models.MPTTModel):<EOL><INDENT>if not issubclass(model_class, MorangoMPTTModel):<EOL><INDENT>raise InvalidMorangoModelConfiguration("<STR_LIT>".format(name))<EOL><DEDENT>if not isinstance(model_class.objects, MorangoMPTTTreeManager):<EOL><INDENT>raise InvalidMPTTManager("<STR_LIT>".format(name))<EOL><DEDENT>if not isinstance(model_class.objects.none(), MorangoTreeQuerySet):<EOL><INDENT>raise InvalidMPTTQuerySet("<STR_LIT>".format(name))<EOL><DEDENT><DEDENT><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>if not isinstance(model_class.objects, SyncableModelManager):<EOL><INDENT>raise InvalidSyncableManager("<STR_LIT>".format(name))<EOL><DEDENT>if not isinstance(model_class.objects.none(), SyncableModelQuerySet):<EOL><INDENT>raise InvalidSyncableQueryset("<STR_LIT>".format(name))<EOL><DEDENT>if model_class._meta.many_to_many:<EOL><INDENT>raise UnsupportedFieldType("<STR_LIT>")<EOL><DEDENT>if not hasattr(model_class, '<STR_LIT>'):<EOL><INDENT>raise InvalidMorangoModelConfiguration("<STR_LIT>".format(name))<EOL><DEDENT>if not hasattr(model_class, '<STR_LIT>'):<EOL><INDENT>raise InvalidMorangoModelConfiguration("<STR_LIT>".format(name))<EOL><DEDENT>profile = model_class.morango_profile<EOL>_profile_models[profile] = _profile_models.get(profile, [])<EOL>if model_class.morango_model_name is not None:<EOL><INDENT>_insert_model_into_profile_dict(model_class, profile)<EOL><DEDENT><DEDENT><DEDENT>for profile, model_list in iteritems(_profile_models):<EOL><INDENT>syncable_models_dict = OrderedDict()<EOL>for model_class in model_list:<EOL><INDENT>syncable_models_dict[model_class.morango_model_name] = model_class<EOL><DEDENT>_profile_models[profile] = syncable_models_dict<EOL><DEDENT>
Per profile, adds each model to a dictionary mapping the morango model name to its model class.
We sort by ForeignKey dependencies to safely sync data.