# Path: hyperadmin/resources/models/resources.py
# class InlineModelResource(BaseModelResource):
#     model = None
#     fk_name = None
#     rel_name = None
#     
#     list_endpoint = (InlineListEndpoint, {})
#     create_endpoint = (InlineCreateEndpoint, {})
#     detail_endpoint = (InlineDetailEndpoint, {})
#     delete_endpoint = (InlineDeleteEndpoint, {})
#     
#     def __init__(self, parent, **kwargs):
#         kwargs['site'] = parent.site
#         kwargs['resource_adaptor'] = self.model
#         kwargs['parent'] = parent
#         super(InlineModelResource, self).__init__(**kwargs)
#     
#     def post_register(self):
#         from django.db.models.fields.related import RelatedObject
#         from django.forms.models import _get_foreign_key
#         self.fk = _get_foreign_key(self._parent.resource_adaptor, self.model, self.fk_name)
#         if self.rel_name is None:
#             #TODO invert this
#             self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
#         super(InlineModelResource, self).post_register()
#     
#     def get_queryset(self, parent):
#         queryset = self.resource_adaptor.objects.all()
#         queryset = queryset.filter(**{self.fk.name:parent})
#         if not self.has_update_permission():
#             queryset = queryset.none()
#         return queryset
#     
#     def get_primary_query(self, **kwargs):
#         return self.get_queryset(parent=self.state['parent'].instance)
#     
#     def get_indexes(self):
#         return {'primary':InlineIndex('primary', self)}
#     
#     def get_base_url_name_suffix(self):
#         assert self.rel_name is not None
#         return self.rel_name
#     
#     def get_item_url(self, item):
#         return self.link_prototypes['update'].get_url(item=item)
#     
#     def get_absolute_url(self):
#         return self.link_prototypes['list'].get_url()
#     
#     def get_breadcrumbs(self):
#         breadcrumbs = self.parent.get_breadcrumbs()
#         parent_item = self.state['parent']
#         breadcrumbs.append(self.parent.get_item_breadcrumb(parent_item))
#         breadcrumbs.append(self.get_breadcrumb())
#         if self.state.item:
#             breadcrumbs.append(self.get_item_breadcrumb(self.state.item))
#         return breadcrumbs
#     
#     def get_form_class(self):
#         if self.form_class:
#             return self.form_class
#         
#         resource = self
#         
#         class AdminForm(forms.ModelForm):
#             state = self.state
#             
#             def save(self, commit=True):
#                 instance = super(AdminForm, self).save(commit=False)
#                 setattr(instance, resource.fk.name, self.state['parent'].instance)
#                 if commit:
#                     instance.save()
#                 return instance
#             
#             class Meta:
#                 model = self.model
#                 exclude = self.get_exclude() + [self.fk.name]
#                 #TODO formfield overides
#                 #TODO fields
#         return AdminForm
#     
#     def get_ln_links(self):
#         links = self.create_link_collection()
#         if self.state.namespace:
#             for item in self.get_resource_items():
#                 links.append(self.link_prototypes['update'].get_link(item=item))
#         return links
#     
#     def get_idempotent_links(self):
#         links = self.create_link_collection()
#         if self.state.namespace:
#             for item in self.get_resource_items():
#                 links.append(self.link_prototypes['delete'].get_link(item=item))
#         return links


from django.contrib.contenttypes.models import ContentType
from django import forms
from hyperadmin.resources.models.resources import InlineModelResource



class GenericInlineModelResource(InlineModelResource):
    model = None
    ct_field = "content_type"
    ct_fk_field = "object_id"
    
    def post_register(self):
        self._ct_field = self.opts.get_field(self.ct_field)
        self._ct_fk_field = self.opts.get_field(self.ct_fk_field)
        if self.rel_name is None:
            self.rel_name = '-'.join((
                self.opts.app_label, self.opts.object_name.lower(),
                self.ct_field, self.ct_fk_field,
            ))

