Hello people,

I'm trying to write a short module to enable me to cleanup my urls.py
a bit since I'm anticipating deployment nightmares.

I'm trying to be a bit clever about this. Since my application is only
going to be used internally, and it's not in a publishing environment,
I've decided to go along the route of creating a decorator and url
registry for my application.

There is however a slight problemette.

For some reason, and i've no idea why, even though the views module is
being loaded it's not registering the views

## start code

### settings.py
DEFAULT_VIEW_MODULE = 'views'
ROOT_URLCONF = 'project.UrlRegistry'

### UrlRegistry.py
from django.conf.urls.defaults import patterns
import sys

class UrlRegistryResolver:

    def __init__(self):
        self._patterns = []
        self._load_patterns_from_views()

    def _load_patterns_from_views(self):
        for app in settings.INSTALLED_APPS:
            try:
                views = "%s.%s" % (app,settings.DEFAULT_VIEW_MODULE)
                module = __import__(views,'','',[''])
            except ImportError:
                pass

        return

    def RegisterPattern(self,pattern,**kwargs):
        def __decorator(view):
            self._patterns += patterns(view.__module__,
                                         (pattern,view.__name__,kwargs))
            return view
        return __decorator

sys.modules[__name__] = UrlRegistryResolver()

## application/views.py

from project import UrlRegistry

import pprint

pp = pprint.PrettyPrinter(indent=2)

@UrlRegistry.register_pattern(r'^test_noargs/')
def testview1(request):

    str = StringIO("Test Worked!<br /><pre>")
    pp.pprint(UrlRegistry.urlpatterns,str)

    return HttpResponse(str.getvalue())


## end code

If someone could anyone shed some light on why the above code is not
working, it would be worth beer tokens? I'm sure it's i'm doing
something fatally dumb. If so, please point me in the direction of the
corner with the 'Dunce' hat.

Ta, muchly

-- 
Richard Smith


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
from django.conf.urls import patterns
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

class UrlRegistryMiddleware(object):
    def process_request(self,request):
        for app in settings.INSTALLED_APPS:
            module = __import__(app,'','',[''])
            if hasattr(module,settings.DEFAULT_VIEW_MODULE):
                views = __import__('%s.%s' %( module.__name__, settings.DEFAULT_VIEW_MODULE ), '','',[''] )
        return None

class UrlRegistry:
    
    def __init__(self):        
        self.url_conf = settings.ROOT_URLCONF
    
    def _get_urlconf_module(self):
        try:
            return self._urlconf_module
        except AttributeError:
            try:
                self._urlconf_module = __import__(self.url_conf,'','',[''])
            except ValueError, e:
                raise ImproperlyConfigured, "Error while importing URLconf %r: %s" % (self.url_conf, e)
            return self._urlconf_module

    urlconf_module = property(_get_urlconf_module)    
    
    def _get_url_patterns(self):
        return self.urlconf_module.urlpatterns
    
    urlpatterns = property(_get_url_patterns)
    
    def RegisterPattern(self,pattern,*args,**kwargs):
        def __decorator(view):
            self.urlpatterns += patterns(view.__module__,
                                         (pattern,view.__name__,kwargs))
            return view
        return __decorator

Reply via email to