# Path: shared_schema_tenants/mixins.py
# class SingleTenantModelMixin(models.Model):
#     tenant = models.ForeignKey(
#         'shared_schema_tenants.Tenant', default=get_default_tenant)
# 
#     objects = SingleTenantModelManager()
# 
#     original_manager = models.Manager()
#     tenant_objects = SingleTenantModelManager()
# 
#     class Meta:
#         abstract = True
#         default_manager_name = 'objects'
#         base_manager_name = 'objects'
# 
#     def save(self, *args, **kwargs):
#         if not hasattr(self, 'tenant'):
#             self.tenant = get_current_tenant()
# 
#         if getattr(self, 'tenant', False):
#             return super(SingleTenantModelMixin, self).save(*args, **kwargs)
#         else:
#             raise TenantNotFoundError()
# 
# 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/validators.py
# def validate_json(value):
#     try:
#         value = json.loads(value)
#     except Exception:
#         raise ValidationError(_("This field must be a valid json"))
# 
#     return value


from django.db import models
from django.conf import settings as django_settings
from django.contrib.sites.models import Site
from django.db.models.signals import post_delete
from model_utils.models import TimeStampedModel
from shared_schema_tenants.mixins import SingleTenantModelMixin
from shared_schema_tenants.settings import get_setting
from shared_schema_tenants.validators import validate_json
        from django.contrib.postgres.fields import JSONField
import json
            import json
            import json
            import json
            import json





class Tenant(TimeStampedModel):
    name = models.CharField(max_length=255)
    slug = models.CharField(max_length=255, primary_key=True)

    if 'postgresql' in django_settings.DATABASES['default']['ENGINE']:
        extra_data = JSONField(blank=True, null=True,
                               default=get_setting('DEFAULT_TENANT_EXTRA_DATA'))
        settings = JSONField(blank=True, null=True,
                             default=get_setting('DEFAULT_TENANT_SETTINGS'))
    else:
        _extra_data = models.TextField(blank=True, null=True,
                                       validators=[validate_json],
                                       default=json.dumps(get_setting('DEFAULT_TENANT_EXTRA_DATA')))
        _settings = models.TextField(blank=True, null=True,

# Path: shared_schema_tenants/mixins.py
# class SingleTenantModelMixin(models.Model):
#     tenant = models.ForeignKey(
#         'shared_schema_tenants.Tenant', default=get_default_tenant)
# 
#     objects = SingleTenantModelManager()
# 
#     original_manager = models.Manager()
#     tenant_objects = SingleTenantModelManager()
# 
#     class Meta:
#         abstract = True
#         default_manager_name = 'objects'
#         base_manager_name = 'objects'
# 
#     def save(self, *args, **kwargs):
#         if not hasattr(self, 'tenant'):
#             self.tenant = get_current_tenant()
# 
#         if getattr(self, 'tenant', False):
#             return super(SingleTenantModelMixin, self).save(*args, **kwargs)
#         else:
#             raise TenantNotFoundError()
# 
# 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/validators.py
# def validate_json(value):
#     try:
#         value = json.loads(value)
#     except Exception:
#         raise ValidationError(_("This field must be a valid json"))
# 
#     return value


# Path: shared_schema_tenants/models.py
from django.db import models
from django.conf import settings as django_settings
from django.contrib.sites.models import Site
from django.db.models.signals import post_delete
from model_utils.models import TimeStampedModel
from shared_schema_tenants.mixins import SingleTenantModelMixin
from shared_schema_tenants.settings import get_setting
from shared_schema_tenants.validators import validate_json
        from django.contrib.postgres.fields import JSONField
import json
            import json
            import json
            import json
            import json






class Tenant(TimeStampedModel):
    name = models.CharField(max_length=255)
    slug = models.CharField(max_length=255, primary_key=True)

    if 'postgresql' in django_settings.DATABASES['default']['ENGINE']:
        extra_data = JSONField(blank=True, null=True,
                               default=get_setting('DEFAULT_TENANT_EXTRA_DATA'))
        settings = JSONField(blank=True, null=True,
                             default=get_setting('DEFAULT_TENANT_SETTINGS'))
    else:
        _extra_data = models.TextField(blank=True, null=True,
                                       validators=[validate_json],
                                       default=json.dumps(get_setting('DEFAULT_TENANT_EXTRA_DATA')))
        _settings = models.TextField(blank=True, null=True,

                                     validators=[validate_json],