# Path: shared_schema_tenants/models.py
# 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']:
#         from django.contrib.postgres.fields import JSONField
#         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],
#                                      default=json.dumps(get_setting('DEFAULT_TENANT_SETTINGS')))
# 
#         @property
#         def extra_data(self):
#             import json
#             return json.loads(self._extra_data)
# 
#         @extra_data.setter
#         def extra_data(self, value):
#             import json
#             self._extra_data = json.dumps(value)
# 
#         @property
#         def settings(self):
#             import json
#             return json.loads(self._settings)
# 
#         @settings.setter
#         def settings(self, value):
#             import json
#             self._settings = json.dumps(value)
# 
#     def __str__(self):
#         return self.name
# 
# class TenantSite(TimeStampedModel, SingleTenantModelMixin):
#     tenant = models.ForeignKey('Tenant', related_name="tenant_sites")
#     site = models.OneToOneField(Site, related_name="tenant_site")
# 
#     def __str__(self):
#         return '%s - %s' % (self.tenant.name, self.site.domain)
# 
# class TenantRelationship(TimeStampedModel, SingleTenantModelMixin):
#     tenant = models.ForeignKey('Tenant', related_name="relationships")
#     user = models.ForeignKey(django_settings.AUTH_USER_MODEL, related_name="relationships")
#     groups = models.ManyToManyField('auth.Group',
#                                     related_name="user_tenant_groups",
#                                     blank=True)
#     permissions = models.ManyToManyField('auth.Permission',
#                                          related_name="user_tenant_permissions",
#                                          blank=True)
# 
#     def __str__(self):
#         groups_str = ', '.join([g.name for g in self.groups.all()])
#         return '%s - %s (%s)' % (str(self.user), str(self.tenant), groups_str)
# 
#     class Meta:
#         unique_together = [('user', 'tenant')]
# 
# Path: shared_schema_tenants/helpers/tenant_extra_data.py
# class TenantExtraDataHelper(TenantJSONFieldHelper):
# 
#     def __init__(self, instance=None):
#         super(TenantExtraDataHelper, self).__init__(
#             instance_field_name='extra_data', instance=instance,
#             tenant_fields=get_setting('DEFAULT_TENANT_EXTRA_DATA_FIELDS'),
#             tenant_default_fields_values=get_setting('DEFAULT_TENANT_EXTRA_DATA'))
# 
# Path: shared_schema_tenants/helpers/tenant_settings.py
# class TenantSettingsHelper(TenantJSONFieldHelper):
# 
#     def __init__(self, instance=None):
#         super(TenantSettingsHelper, self).__init__(
#             instance_field_name='settings', instance=instance,
#             tenant_fields=get_setting('DEFAULT_TENANT_SETTINGS_FIELDS'),
#             tenant_default_fields_values=get_setting('DEFAULT_TENANT_SETTINGS'))


from django import forms
from django.contrib import admin
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
from django.utils.text import ugettext_lazy as _
from shared_schema_tenants.models import Tenant, TenantSite, TenantRelationship
from shared_schema_tenants.helpers.tenant_extra_data import TenantExtraDataHelper
from shared_schema_tenants.helpers.tenant_settings import TenantSettingsHelper
import json
            raise ValidationError(json.dumps(e.message_dict))
        else:
            return validated_extra_data

    def clean__settings(self):
        settings = self.cleaned_data.get('settings', {})
        settings_helper = TenantSettingsHelper()
        try:
            validated_settings = settings_helper.validate_fields(
                {}, settings, partial=False)
        except ValidationError as e:
            raise ValidationError(json.dumps(e.message_dict))
        else:
            return validated_settings


class TenantSiteInLine(admin.StackedInline):
    model = TenantSite
    form = TenantSiteForm
    extra = 0
    min_num = 1


class TenantAdmin(admin.ModelAdmin):
    model = Tenant
    form = TenantForm
    inlines = [TenantSiteInLine]
    prepopulated_fields = {"slug": ("name",)}



