Skip to content

Instantly share code, notes, and snippets.

@leplatrem
Last active December 23, 2015 00:09
Show Gist options
  • Save leplatrem/6552003 to your computer and use it in GitHub Desktop.
Save leplatrem/6552003 to your computer and use it in GitHub Desktop.
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