snipt/blogs/middleware.py

49 lines
1.9 KiB
Python
Raw Normal View History

2015-07-24 18:39:25 -07:00
from annoying.functions import get_object_or_None
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
2012-06-01 08:38:55 -07:00
from django.shortcuts import get_object_or_404
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
host = request.META.get('HTTP_HOST', '')
host_s = host.replace('www.', '').split('.')
2019-01-23 15:52:55 -08: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:
2019-01-23 15:52:55 -08:00
if host_s[1] == "snipt":
2012-06-01 08:38:55 -07:00
2019-01-23 15:52:55 -08:00
blog_user = "".join(host_s[:-2])
2019-01-23 15:52:55 -08:00
if "-" in blog_user:
request.blog_user = get_object_or_None(
User, username__iexact=blog_user
)
if request.blog_user is None:
2019-01-23 15:52:55 -08:00
request.blog_user = get_object_or_404(
User, username__iexact=blog_user.replace("-", "_")
)
else:
2019-01-23 15:52:55 -08:00
request.blog_user = get_object_or_404(
User, username__iexact=blog_user
)
2012-06-01 09:10:10 -07:00
if request.blog_user is None:
pro_users = User.objects.filter(userprofile__is_pro=True)
2012-08-09 12:47:27 -07:00
for pro_user in pro_users:
2013-02-11 22:22:50 -08:00
if pro_user.profile.blog_domain:
2019-01-23 15:52:55 -08:00
if host in pro_user.profile.blog_domain.split(" "):
2013-02-11 22:05:12 -08:00
request.blog_user = pro_user
2019-01-23 15:52:55 -08:00
if host != pro_user.profile.get_primary_blog_domain():
2015-07-24 18:39:25 -07:00
return HttpResponseRedirect(
2019-01-23 15:52:55 -08:00
"http://"
+ pro_user.profile.get_primary_blog_domain()
)