# Path: shared_schema_tenants/models.py
# 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']:
#         from django.contrib.postgres.fields import JSONField
#         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],
#                                      default=json.dumps(get_setting('DEFAULT_TENANT_SETTINGS')))
# 
#         @property
#         def extra_data(self):
#             import json
#             return json.loads(self._extra_data)
# 
#         @extra_data.setter
#         def extra_data(self, value):
#             import json
#             self._extra_data = json.dumps(value)
# 
#         @property
#         def settings(self):
#             import json
#             return json.loads(self._settings)
# 
#         @settings.setter
#         def settings(self, value):
#             import json
#             self._settings = json.dumps(value)
# 
#     def __str__(self):
#         return self.name
# 
# class TenantSite(TimeStampedModel, SingleTenantModelMixin):
#     tenant = models.ForeignKey('Tenant', related_name="tenant_sites")
#     site = models.OneToOneField(Site, related_name="tenant_site")
# 
#     def __str__(self):
#         return '%s - %s' % (self.tenant.name, self.site.domain)
# 
# class TenantRelationship(TimeStampedModel, SingleTenantModelMixin):
#     tenant = models.ForeignKey('Tenant', related_name="relationships")
#     user = models.ForeignKey(django_settings.AUTH_USER_MODEL, related_name="relationships")
#     groups = models.ManyToManyField('auth.Group',
#                                     related_name="user_tenant_groups",
#                                     blank=True)
#     permissions = models.ManyToManyField('auth.Permission',
#                                          related_name="user_tenant_permissions",
#                                          blank=True)
# 
#     def __str__(self):
#         groups_str = ', '.join([g.name for g in self.groups.all()])
#         return '%s - %s (%s)' % (str(self.user), str(self.tenant), groups_str)
# 
#     class Meta:
#         unique_together = [('user', 'tenant')]
# 
# Path: shared_schema_tenants/helpers/tenant_extra_data.py
# class TenantExtraDataHelper(TenantJSONFieldHelper):
# 
#     def __init__(self, instance=None):
#         super(TenantExtraDataHelper, self).__init__(
#             instance_field_name='extra_data', instance=instance,
#             tenant_fields=get_setting('DEFAULT_TENANT_EXTRA_DATA_FIELDS'),
#             tenant_default_fields_values=get_setting('DEFAULT_TENANT_EXTRA_DATA'))
# 
# Path: shared_schema_tenants/helpers/tenant_settings.py
# class TenantSettingsHelper(TenantJSONFieldHelper):
# 
#     def __init__(self, instance=None):
#         super(TenantSettingsHelper, self).__init__(
#             instance_field_name='settings', instance=instance,
#             tenant_fields=get_setting('DEFAULT_TENANT_SETTINGS_FIELDS'),
#             tenant_default_fields_values=get_setting('DEFAULT_TENANT_SETTINGS'))


# Path: shared_schema_tenants/admin.py
from django import forms
from django.contrib import admin
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
from django.utils.text import ugettext_lazy as _
from shared_schema_tenants.models import Tenant, TenantSite, TenantRelationship
from shared_schema_tenants.helpers.tenant_extra_data import TenantExtraDataHelper
from shared_schema_tenants.helpers.tenant_settings import TenantSettingsHelper
import json

            raise ValidationError(json.dumps(e.message_dict))
        else:
            return validated_extra_data

    def clean__settings(self):
        settings = self.cleaned_data.get('settings', {})
        settings_helper = TenantSettingsHelper()
        try:
            validated_settings = settings_helper.validate_fields(
                {}, settings, partial=False)
        except ValidationError as e:
            raise ValidationError(json.dumps(e.message_dict))
        else:
            return validated_settings


class TenantSiteInLine(admin.StackedInline):
    model = TenantSite
    form = TenantSiteForm
    extra = 0
    min_num = 1


class TenantAdmin(admin.ModelAdmin):
    model = Tenant
    form = TenantForm
    inlines = [TenantSiteInLine]
    prepopulated_fields = {"slug": ("name",)}



admin.site.register(Tenant, TenantAdmin)