# Path: shared_schema_tenants/settings.py
# def get_setting(settings_name):
#     tenant_settings = getattr(settings, 'SHARED_SCHEMA_TENANTS', {})
#     DEFAULT_TENANT_SETTINGS_FIELDS = tenant_settings.get(
#         'DEFAULT_TENANT_SETTINGS_FIELDS', {})
#     DEFAULT_TENANT_EXTRA_DATA_FIELDS = tenant_settings.get(
#         'DEFAULT_TENANT_EXTRA_DATA_FIELDS', {})
# 
#     settings_dict = {
#         "TENANT_SERIALIZER": (tenant_settings
#                               .get('SERIALIZERS', {})
#                               .get('TENANT_SERIALIZER',
#                                    'shared_schema_tenants.serializers.TenantSerializer')),
#         "TENANT_SITE_SERIALIZER": (tenant_settings
#                                    .get('SERIALIZERS', {})
#                                    .get('TENANT_SITE_SERIALIZER',
#                                         'shared_schema_tenants.serializers.TenantSiteSerializer')),
#         "TENANT_SETTINGS_SERIALIZER": (tenant_settings
#                                        .get('SERIALIZERS', {})
#                                        .get('TENANT_SETTINGS_SERIALIZER',
#                                             'shared_schema_tenants.serializers.TenantSettingsSerializer')),
#         "TENANT_RELATIONSHIP_SERIALIZER": (
#             tenant_settings
#             .get('SERIALIZERS', {})
#             .get('TENANT_SITE_SERIALIZER',
#                  'shared_schema_tenants.serializers.TenantSiteSerializer')),
#         "DEFAULT_TENANT_SETTINGS_FIELDS": DEFAULT_TENANT_SETTINGS_FIELDS,
#         "DEFAULT_TENANT_SETTINGS": {key: value.get('default')
#                                     for key, value
#                                     in DEFAULT_TENANT_SETTINGS_FIELDS.items()},
#         "DEFAULT_TENANT_EXTRA_DATA_FIELDS": DEFAULT_TENANT_EXTRA_DATA_FIELDS,
#         "DEFAULT_TENANT_EXTRA_DATA": {key: value.get('default')
#                                       for key, value
#                                       in DEFAULT_TENANT_EXTRA_DATA_FIELDS.items()},
#         "DEFAULT_SITE_DOMAIN": tenant_settings.get(
#             'DEFAULT_SITE_DOMAIN',
#             'localhost'
#         ),
#         "DEFAULT_TENANT_SLUG": tenant_settings.get(
#             'DEFAULT_TENANT_SLUG',
#             'default'
#         ),
#         "TENANT_RETRIEVERS": tenant_settings.get('TENANT_RETRIEVERS', [
#             'shared_schema_tenants.tenant_retrievers.retrieve_by_domain',
#             'shared_schema_tenants.tenant_retrievers.retrieve_by_http_header',
#             'shared_schema_tenants.tenant_retrievers.retrieve_by_session',
#         ]),
#         "ADD_TENANT_TO_SESSION": tenant_settings.get('ADD_TENANT_TO_SESSION', True),
#         "TENANT_HTTP_HEADER": tenant_settings.get('TENANT_HTTP_HEADER', 'Tenant-Slug'),
#         "DEFAULT_TENANT_OWNER_PERMISSIONS": tenant_settings.get(
#             'DEFAULT_TENANT_OWNER_PERMISSIONS', [
#                 'shared_schema_tenants.add_tenant',
#                 'shared_schema_tenants.change_tenant',
#                 'shared_schema_tenants.delete_tenant',
#                 'shared_schema_tenants.add_tenantsite',
#                 'shared_schema_tenants.change_tenantsite',
#                 'shared_schema_tenants.delete_tenantsite',
#                 'shared_schema_tenants.add_tenantrelationship',
#                 'shared_schema_tenants.delete_tenantrelationship',
#                 'shared_schema_tenants.change_tenantrelationship',
#             ])
#     }
# 
#     return settings_dict.get(settings_name)


from django.contrib.sites.models import Site
from django.contrib.auth.models import Group, Permission
from django.db import transaction
from shared_schema_tenants.settings import get_setting
    from shared_schema_tenants.middleware import TenantMiddleware
    from shared_schema_tenants.middleware import TenantMiddleware
    from shared_schema_tenants.middleware import TenantMiddleware
    from shared_schema_tenants.models import Tenant
    from shared_schema_tenants.models import Tenant  # noqa
    from shared_schema_tenants.helpers import TenantExtraDataHelper



def get_current_tenant():
    return TenantMiddleware.get_current_tenant()


def set_current_tenant(tenant_slug):
    return TenantMiddleware.set_tenant(tenant_slug)


def clear_current_tenant():
    return TenantMiddleware.clear_tenant()


def create_tenant(name, slug, extra_data, domains=[], user=None):
    with transaction.atomic():
        tenant = Tenant.objects.create(
            name=name, slug=slug, extra_data=extra_data)

        if len(domains) > 0:
            for domain in domains:
                site = Site.objects.create(name=name, domain=domain)
                tenant.tenant_sites.create(site=site)

        if user:
            rel = tenant.relationships.create(user=user)

