# 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
    def extra_forms(self, object_id):

        instance = self.model.objects.get(pk=object_id)
                
        extra_formsets = []

        for child in self.children:
            factory = modelformset_factory(child, extra=0, fields=['id'], formset=VisiblePrimaryKeyFormset)
            descriptor_name = self._descriptor_cache[child]
            descriptor = getattr(instance, descriptor_name)
            
            # create formset
            form = factory(queryset=descriptor.all())

            # this will find the name of the property in the model
            # the descriptor's inverse references
            try:
                field_name = descriptor.core_filters.keys().pop().split('__')[0]
            except Exception:
                field_name = instance._meta.object_name.lower()
            
            # find url for the + button
            url_descriptor = (self.admin_site.name, child._meta.app_label, child._meta.object_name.lower())
            url_pattern = '%s:%s_%s_add' % url_descriptor
            url = urlresolvers.reverse(url_pattern)
                
            #add properties to the formset            
            form.parent = instance            
            form.name = child.__name__.lower()
            form.title = child._meta.verbose_name_plural            

# 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

    def extra_forms(self, object_id):

        instance = self.model.objects.get(pk=object_id)
                
        extra_formsets = []

        for child in self.children:
            factory = modelformset_factory(child, extra=0, fields=['id'], formset=VisiblePrimaryKeyFormset)
            descriptor_name = self._descriptor_cache[child]
            descriptor = getattr(instance, descriptor_name)
            
            # create formset
            form = factory(queryset=descriptor.all())

            # this will find the name of the property in the model
            # the descriptor's inverse references
            try:
                field_name = descriptor.core_filters.keys().pop().split('__')[0]
            except Exception:
                field_name = instance._meta.object_name.lower()
            
            # find url for the + button
            url_descriptor = (self.admin_site.name, child._meta.app_label, child._meta.object_name.lower())
            url_pattern = '%s:%s_%s_add' % url_descriptor
            url = urlresolvers.reverse(url_pattern)
                
            #add properties to the formset            
            form.parent = instance            
            form.name = child.__name__.lower()
            form.title = child._meta.verbose_name_plural            

            form.addurl = "%s?%s=%s" % (strip_language_code(url), field_name, object_id)