Skip to content

Instantly share code, notes, and snippets.

@leplatrem

leplatrem/gist:6552003

Last active Dec 23, 2015
Embed
What would you like to do?
Auto-login middleware
from urlparse import urlparse
from django.conf import settings
from django.contrib.auth import get_user_model
User = get_user_model()
CONVERSION_SERVER_HOST = urlparse(settings.CONVERSION_SERVER).hostname
try:
internal_user = User.objects.get(username='__internal__')
except User.DoesNotExist:
internal_user = User(username='__internal__', password='__nologin__')
internal_user.is_active = False
internal_user.save()
class AutoLoginMiddleware(object):
def process_request(self, request):
user = getattr(request, 'user', None)
if user and user.is_anonymous():
remoteip = request.META.get('REMOTE_ADDR')
remotehost = request.META.get('REMOTE_HOST')
if CONVERSION_SERVER_HOST in (remoteip, remotehost):
request.user = internal_user
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment