Last active
August 29, 2015 14:06
-
-
Save pikhovkin/d6b404079c5f8ec608a6 to your computer and use it in GitHub Desktop.
Django-middleware for auto login in DEBUG mode
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
# coding: utf-8 | |
from django.contrib import auth | |
from django.conf import settings | |
from django.core.exceptions import PermissionDenied | |
from django.shortcuts import redirect | |
User = auth.get_user_model() | |
class UserAutoLogin(object): | |
password = '***************************' | |
def create_session(self, request, user): | |
host = request.get_host() | |
if 'testserver' == host: | |
return | |
user.set_password(self.password) | |
user.save() | |
if 'localhost' in host: | |
if ':8001' in host: | |
username = 'admin' | |
# elif ':8002' in host: | |
# username = 'admin' | |
else: | |
username = user.username | |
else: # shared | |
if ':8000' in host: | |
username = 'admin' | |
# elif ':8001' in host: | |
# username = 'admin' | |
else: | |
username = user.username | |
authuser = auth.authenticate(username=username, password=self.password) | |
if not authuser: | |
if getattr(settings, 'LOGIN_URL'): | |
return redirect(getattr(settings, 'LOGIN_URL')) | |
raise PermissionDenied | |
auth.login(request, authuser) | |
def process_request(self, request): | |
if not settings.DEBUG: | |
return | |
user = User.objects.get(username='admin') | |
self.create_session(request, user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment