How can I change the "already exists" message in Django allauth?

When trying to log in with a social account when there is already an account with that email, the following message appears:

An account already exists with this e-mail address. Please sign in to that account first, then connect your Google account.

Now I would like to change this message. At first I tried to override ACCOUNT_SIGNUP_FORM_CLASS = 'mymodule.forms.MySignupForm'

and gave me my own method raise_duplicate_email_error

, but this method is never called.

The form looks like this:

class SignupForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()
    boolflag = forms.BooleanField()

    def raise_duplicate_email_error(self):
        # here I tried to override the method, but it is not called
        raise forms.ValidationError(
            _("An account already exists with this e-mail address."
              " Please sign in to that account."))

    def signup(self, request, user):
        # do stuff to the user and save it 

      

So the question is, how can I change this message?

+3


source to share


2 answers


If you want the method to be raise_duplicate_email_error

called, you must inherit from the form class that is actually calling self.raise_duplicate_email_error()

! However, you just inherit from forms.Form

!

Let's take a look at the code forms.py

at https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py . We can see what raise_duplicate_email_error

is a method BaseSignupForm

(and is called by its method clean_email

).

So you need to inherit from either from allauth.account.forms.BaseSignupForm

or from allauth.account.forms.SignupForm

(which also inherits from BaseSignupForm

and adds some more fields to it).

Update after OP's comment (which BaseSignupForm

comes from a function _base_signup_form_class()

that itself imports the form class defined in SIGNUP_FORM_CLASS setting

): Hmm, you're right. The problem is that the methods raise_duplicate_email_error

and clean_email

BaseSignupForm

do not cause unidirectional methods of their ancestors through super (so your raise_duplicate_email_error

never called).

Let's see what it looks like: if you added a line url(r'^accounts/', include('allauth.urls')),

to urls.py (which is usually done for django-allauth), you will see a line url(r"^signup/$", views.signup, name="account_signup"),

in the file https://github.com/pennersr/django-allauth/blob/13edcfef0d7e8f0de0003d6bcce7ef58119a5945/allauth/account /urls.py and then in https://github.com/pennersr/django-allauth/blob/13edcfef0d7e8f0de0003d6bcce7ef58119a5945/allauth/account/views.py you will see registration definition as signup = SignupView.as_view()

. So let's override SignupView

to use our own form and then use our class view for account_sigunp

!

Here's how to do it:



and. Create your custom view that inherits SignupView

and overrides the form class

class CustomFormSignupView (allauth.accounts.views.SignupView):
    form_class = CustomSignupForm

b. Create a custom form that inherits from SignupForm

and overrides the email verification message

class CustomSignupForm (allauth.accounts.forms.SignupForm):
    def raise_duplicate_email_error (self):
        # here I tried to override the method, but it is not called
        raise forms.ValidationError (
            _ ("An account already exists with this e-mail address."
              "Please sign in to that account."))

from. In your own urls.py add the following after include('allauth.urls')

to override account_signup

url

    url (r '^ accounts /', include ('allauth.urls')),
    url (r "^ accounts / signup / $", CustomFormSignupView.as_view (), name = "account_signup"), ``
+3


source


After creating your CustomSignupForm (allauth.accounts.forms.SignupForm) that overrides the raise_duplicate_email_error method, you can with version 0.18, add the following to the settings file:

SOCIALACCOUNT_FORMS = {
    'signup': 'path.to.your.custom.social.signup.form.CustomSignupForm'
}

      



Now the new method raise_duplicate_email_error is called.

Hope this helps you.

0


source







All Articles