# Path: easymode/tree/admin/relation.py
# class _CanFindParentLink(object):
#     """Adds function to find a link to a parent model"""
#         
#     def _get_parent_link(self, object_id):
#         parent_link_data = {}
#         if hasattr(self, 'parent_link'):
#             parent_link = getattr(self.model, self.parent_link)
#             instance = self.model.objects.get(pk=object_id)
# 
#             parent = parent_link.__get__(instance)
#             parent_type_name = parent._meta.object_name.lower()
#             parent_name = parent
#             parent_id = str(parent_link.field.value_from_object(instance))
# 
#             info = (self.admin_site.name, parent._meta.app_label, parent_type_name)
#             
#             parent_link_data['parent_model'] = strip_language_code(urlresolvers.reverse("%s:%s_%s_change" % info, args=[parent_id]))
#             parent_link_data['parent_name'] = "%s %s" % (force_unicode(parent._meta.verbose_name), parent_name)
#         
#         return parent_link_data


from django.contrib import admin
from django.http import HttpResponseRedirect
from easymode.tree.admin.forms import RecursiveInlineFormSet
from easymode.tree.admin.relation import _CanFindParentLink


__all__ = ('LinkInline', 'LinkedItemAdmin')

class LinkInline(admin.StackedInline):
    """
    A base class for an admin inline that will render as links to the change_view.
    This means you have to also register the model you are inlining with a non-inline
    ModelAdmin::
        
        class BottomAdmin(InvisibleModelAdmin):
            parent_link = 'top'
        
        class BottomLinkInline(LinkInline):
            fields = ('top',)
            model = BottomModel
    
    .. attribute:: fields
        
        It is mandatory to set the fields or fieldsets attribute of a class extending
        ``LinkInline``. If you don't, all the fields will be show, instead of only
        the change or add button. Ofcourse it can't hurt to add some ``read_only_fields``
        to add some context.
    """
    formset = RecursiveInlineFormSet
    template = 'tree/admin/edit_inline/link_inline.html'
    extra = 1


# Path: easymode/tree/admin/relation.py
# class _CanFindParentLink(object):
#     """Adds function to find a link to a parent model"""
#         
#     def _get_parent_link(self, object_id):
#         parent_link_data = {}
#         if hasattr(self, 'parent_link'):
#             parent_link = getattr(self.model, self.parent_link)
#             instance = self.model.objects.get(pk=object_id)
# 
#             parent = parent_link.__get__(instance)
#             parent_type_name = parent._meta.object_name.lower()
#             parent_name = parent
#             parent_id = str(parent_link.field.value_from_object(instance))
# 
#             info = (self.admin_site.name, parent._meta.app_label, parent_type_name)
#             
#             parent_link_data['parent_model'] = strip_language_code(urlresolvers.reverse("%s:%s_%s_change" % info, args=[parent_id]))
#             parent_link_data['parent_name'] = "%s %s" % (force_unicode(parent._meta.verbose_name), parent_name)
#         
#         return parent_link_data


# Path: easymode/tree/admin/abstract.py
from django.contrib import admin
from django.http import HttpResponseRedirect
from easymode.tree.admin.forms import RecursiveInlineFormSet
from easymode.tree.admin.relation import _CanFindParentLink



__all__ = ('LinkInline', 'LinkedItemAdmin')

class LinkInline(admin.StackedInline):
    """
    A base class for an admin inline that will render as links to the change_view.
    This means you have to also register the model you are inlining with a non-inline
    ModelAdmin::
        
        class BottomAdmin(InvisibleModelAdmin):
            parent_link = 'top'
        
        class BottomLinkInline(LinkInline):
            fields = ('top',)
            model = BottomModel
    
    .. attribute:: fields
        
        It is mandatory to set the fields or fieldsets attribute of a class extending
        ``LinkInline``. If you don't, all the fields will be show, instead of only
        the change or add button. Ofcourse it can't hurt to add some ``read_only_fields``
        to add some context.
    """
    formset = RecursiveInlineFormSet
    template = 'tree/admin/edit_inline/link_inline.html'
    extra = 1


class LinkedItemAdmin(admin.ModelAdmin, _CanFindParentLink):