from __future__ import unicode_literals

from django.conf.urls import patterns, include, url
from django.contrib.admin.sites import AdminSite

from mezzanine.utils.importing import import_dotted_path


class LazyAdminSite(AdminSite):
    """
    Defers calls to register/unregister until autodiscover is called
    to avoid load issues with injectable model fields defined by
    ``settings.EXTRA_MODEL_FIELDS``.
    """

    def __init__(self, *args, **kwargs):
        self._deferred = []
        super(LazyAdminSite, self).__init__(*args, **kwargs)

    def register(self, *args, **kwargs):
        for name, deferred_args, deferred_kwargs in self._deferred:
            if name == "unregister" and deferred_args[0] == args[0]:
                self._deferred.append(("register", args, kwargs))
                break
        else:
            super(LazyAdminSite, self).register(*args, **kwargs)

    def unregister(self, *args, **kwargs):
        self._deferred.append(("unregister", args, kwargs))

    def lazy_registration(self):
        for name, deferred_args, deferred_kwargs in self._deferred:
            getattr(AdminSite, name)(self, *deferred_args, **deferred_kwargs)

    @property
    def urls(self):
        from django.conf import settings
        urls = patterns("", ("", super(LazyAdminSite, self).urls),)
        # Filebrowser admin media library.
        fb_name = getattr(settings, "PACKAGE_NAME_FILEBROWSER", "")
        if fb_name in settings.INSTALLED_APPS:
            try:
                fb_urls = import_dotted_path("%s.sites.site" % fb_name).urls
            except ImportError:
                fb_urls = "%s.urls" % fb_name
            urls = patterns("",
                ("^media-library/", include(fb_urls)),
                url("^auth/user/(\d+)/password/$",
                    lambda request, user_id: self.password_change(request),
                    name="password_change"),
            ) + urls
        return urls

@@ -1680,32 +1680,16 @@
 erns(%22%22,
-%0A               
  (%22%5Emedi
@@ -1723,201 +1723,613 @@
 s)),
-%0A                url(%22%5Eauth/user/(%5Cd+)/password/$%22,%0A                    lambda request, user_id: self.password_change(request),%0A                    name=%22password_change%22),%0A            ) + urls
+) + urls%0A        # Give the urlpatterm for the user password change view an%0A        # actual name, so that it can be reversed with multiple%0A        # languages are supported in the admin.%0A        for admin in self._registry.values():%0A            user_change_password = getattr(admin, %22user_change_password%22, None)%0A            if user_change_password:%0A                urls = patterns(%22%22,%0A                    url(%22%5Eauth/user/(%5Cd+)/password/$%22,%0A                        self.admin_view(user_change_password),%0A                        name=%22user_change_password%22),%0A                ) + urls%0A                break
 %0A   

Fix the fix for reversing the change password form when multiple langages are enabled in the admin.