Skip to content

Instantly share code, notes, and snippets.

@t-book
Last active August 17, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-book/987cbf98437e480491ead763280940a4 to your computer and use it in GitHub Desktop.
Save t-book/987cbf98437e480491ead763280940a4 to your computer and use it in GitHub Desktop.
form.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from geonode.people.models import Profile
class CustomSignupForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=True)
last_name = forms.CharField(max_length=30, required=True)
email = forms.EmailField(max_length=254)
field_order = ['first_name', 'last_name', 'email', 'username']
class Meta:
model = Profile
fields = ('email', 'username','first_name', 'last_name', 'organization', 'city', 'position', 'profile')
field_order = ['username','first_name', 'last_name', 'email', 'password', 'organization', 'profile', 'position', 'city']
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.organization = self.cleaned_data['organization']
user.city = self.cleaned_data['city']
user.position = self.cleaned_data['position']
user.profile = self.cleaned_data['profile']
def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
try:
Profile.objects.get(username=username)
except Profile.DoesNotExist:
return username
raise forms.ValidationError(
self.error_messages['duplicate_username'],
code='duplicate_username',
)
if commit:
user.save()
return user
@t-book
Copy link
Author

t-book commented Aug 17, 2020

ACCOUNT_SIGNUP_FORM_CLASS = os.getenv("ACCOUNT_SIGNUP_FORM_CLASS",
                                          'ws_geonode.forms.CustomSignupForm')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment