2015-07-24 18:39:25 -07:00
|
|
|
from annoying.functions import get_object_or_None
|
|
|
|
from django.contrib.auth.models import User
|
2013-02-11 22:32:44 -08:00
|
|
|
from django.http import HttpResponseRedirect
|
2012-06-01 08:38:55 -07:00
|
|
|
from django.shortcuts import get_object_or_404
|
2012-06-07 07:20:48 -07:00
|
|
|
|
2012-06-01 08:38:55 -07:00
|
|
|
|
|
|
|
class BlogMiddleware:
|
2012-05-24 20:55:30 -07:00
|
|
|
def process_request(self, request):
|
2012-06-01 08:38:55 -07:00
|
|
|
request.blog_user = None
|
|
|
|
|
2012-05-24 20:55:30 -07:00
|
|
|
host = request.META.get('HTTP_HOST', '')
|
|
|
|
host_s = host.replace('www.', '').split('.')
|
2012-06-01 08:38:55 -07:00
|
|
|
|
2015-07-24 18:28:31 -07:00
|
|
|
if host != 'snipt.net' and \
|
|
|
|
host != 'snipt.localhost' and \
|
|
|
|
host != 'local.snipt.net':
|
2012-06-01 08:38:55 -07:00
|
|
|
if len(host_s) > 2:
|
|
|
|
if host_s[1] == 'snipt':
|
|
|
|
|
2012-06-07 07:20:48 -07:00
|
|
|
blog_user = ''.join(host_s[:-2])
|
|
|
|
|
|
|
|
if '-' in blog_user:
|
2015-07-24 18:28:31 -07:00
|
|
|
request.blog_user = \
|
|
|
|
get_object_or_None(User,
|
|
|
|
username__iexact=blog_user)
|
2012-06-07 07:20:48 -07:00
|
|
|
|
|
|
|
if request.blog_user is None:
|
2015-07-24 18:28:31 -07:00
|
|
|
request.blog_user = \
|
|
|
|
get_object_or_404(User,
|
2015-07-24 18:39:25 -07:00
|
|
|
username__iexact=blog_user
|
|
|
|
.replace('-', '_'))
|
2012-06-07 07:20:48 -07:00
|
|
|
else:
|
2015-07-24 18:28:31 -07:00
|
|
|
request.blog_user = \
|
|
|
|
get_object_or_404(User, username__iexact=blog_user)
|
2012-06-01 09:10:10 -07:00
|
|
|
|
2013-01-15 21:09:54 -08:00
|
|
|
if request.blog_user is None:
|
2015-10-18 18:07:01 -07:00
|
|
|
# TODO: This needs to check profile.has_pro() instead.
|
2013-01-15 21:18:15 -08:00
|
|
|
pro_users = User.objects.filter(userprofile__is_pro=True)
|
2012-08-09 12:47:27 -07:00
|
|
|
|
2013-01-15 21:09:54 -08:00
|
|
|
for pro_user in pro_users:
|
2013-02-11 22:22:50 -08:00
|
|
|
if pro_user.profile.blog_domain:
|
2013-02-11 22:05:12 -08:00
|
|
|
if host in pro_user.profile.blog_domain.split(' '):
|
|
|
|
request.blog_user = pro_user
|
2013-02-11 22:32:44 -08:00
|
|
|
|
2015-07-24 18:39:25 -07:00
|
|
|
if host != \
|
|
|
|
pro_user.profile.get_primary_blog_domain():
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
'http://' +
|
|
|
|
pro_user
|
|
|
|
.profile
|
|
|
|
.get_primary_blog_domain())
|