Source

django-publicauth / openauth / forms.py

Full commit
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from django import forms

from annoying.functions import get_object_or_None
from annoying.decorators import autostrip

from openauth.models import SocialID
from openauth import settings


class SocialIDExtraForm(forms.Form):
    def __init__(self, *args, **kwargs):
        """
        Form with fields from backend PROFILE_MAPPINGS setting.
        """
        fields = kwargs.pop("fields", {})
        for key, value in fields.values():
            self.base_fields[key] = value
        super(SocialIDExtraForm, self).__init__(*args, **kwargs)

    def clean_username(self):
        if get_object_or_None(User, username=self.cleaned_data['username']):
            raise forms.ValidationError(_(u'This username name is already taken'))
        return self.cleaned_data['username']


    def save(self, identity, provider):
        user = User.objects.create(username=self.cleaned_data['username'])
        if settings.SOCIALID_ACTIVATION_REQUIRED:
            user.is_active = False
        user.save()
        SocialID.objects.create(user=user, identity=identity, provider=provider)
        return user

SocialIDExtraForm = autostrip(SocialIDExtraForm)