from django.shortcuts import redirect

from social.pipeline.partial import partial

# Monkey patching - an occasionally necessary evil.
from social import utils
from social.exceptions import InvalidEmail, AuthException

from django.core import signing
from django.core.signing import BadSignature
from django.contrib.sessions.models import Session
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied


@partial
def require_email(strategy, details, user=None, is_new=False, *args, **kwargs):
    if kwargs.get('ajax') or user and user.email:
        return
    elif is_new and not details.get('email'):
        email = strategy.request_data().get('email')
        if not email and details.get('username'):
            return
        if email:
            details['email'] = email
        else:
            return redirect('require_email')


def email_creator(strategy, details, user=None, is_new=False, *args, **kwargs):
    backend = kwargs['backend']
    request = kwargs['request']
    if not details.get('email'):
        details['email'] = "{}.{}@{}".format(
            details['username'], backend, request.get_host()
        )


def user_password(strategy, user, is_new=False, *args, **kwargs):
    backend = kwargs['backend']
    if backend.name != 'email':
        return

    password = strategy.request_data()['password']
    if is_new:
        user.set_password(password)
        user.save()
    elif not user.check_password(password):
        raise PermissionDenied


def get_user_by_email(strategy, details, user=None, is_new=False, *args, **kwargs):
    if kwargs.get('ajax') or user and user.email:
        return
    elif is_new and details.get('email'):
        user = get_user_model().objects.get(email=details.get('email'))
        return {'is_new': True, 'user': user}

@@ -475,16 +475,54 @@
 enied%0A%0A%0A
+ABSENT_MAIL_HOST = %22no.mail.for.me%22%0A%0A%0A
 @partial
@@ -1220,26 +1220,24 @@
 nd, 
-request.get_host()
+ABSENT_MAIL_HOST
 %0A   

fix email auth