django-lazysignup
django-lazysignup copied to clipboard
Doesn't work with pinax.apps.account
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']
}
You could probably add this to utils or something. Or just leave it here for reference ;)
Oops, the line in init should be this instead:
super(PinaxLazyConvertForm, self).__init__(*args, **kwargs)
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.
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.
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.