# Path: shared_schema_tenants/settings.py
# def get_setting(settings_name):
#     tenant_settings = getattr(settings, 'SHARED_SCHEMA_TENANTS', {})
#     DEFAULT_TENANT_SETTINGS_FIELDS = tenant_settings.get(
#         'DEFAULT_TENANT_SETTINGS_FIELDS', {})
#     DEFAULT_TENANT_EXTRA_DATA_FIELDS = tenant_settings.get(
#         'DEFAULT_TENANT_EXTRA_DATA_FIELDS', {})
# 
#     settings_dict = {
#         "TENANT_SERIALIZER": (tenant_settings
#                               .get('SERIALIZERS', {})
#                               .get('TENANT_SERIALIZER',
#                                    'shared_schema_tenants.serializers.TenantSerializer')),
#         "TENANT_SITE_SERIALIZER": (tenant_settings
#                                    .get('SERIALIZERS', {})
#                                    .get('TENANT_SITE_SERIALIZER',
#                                         'shared_schema_tenants.serializers.TenantSiteSerializer')),
#         "TENANT_SETTINGS_SERIALIZER": (tenant_settings
#                                        .get('SERIALIZERS', {})
#                                        .get('TENANT_SETTINGS_SERIALIZER',
#                                             'shared_schema_tenants.serializers.TenantSettingsSerializer')),
#         "TENANT_RELATIONSHIP_SERIALIZER": (
#             tenant_settings
#             .get('SERIALIZERS', {})
#             .get('TENANT_SITE_SERIALIZER',
#                  'shared_schema_tenants.serializers.TenantSiteSerializer')),
#         "DEFAULT_TENANT_SETTINGS_FIELDS": DEFAULT_TENANT_SETTINGS_FIELDS,
#         "DEFAULT_TENANT_SETTINGS": {key: value.get('default')
#                                     for key, value
#                                     in DEFAULT_TENANT_SETTINGS_FIELDS.items()},
#         "DEFAULT_TENANT_EXTRA_DATA_FIELDS": DEFAULT_TENANT_EXTRA_DATA_FIELDS,
#         "DEFAULT_TENANT_EXTRA_DATA": {key: value.get('default')
#                                       for key, value
#                                       in DEFAULT_TENANT_EXTRA_DATA_FIELDS.items()},
#         "DEFAULT_SITE_DOMAIN": tenant_settings.get(
#             'DEFAULT_SITE_DOMAIN',
#             'localhost'
#         ),
#         "DEFAULT_TENANT_SLUG": tenant_settings.get(
#             'DEFAULT_TENANT_SLUG',
#             'default'
#         ),
#         "TENANT_RETRIEVERS": tenant_settings.get('TENANT_RETRIEVERS', [
#             'shared_schema_tenants.tenant_retrievers.retrieve_by_domain',
#             'shared_schema_tenants.tenant_retrievers.retrieve_by_http_header',
#             'shared_schema_tenants.tenant_retrievers.retrieve_by_session',
#         ]),
#         "ADD_TENANT_TO_SESSION": tenant_settings.get('ADD_TENANT_TO_SESSION', True),
#         "TENANT_HTTP_HEADER": tenant_settings.get('TENANT_HTTP_HEADER', 'Tenant-Slug'),
#         "DEFAULT_TENANT_OWNER_PERMISSIONS": tenant_settings.get(
#             'DEFAULT_TENANT_OWNER_PERMISSIONS', [
#                 'shared_schema_tenants.add_tenant',
#                 'shared_schema_tenants.change_tenant',
#                 'shared_schema_tenants.delete_tenant',
#                 'shared_schema_tenants.add_tenantsite',
#                 'shared_schema_tenants.change_tenantsite',
#                 'shared_schema_tenants.delete_tenantsite',
#                 'shared_schema_tenants.add_tenantrelationship',
#                 'shared_schema_tenants.delete_tenantrelationship',
#                 'shared_schema_tenants.change_tenantrelationship',
#             ])
#     }
# 
#     return settings_dict.get(settings_name)


# Path: shared_schema_tenants/helpers/tenants.py
from django.contrib.sites.models import Site
from django.contrib.auth.models import Group, Permission
from django.db import transaction
from shared_schema_tenants.settings import get_setting
    from shared_schema_tenants.middleware import TenantMiddleware
    from shared_schema_tenants.middleware import TenantMiddleware
    from shared_schema_tenants.middleware import TenantMiddleware
    from shared_schema_tenants.models import Tenant
    from shared_schema_tenants.models import Tenant  # noqa
    from shared_schema_tenants.helpers import TenantExtraDataHelper




def get_current_tenant():
    return TenantMiddleware.get_current_tenant()


def set_current_tenant(tenant_slug):
    return TenantMiddleware.set_tenant(tenant_slug)


def clear_current_tenant():
    return TenantMiddleware.clear_tenant()


def create_tenant(name, slug, extra_data, domains=[], user=None):
    with transaction.atomic():
        tenant = Tenant.objects.create(
            name=name, slug=slug, extra_data=extra_data)

        if len(domains) > 0:
            for domain in domains:
                site = Site.objects.create(name=name, domain=domain)
                tenant.tenant_sites.create(site=site)

        if user:
            rel = tenant.relationships.create(user=user)

            rel.groups.add(create_default_tenant_groups()[0])