stephenmcd/mezzanine — pull request #424 by dpnova (2012-10-06 22:14:14+00:00)
Title: Allow subclasses of ProfileForm inject their own ProfileFieldForm

Currently it's very hard to modify the fields that appear on Signup and Profile save. For example you may want to have extra fields on a user's setting page but only a simple subset on sign up. Modifying the excludes attribute on the ProfileFieldsForm isn't safe as it effects all instances of using the form.

This patch allows the ProfileForm subclass to provide its own form as a substitute, this allow easier customisation of which fields appear and how they are validated on a per action level.

Below is an example of an implementation on a subclass.

```python
class UserProfileForm(ProfileForm):

def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args, **kwargs)
self.error_class = SpanErrorList

def coerce(x):
if x == "True":
return True
if x == " False":
return False
if not self._signup:
from profiles.models import BOOL_CHOICES
self.fields['fb_share'] = forms.TypedChoiceField(
widget=forms.RadioSelect,
choices=BOOL_CHOICES,
coerce=coerce
)

class Meta:
model = User
fields = ['first_name', 'last_name', 'username', "email"]

def get_profile_fields_form(self):
if self._signup:
return SignupProfileFieldsForm
else:
return ProfileFieldsForm
```