# Path: hstore_flattenfields/db/base.py
# class HStoreModel(models.Model):
#     _dfields = hstore.DictionaryField(db_index=True, null=True, blank=True)
# 
#     __metaclass__ = HStoreModelMeta
#     objects = FlattenFieldsFilterManager()
# 
#     class Meta:
#         abstract = True
# 
#     def __init__(self, *args, **kwargs):
#         _dfields = None
#         if args:
#             # XXX: hack in order to save _dfields without alter django
#             # save _dfields in args and restore
# 
#             # what the index of _dfields?
#             i = 0
#             index = None
#             for f in self._meta.local_fields:
#                 if f.name == "_dfields":
#                     index = i
#                     break
#                 i = i + 1
#             if index is not None and index < len(args):
#                 _dfields = args[index]
# 
#         super(HStoreModel, self).__init__(*args, **kwargs)
#         if _dfields: self._dfields = _dfields
# 
#     @property
#     def dynamic_fields(self):
#         return self._dynamic_fields
# 
#     @property
#     def content_panes(self):
#         return self._content_panes
# 
#     @property
#     def related_instances(self):
#         return []
# 
#     def fields_without_content_panes(self):
#         fields_w_cpanes = map(lambda x: x.name, 
#             filter(lambda f: f.content_pane, self.dynamic_fields)
#         )
#         def without_cpane(f): 
#             return f.name not in fields_w_cpanes
#         return filter(without_cpane, self._meta.fields)
# 
#     @property
#     def default_content_pane(self):
#         return {
#             'name': getattr(settings, 'DEFAULT_CONTENT_PANE_NAME', _('Main Information')),
#             'slug': 'default',
#             'pk': '',
#             'model': self.__class__,
#             'fields': self.fields_without_content_panes()
#         }


from django.db.models.signals import pre_init, post_init
from django.db.models import Q
from django.dispatch import receiver
from hstore_flattenfields.db.base import HStoreModel
from hstore_flattenfields.utils import *
            from hstore_flattenfields.models import (
                DynamicField, ContentPane
            )
       	from hstore_flattenfields.models import DynamicFieldGroup


@receiver(pre_init)
def fill_model_with_fields(sender, **kwargs):
    if issubclass(sender, HStoreModel):
        try:
            assert dynamic_field_table_exists()
        except (AssertionError, ImportError):
            content_panes = metafields = []
        else:
            metafields = DynamicField.objects.filter(
                refer=sender.__name__
            ).order_by('pk').select_related('group', 'content_pane')
            content_panes = ContentPane.objects.filter(
                content_type__model=sender.__name__.lower()
            )
        for dynamic_field in metafields:
            field = create_field_from_instance(dynamic_field)
            if not field.name in sender._meta.get_all_field_names():
                sender.add_to_class(field.name, field)
                # field.contribute_to_class(sender, field.name)
        setattr(sender, '_dynamic_fields', metafields)
        setattr(sender, '_content_panes', content_panes)
        
@receiver(post_init)
def setup_fields_in_instance(sender, instance, **kwargs):

# Path: hstore_flattenfields/db/base.py
# class HStoreModel(models.Model):
#     _dfields = hstore.DictionaryField(db_index=True, null=True, blank=True)
# 
#     __metaclass__ = HStoreModelMeta
#     objects = FlattenFieldsFilterManager()
# 
#     class Meta:
#         abstract = True
# 
#     def __init__(self, *args, **kwargs):
#         _dfields = None
#         if args:
#             # XXX: hack in order to save _dfields without alter django
#             # save _dfields in args and restore
# 
#             # what the index of _dfields?
#             i = 0
#             index = None
#             for f in self._meta.local_fields:
#                 if f.name == "_dfields":
#                     index = i
#                     break
#                 i = i + 1
#             if index is not None and index < len(args):
#                 _dfields = args[index]
# 
#         super(HStoreModel, self).__init__(*args, **kwargs)
#         if _dfields: self._dfields = _dfields
# 
#     @property
#     def dynamic_fields(self):
#         return self._dynamic_fields
# 
#     @property
#     def content_panes(self):
#         return self._content_panes
# 
#     @property
#     def related_instances(self):
#         return []
# 
#     def fields_without_content_panes(self):
#         fields_w_cpanes = map(lambda x: x.name, 
#             filter(lambda f: f.content_pane, self.dynamic_fields)
#         )
#         def without_cpane(f): 
#             return f.name not in fields_w_cpanes
#         return filter(without_cpane, self._meta.fields)
# 
#     @property
#     def default_content_pane(self):
#         return {
#             'name': getattr(settings, 'DEFAULT_CONTENT_PANE_NAME', _('Main Information')),
#             'slug': 'default',
#             'pk': '',
#             'model': self.__class__,
#             'fields': self.fields_without_content_panes()
#         }


# Path: hstore_flattenfields/signals.py
from django.db.models.signals import pre_init, post_init
from django.db.models import Q
from django.dispatch import receiver
from hstore_flattenfields.db.base import HStoreModel
from hstore_flattenfields.utils import *
            from hstore_flattenfields.models import (
                DynamicField, ContentPane
            )
       	from hstore_flattenfields.models import DynamicFieldGroup



@receiver(pre_init)
def fill_model_with_fields(sender, **kwargs):
    if issubclass(sender, HStoreModel):
        try:
            assert dynamic_field_table_exists()
        except (AssertionError, ImportError):
            content_panes = metafields = []
        else:
            metafields = DynamicField.objects.filter(
                refer=sender.__name__
            ).order_by('pk').select_related('group', 'content_pane')
            content_panes = ContentPane.objects.filter(
                content_type__model=sender.__name__.lower()
            )
        for dynamic_field in metafields:
            field = create_field_from_instance(dynamic_field)
            if not field.name in sender._meta.get_all_field_names():
                sender.add_to_class(field.name, field)
                # field.contribute_to_class(sender, field.name)
        setattr(sender, '_dynamic_fields', metafields)
        setattr(sender, '_content_panes', content_panes)
        
@receiver(post_init)
def setup_fields_in_instance(sender, instance, **kwargs):

    if issubclass(sender, HStoreModel):