Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple signup, registration forms using django-allauth

The application I am working on needs a separate login for 2 different type of users. We need "clients" and "business" owners to be able to register.

For the "business" owner all that I need to do is set the boolean user.is_business to True

I have used ACCOUNT_SIGNUP_FORM_CLASS with a separate class that sets the boolean to true and that works like a charm. But then the client login doesn't work anymore.

Is there a way to create a separate signup view for a different user?

I have tried the following

class BusinessUserRegistrationView(FormView):
    form_class = BusinessSignupForm
    template_name = 'allauth/account/signup.html'
    view_name = 'organisersignup'
    success_url = reverse_lazy(view_name)
organisersignup = BusinessUserRegistrationView.as_view()

And the form

class BusinessSignupForm(BaseSignupForm):
    password1 = SetPasswordField(label=_("Password"))
    password2 = PasswordField(label=_("Password (again)"))
    confirmation_key = forms.CharField(max_length=40,
                                       required=False,
                                       widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):

        super(BusinessSignupForm, self).__init__(*args, **kwargs)
        if not app_settings.SIGNUP_PASSWORD_VERIFICATION:
            del self.fields["password2"]

    def clean(self):
        super(BusinessSignupForm, self).clean()
        if app_settings.SIGNUP_PASSWORD_VERIFICATION \
                and "password1" in self.cleaned_data \
                and "password2" in self.cleaned_data:
            if self.cleaned_data["password1"] \
                    != self.cleaned_data["password2"]:
                raise forms.ValidationError(_("You must type the same password"
                                              " each time."))
        return self.cleaned_data

    def save(self, request):
        adapter = get_adapter()
        user = adapter.new_user(request)
        user.is_business = True
        adapter.save_user(request, user, self)
        self.custom_signup(request, user)
        setup_user_email(request, user, [])
        return user

And in the urls.py

url(r'^organiser/$', 'authentication.views.organisersignup', name='organisersignup'),

The problem is that somehow, the boolean is_business is never set to True. The from shows, I can save, but what is saved is never a business always a client. The BusinessSignupForm is a copy of the SignUpForm found in the allauth forms.

What am I doing wrong?

like image 867
Astuanax Avatar asked Mar 08 '16 19:03

Astuanax


2 Answers

I'll answer the question as I found the solution to have multiple signup forms with allauth.

Form:

class BusinessSignupForm(SignupForm):
    def save(self, request):
        user = super(BusinessSignupForm, self).save(request)
        user.is_organizer = True
        user.save()
        return user

View

class BusinessUserRegistrationView(SignupView):
    template_name = 'allauth/account/signup-organizer.html'
    form_class = BusinessSignupForm
    redirect_field_name = 'next'
    view_name = 'organisersignup'
    success_url = None

    def get_context_data(self, **kwargs):
        ret = super(BusinessUserRegistrationView, self).get_context_data(**kwargs)
        ret.update(self.kwargs)
        return ret

organisersignup = BusinessUserRegistrationView.as_view()

Template

 <form id="signup_form" method="post" action="{% url 'organisersignup' %}">
      {% csrf_token %}
      {% bootstrap_form form %}
 </form>

This can be reused over and over again to modify properties of the custom user model if you have one.

Currently running Django==1.8.10 and django-allauth==0.24.1

like image 110
Astuanax Avatar answered Oct 24 '22 16:10

Astuanax


Instead of using user.is_business = True to differentiate types of users consider using a BusinessProfile class. You can have several profile types per user if necessary, for example a PartnerProfile, ClientProfile, SupplierProfile etc. Each profile type can have it's own signup, login, and profile pages.

Here are some alternative solutions: Multiple user type sign up with django-allauth

like image 41
Alex Gustafson Avatar answered Oct 24 '22 17:10

Alex Gustafson