подправленая версия #1165

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from django.conf import settings
from django.http import HttpResponseRedirect
from django.core.urlresolvers import resolve
from django.views.debug import technical_500_response
from django.views.defaults import server_error
from django.contrib.sites.models import Site
from sys import exc_info
class RedirectMiddleware(object):
def process_request(self, request):
if request.is_ajax():
return None
try:
site = Site.objects.get_current()
except:
return None
host = request.get_host()
if host != site.domain:
if host.endswith('.' + site.domain):
subdomain = host[:host.index('.')]
if subdomain not in settings.DENIED_SUBDOMAINS:
try:
callback, callback_args, callback_kwargs = resolve(request.path, settings.SUBDOMAINS_ROOT_URLCONF)
except Exception, e:
if settings.DEBUG:
return technical_500_response(request, *exc_info())
else:
return server_error(request)
print 'except', repr(e)
else:
callback_kwargs.update({'user': subdomain})
try:
return callback(request, *callback_args, **callback_kwargs)
except Exception, e:
if settings.DEBUG:
return technical_500_response(request, *exc_info())
else:
return server_error(request)
return HttpResponseRedirect('http://%s%s' % (site.domain, request.path))