# Path: hyperadmin/resources/models/resources.py
# class InlineModelResource(BaseModelResource):
#     model = None
#     fk_name = None
#     rel_name = None
#     
#     list_endpoint = (InlineListEndpoint, {})
#     create_endpoint = (InlineCreateEndpoint, {})
#     detail_endpoint = (InlineDetailEndpoint, {})
#     delete_endpoint = (InlineDeleteEndpoint, {})
#     
#     def __init__(self, parent, **kwargs):
#         kwargs['site'] = parent.site
#         kwargs['resource_adaptor'] = self.model
#         kwargs['parent'] = parent
#         super(InlineModelResource, self).__init__(**kwargs)
#     
#     def post_register(self):
#         from django.db.models.fields.related import RelatedObject
#         from django.forms.models import _get_foreign_key
#         self.fk = _get_foreign_key(self._parent.resource_adaptor, self.model, self.fk_name)
#         if self.rel_name is None:
#             #TODO invert this
#             self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
#         super(InlineModelResource, self).post_register()
#     
#     def get_queryset(self, parent):
#         queryset = self.resource_adaptor.objects.all()
#         queryset = queryset.filter(**{self.fk.name:parent})
#         if not self.has_update_permission():
#             queryset = queryset.none()
#         return queryset
#     
#     def get_primary_query(self, **kwargs):
#         return self.get_queryset(parent=self.state['parent'].instance)
#     
#     def get_indexes(self):
#         return {'primary':InlineIndex('primary', self)}
#     
#     def get_base_url_name_suffix(self):
#         assert self.rel_name is not None
#         return self.rel_name
#     
#     def get_item_url(self, item):
#         return self.link_prototypes['update'].get_url(item=item)
#     
#     def get_absolute_url(self):
#         return self.link_prototypes['list'].get_url()
#     
#     def get_breadcrumbs(self):
#         breadcrumbs = self.parent.get_breadcrumbs()
#         parent_item = self.state['parent']
#         breadcrumbs.append(self.parent.get_item_breadcrumb(parent_item))
#         breadcrumbs.append(self.get_breadcrumb())
#         if self.state.item:
#             breadcrumbs.append(self.get_item_breadcrumb(self.state.item))
#         return breadcrumbs
#     
#     def get_form_class(self):
#         if self.form_class:
#             return self.form_class
#         
#         resource = self
#         
#         class AdminForm(forms.ModelForm):
#             state = self.state
#             
#             def save(self, commit=True):
#                 instance = super(AdminForm, self).save(commit=False)
#                 setattr(instance, resource.fk.name, self.state['parent'].instance)
#                 if commit:
#                     instance.save()
#                 return instance
#             
#             class Meta:
#                 model = self.model
#                 exclude = self.get_exclude() + [self.fk.name]
#                 #TODO formfield overides
#                 #TODO fields
#         return AdminForm
#     
#     def get_ln_links(self):
#         links = self.create_link_collection()
#         if self.state.namespace:
#             for item in self.get_resource_items():
#                 links.append(self.link_prototypes['update'].get_link(item=item))
#         return links
#     
#     def get_idempotent_links(self):
#         links = self.create_link_collection()
#         if self.state.namespace:
#             for item in self.get_resource_items():
#                 links.append(self.link_prototypes['delete'].get_link(item=item))
#         return links


# Path: hyperadmin/resources/models/generic.py
from django.contrib.contenttypes.models import ContentType
from django import forms
from hyperadmin.resources.models.resources import InlineModelResource




class GenericInlineModelResource(InlineModelResource):
    model = None
    ct_field = "content_type"
    ct_fk_field = "object_id"
    
    def post_register(self):
        self._ct_field = self.opts.get_field(self.ct_field)
        self._ct_fk_field = self.opts.get_field(self.ct_fk_field)
        if self.rel_name is None:
            self.rel_name = '-'.join((
                self.opts.app_label, self.opts.object_name.lower(),
                self.ct_field, self.ct_fk_field,
            ))

        super(InlineModelResource, self).post_register()