trying to do ip based resolve

49 views
Skip to first unread message

Kurt

unread,
Oct 4, 2009, 1:41:34 PM10/4/09
to Django users

I think I must be doing something fundamentally wrong since this seems
like an easy problem...

I'm building a site, I want all ips not in INTERNAL_IPS to get an
"under construction" page and my internal ips to get the real site.
What I'm trying to do is call the application urlconf via the resolve
function.

The redirect to the construction page works fine.

The resolve seems to work since the /admin/ pages work but
django_authopenid do not, I get strange errors like "Reverse for
'user_sendpw' with arguments '()' and keyword arguments '{}' not
found."

If I change settings.py:ROOT_URLCONF to be "myapp.urls" then
authopenid works.

settings.py:
ROOT_URLCONF = 'redirector.urls'

redirector/urls.py:
urlpatterns = patterns('',
(r'^', 'redirector.views.redirect' ),
)

redirector/views.py:
def redirect(request, *args, **kwargs):
remote_addr = request.META['REMOTE_ADDR']
is_internal = remote_addr in settings.INTERNAL_IPS

if not is_internal:
return render_to_response( 'construction.html' )

uri = request.META.get('REQUEST_URI', '/' )

# if we're an internal ip address then find the application
urlconf
app = settings.SETTINGS_MODULE.split( "." )[0]
app_urls = app + ".urls"

# FANCY BIT IS RIGHT HERE
view, args, kwargs = resolve( urlparse(uri)[2], app_urls )

return view(request, *args, **kwargs)

Kristaps Kūlis

unread,
Oct 4, 2009, 3:19:12 PM10/4/09
to django...@googlegroups.com
You should and must do such things at middleware (as god intended).

example: http://www.djangosnippets.org/snippets/845/
just return custom  HttpResponse, not HttpResponseForbiden


Kristaps Kūlis

Kurt Neufeld

unread,
Oct 4, 2009, 4:23:26 PM10/4/09
to django...@googlegroups.com

Wow, was that ever easy. Thanks for your help!

Kurt

For future googlers:

settings.py:
MIDDLEWARE_CLASSES = (
...
'redirector.ConstructionFilterMiddleware',
...
)

redirector/__init__.py:
from django.shortcuts import render_to_response
from django.conf import settings

class ConstructionFilterMiddleware(object):
"""
This middleware redirects all requests from clients that are
not in INTERNAL_IPS to the construction.html page
"""

def process_request(self, request):


remote_addr = request.META['REMOTE_ADDR']
is_internal = remote_addr in settings.INTERNAL_IPS

if not is_internal:
return render_to_response( 'construction.html' )

Kurt Neufeld

unread,
Oct 13, 2009, 8:55:28 PM10/13/09
to django...@googlegroups.com

I'm replying to my own message because I was getting some strange
behaviour and this is how I fixed it.

I would occasionally see unformatted debug_toolbar output after my
"under construction" page. I could reliably reproduce this "bug" (I
think it's a bug but I have no idea where) via the following:

1. make a request using a proxy that is in INTERNAL_IP, see proper
internal page
2. immediately make a request using a proxy that is not in
INTERNAL_IP, see construction page with debug output
3. 'touch settings.py'
4. make a request using external proxy, see only construction page, no
debug output

If it makes any difference I'm using wsgi and a monitor.py program
that I got from a website.

Anyhow, the solution was to change my middleware to:

def process_response( self, request, response ):
remote_addr = request.META['REMOTE_ADDR']
return remote_addr in settings.INTERNAL_IPS

if not is_internal:
return render_to_response( 'construction.html' )

return response


ie: do a process_RESPONSE not a process_REQUEST. So there's how you do
that.

Anybody have any ideas about the bug of seeing the debug_toolbar
output (without css/js formatting). I'll file a bug if it's a bug and
not just me not knowing how to write middleware. My middleware
problem? debug_toolbar problem? Django problem?

Kurt
Reply all
Reply to author
Forward
0 new messages