django-lazysignup icon indicating copy to clipboard operation
django-lazysignup copied to clipboard

Doesn't work with pinax.apps.account

Open aehlke opened this issue 13 years ago • 5 comments

I'm using the .9x master dev version of Pinax and was able to get lazysignup working with pinax's account app with the following form, which I pass to the convert view:

from account.forms import SignupForm as PinaxSignupForm

class PinaxLazyConvertForm(PinaxSignupForm):
    '''
    `PinaxSignupForm` is a subclass of forms.Form.
    django-lazysignup expects this form to behave like a ModelForm.
    So it will pass `instance` to __init__, which we will save to use 
    later when we create the user when the form is saved.
    '''
    def __init__(self, *args, **kwargs):
        self.instance = None
        if 'instance' in kwargs:
            # PinaxSignupForm breaks if we pass it this
            self.instance = kwargs.pop('instance')
        super(LazyConvertForm, self).__init__(*args, **kwargs)

    def create_user(self, username=None, commit=True):
        '''Gets called by PinaxSignupForm.save'''
        user = self.instance or User()
        if username is None:
            raise NotImplementedError("SignupForm.create_user does not handle "
                "username=None case. You must override this method.")
        user.username = username
        user.email = self.cleaned_data["email"].strip().lower()
        password = self.cleaned_data.get("password1")
        if password:
            user.set_password(password)
        else:
            user.set_unusable_password()
        if commit:
            user.save()
        return user

    def get_credentials(self):
        return {
            'username': self.cleaned_data['username'],
            'password': self.cleaned_data['password1']
        }

aehlke avatar Feb 18 '11 23:02 aehlke

You could probably add this to utils or something. Or just leave it here for reference ;)

aehlke avatar Feb 18 '11 23:02 aehlke

Oops, the line in init should be this instead:

    super(PinaxLazyConvertForm, self).__init__(*args, **kwargs)

aehlke avatar Feb 19 '11 00:02 aehlke

Thanks for the report. I'm not sure this belongs in lazysignup itself - I certainly don't want to grow a dependency on the accounts app! But it might be a useful note for the docs. I'll leave the ticket open for now, and review this for inclusion next time I make changes.

danfairs avatar Feb 19 '11 12:02 danfairs

You're right, I'm not sure where it's best to put it. Doesn't belong in pinax either. It's not entirely unlikely that someone would want to use this package w/ pinax though, so some note of it would be nice.

aehlke avatar Feb 21 '11 07:02 aehlke

If more than documentation was required, I'd probably just drop a pinax.py in there, containing the integration code.

I imagine it'll get done when I do a Pinax project :)

Of course if you wanted to fork the project and add it with docs and tests, I'd be happy to merge that. The tests already use the mock package, so you should be able to mock out any parts of the Pinax framework required.

danfairs avatar Feb 21 '11 09:02 danfairs