from django.conf import settings


class AppNameDatabaseRouter(object):
    """
    Per application database. Use the database named like the app or fall
    back to default.
    """
    def db_for_read(self, model, **hints):
        if model._meta.app_label in settings.DATABASES:
            return model._meta.app_label
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in settings.DATABASES:
            return model._meta.app_label
        return None

    def allow_relation(self, obj1, obj2, **hints):
        return obj1._meta.app_label == obj2._meta.app_label