# -*- 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, render from registration import signals from registration.models import RegistrationProfile from social.pipeline.partial import partial from proj.forms import EmailForm, AssociateForm 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 get_email(strategy, details, response, *args, **kwargs): if not details.get('email'): email = response.get('email') if email: details['email'] = email return {'details': details} 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 email = details.get('email') if is_new and not email: form = EmailForm(strategy.request.POST or None) if strategy.request.method == "POST" and form.is_valid(): email = form.cleaned_data.get('email') if not User.objects.filter(email=email).exists(): details['email'] = email return {'email': email} else: form = AssociateForm(strategy.request.POST or None) if form.is_valid() and form.user: details['email'] = email return {'email': email, 'user': form.user} return render(strategy.request, "registration/acquire_email.html", {'form': form}) return def social_user(backend, uid, user=None, *args, **kwargs): provider = backend.name social = backend.strategy.storage.user.get_social_auth(provider, uid) if social: # if user and social.user != user: # msg = 'This {0} account is already in use.'.format(provider) # raise AuthAlreadyAssociated(backend, msg) # elif not user: user = social.user return {'social': social, 'user': user, 'is_new': user is None, 'new_association': social is None} def user_details(strategy, details, user=None, *args, **kwargs): """Update user details using data from provider.""" if user: changed = False # flag to track changes protected = ('username', 'id', 'pk', 'email', 'first_name', 'last_name') + \ tuple(strategy.setting('PROTECTED_USER_FIELDS', [])) # Update user model attributes with the new data sent by the current # provider. Update on some attributes is disabled by default, for # example username and id fields. It's also possible to disable update # on fields defined in SOCIAL_AUTH_PROTECTED_FIELDS. for name, value in details.items(): if value and hasattr(user, name): # Check https://github.com/omab/python-social-auth/issues/671 current_value = getattr(user, name, None) if not current_value or name not in protected: changed |= current_value != value setattr(user, name, value) if changed: strategy.storage.user.changed(user) 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()