# -*- coding: utf-8 -*- import random import string from accounts.models import User from django.conf import settings from django.contrib.sites.models import RequestSite, Site from django.core import signing from django.core.mail import EmailMultiAlternatives from django.core.urlresolvers import reverse from django.shortcuts import redirect from registration import signals from registration.models import RegistrationProfile from social.pipeline.partial import partial def random_pass(): digits = random.sample(('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'), 4) chars = random.sample(string.lowercase[:], 4) password = chars + digits random.shuffle(password) return ''.join(password) def load_user(strategy, details, response, uid, *args, **kwargs): user = None if details.get('email'): email = details.get('email') user = User.objects.safe_get(email=email) return {'user': user, 'is_new': False} def create_user(strategy, details, response, uid, user=None, *args, **kwargs): if user: return {'user': user, 'is_new': False} else: request = strategy.request if Site._meta.installed: site = Site.objects.get_current() else: site = RequestSite(request) new_user = RegistrationProfile.objects.create_inactive_user(details['first_name'], details['last_name'], details['email'], random_pass(), site or 1) signals.user_registered.send(sender=User, user=new_user, request=request) #user = User.objects.create_social_user(username, details['first_name'], details['last_name']) return {'user': new_user, 'is_new': True} @partial def require_email(strategy, details, user=None, is_new=False, *args, **kwargs): if user and user.email: return elif is_new and not details.get('email'): email = strategy.request_data().get('email') if email and not User.objects.filter(email=email).exists(): details['email'] = email else: strategy.request.session['new_email'] = True strategy.request.session['new_email_invalid'] = True return redirect('acquire_email') def SendVerificationEmail(strategy, backend, code): """ Send an email with an embedded verification code and the necessary details to restore the required session elements to complete the verification and sign-in, regardless of what browser the user completes the verification from. """ signature = signing.dumps({"session_key": strategy.session.session_key, "email": code.email}, key=settings.EMAIL_SECRET_KEY) verifyURL = "{0}?verification_code={1}&signature={2}".format( reverse('social:complete', args=(backend.name,)), code.code, signature) verifyURL = strategy.request.build_absolute_uri(verifyURL) emailHTML = ''# Include your function that returns an html string here emailText = """Welcome to Expomap.ru! In order to login with your new user account, you need to verify your email address with us. Please click on this link to continue registration. """.format(verifyURL=verifyURL) kwargs = { "subject": "Verify Your Account", "body": emailText, "from_email": settings.CALLBACK_EMAIL, "to": [code.email], } email = EmailMultiAlternatives(**kwargs) email.attach_alternative(emailHTML, "text/html") email.send()