Last active
December 23, 2015 00:09
-
-
Save leplatrem/6552003 to your computer and use it in GitHub Desktop.
Auto-login middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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