# Path: easymode/utils/languagecode.py
# def strip_language_code(url):
#     """
#     Strip the language code from the beginning of string
# 
#     >>> strip_language_code('http://example.com/en/example.html')
#     'http://example.com/example.html'
#     
#     :param url: An url.
#     :rtype: The ``url`` but the language code is removed.
#     """
#     return STRIP_LANGUAGE_CODE_REGEX.sub('/',url, 1)


from django.http import HttpResponseRedirect
from django.forms.models import  modelformset_factory
from django.core import urlresolvers
from django.conf import settings
from django.utils.encoding import force_unicode
from easymode.tree.introspection import get_foreign_key_desciptors
from easymode.tree.admin.formsets import VisiblePrimaryKeyFormset
from easymode.utils.languagecode import strip_language_code
    from reversion.admin import VersionAdmin
    from django.contrib import admin
"""
Contains a modeladmin class that shows links to all related items in its change_view.

If you want to use :class:`InvisibleModelAdmin` make sure easymode comes before
``django.contrib.admin`` in the ``INSTALLED_APPS`` because it has to 
override admin/index.html to make it work.
"""


if 'reversion' in settings.INSTALLED_APPS:
    AdminBase = VersionAdmin
else:
    AdminBase = admin.ModelAdmin

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)
            

# Path: easymode/utils/languagecode.py
# def strip_language_code(url):
#     """
#     Strip the language code from the beginning of string
# 
#     >>> strip_language_code('http://example.com/en/example.html')
#     'http://example.com/example.html'
#     
#     :param url: An url.
#     :rtype: The ``url`` but the language code is removed.
#     """
#     return STRIP_LANGUAGE_CODE_REGEX.sub('/',url, 1)


# Path: easymode/tree/admin/relation.py
from django.http import HttpResponseRedirect
from django.forms.models import  modelformset_factory
from django.core import urlresolvers
from django.conf import settings
from django.utils.encoding import force_unicode
from easymode.tree.introspection import get_foreign_key_desciptors
from easymode.tree.admin.formsets import VisiblePrimaryKeyFormset
from easymode.utils.languagecode import strip_language_code
    from reversion.admin import VersionAdmin
    from django.contrib import admin

"""
Contains a modeladmin class that shows links to all related items in its change_view.

If you want to use :class:`InvisibleModelAdmin` make sure easymode comes before
``django.contrib.admin`` in the ``INSTALLED_APPS`` because it has to 
override admin/index.html to make it work.
"""


if 'reversion' in settings.INSTALLED_APPS:
    AdminBase = VersionAdmin
else:
    AdminBase = admin.ModelAdmin

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]))