class CustomAdminAuthenticationForm(AuthenticationForm): """ A custom authentication form used in the admin app. """ this_is_the_login_form = forms.BooleanField(widget=forms.HiddenInput, initial=1, error_messages={'required': LOGIN_SESSION_EXPIRED}) def clean(self): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') ad_server = settings.AD_SERVER message = ERROR_MESSAGE_LOGIN params = {'username': self.username_field.verbose_name} result = None if username and password: try: active_directory = SimpleLDAP(ad_server, username, password) result = active_directory.login() except (ValueError, Exception) as e: logging.info(e) if result: if result is -1: logging.info(message) else: try: self.user_cache = CustomUser.objects.get(username=username) self.user_cache.backend = 'django.contrib.auth.backends.ModelBackend' except CustomUser.DoesNotExist as e: logging.info(e) if not self.user_cache: self.user_cache = authenticate(username=username, password=password) print self.user_cache print self.user_cache.is_active print self.user_cache.is_staff if self.user_cache is None: raise forms.ValidationError(message, code='invalid', params=params) elif not self.user_cache.is_active or not self.user_cache.is_staff: raise forms.ValidationError(message, code='invalid', params=params) return self.cleaned_data
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question