def get_user_by_username_or_email(username_or_email): """ Given an email or username, returns a user object or None """ try: if is_valid_email(username_or_email): user = User.objects.get(email__iexact=username_or_email) else: user = User.objects.get(username__iexact=username_or_email) except User.DoesNotExist: try: from emailware.models import EmailAddress except ImportError: return None try: user = EmailAddress.objects.get(email__iexact=username_or_email).user except EmailAddress.DoesNotExist: return None return user
def clean_email(self): """ Validates that an active user exists with the given username / email address """ username_or_email = self.cleaned_data["email"] if is_valid_email(username_or_email): try: user = User.objects.get(email__iexact=username_or_email) except User.DoesNotExist: raise forms.ValidationError(self.custom_error_messages['unknown_email']) if not user.is_active: raise forms.ValidationError(self.custom_error_messages['unknown_email']) if not user.has_usable_password(): raise forms.ValidationError(self.custom_error_messages['unusable_email']) else: try: user = User.objects.get(username__iexact=username_or_email) except User.DoesNotExist: raise forms.ValidationError(self.custom_error_messages['unknown_username']) if not user.is_active: raise forms.ValidationError(self.custom_error_messages['unknown_username']) if not user.has_usable_password(): raise forms.ValidationError(self.custom_error_messages['unusable_username']) return user.email