Last active
August 17, 2020 12:56
-
-
Save t-book/987cbf98437e480491ead763280940a4 to your computer and use it in GitHub Desktop.
form.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
t-book
commented
Aug 17, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment