import importlib

from django.conf import settings
from django.http import Http404, HttpResponseRedirect

from glitter import urls as glitter_urls
from glitter.exceptions import GlitterRedirectException, GlitterUnpublishedException


class PageFallbackMiddleware(object):
    def process_response(self, request, response):

        # Import here as it causes migrations for pages if app is not installed apps.
        from glitter.pages.views import glitter, render_object_unpublished

        if response.status_code != 404:
            return response  # No need to check for a page for non-404 responses.

        try:
            return glitter(request=request, url=request.path_info)
        except Http404:
            # Return the original response if a 404 occurs
            return response
        except GlitterUnpublishedException as exception:
            return render_object_unpublished(request=request, obj=exception.obj)
        except GlitterRedirectException as exception:
            return HttpResponseRedirect(exception.url)

        return None


class GlitterUrlConf(object):
    def __init__(self):
        """
        URLs for Glitter App Pages can change based upon user's actions. So need to rebuild on each
        request.

        Have tried to reload the minimum amount of urlconfs to keep performance up.
        """
        root_urlconf = importlib.import_module(settings.ROOT_URLCONF)
        importlib.reload(root_urlconf)
        importlib.reload(glitter_urls)
        self.urlpatterns = root_urlconf.urlpatterns
        self.handler500 = root_urlconf.handler500


class GlitterUrlConfMiddleware(object):
    def process_request(self, request):
        """
        Avoids having to restart the server to recreate the url_conf being used by Django.

        Doing it this way allows the url_conf to be set at runtime.
        """
        request.urlconf = GlitterUrlConf()

@@ -14,34 +14,43 @@
 lib%0A
-%0A
 from 
-django.conf 
+threading import Lock%0A
 import s
 etti
@@ -45,23 +45,19 @@
 import s
-ettings
+ys%0A
 %0Afrom dj
@@ -108,49 +108,8 @@
 ct%0A%0A
-from glitter import urls as glitter_urls%0A
 from
@@ -189,16 +189,54 @@
 ception%0A
+from glitter.pages.models import Page%0A
 %0A%0Aclass 
@@ -1061,24 +1061,69 @@
 turn None%0A%0A%0A
+_urlconf_pages = %5B%5D%0A_urlconf_lock = Lock()%0A%0A%0A
 class Glitte
@@ -1130,16 +1130,26 @@
 rUrlConf
+Middleware
 (object)
@@ -1162,236 +1162,204 @@
 def 
-__init__(self):%0A        %22%22%22%0A        URLs for Glitter App Pages can change based upon user's actions. So need to rebuild on each%0A        request.%0A%0A        Have tried to reload the minimum amount of urlconfs to keep performance up
+process_request(self, request):%0A        %22%22%22%0A        Reloads glitter URL patterns if page URLs change.%0A%0A        Avoids having to restart the server to recreate the glitter URLs being used by Django
 .%0A  
@@ -1376,28 +1376,31 @@
         
-root
+global 
 _urlconf
  = impor
@@ -1395,319 +1395,264 @@
 conf
- = importlib.import_module(settings.ROOT_URLCONF)%0A        importlib.reload(root_urlconf)%0A        importlib.reload(glitter_urls)%0A        self.urlpatterns = root_urlconf.urlpatterns%0A        self.handler500 = root_urlconf.handler500%0A%0A%0Aclass GlitterUrlConfMiddleware(object):%0A    def process_request(self, request):
+_pages%0A%0A        page_list = list(%0A            Page.objects.exclude(glitter_app_name='').values_list('id', 'url').order_by('id')%0A        )%0A%0A        with _urlconf_lock:%0A            if page_list != _urlconf_pages:%0A                glitter_urls = 'glitter.urls'
 %0A   
@@ -1656,28 +1656,24 @@
         
-%22%22%22%0A
         
 Avoids h
@@ -1668,159 +1668,103 @@
     
-Avoids having to restart the server to recreate the url_conf being used by Django.%0A%0A        Doing it this way allows the url_conf to be set at runtime.
+if glitter_urls in sys.modules:%0A                    importlib.reload(sys.modules%5Bglitter_urls%5D)
 %0A   
@@ -1768,28 +1768,24 @@
         
-%22%22%22%0A
         
 request.
@@ -1780,39 +1780,31 @@
     
-request.urlconf = GlitterUrlConf()
+_urlconf_pages = page_list
 %0A

Fix memory leak with GlitterUrlConf middleware