Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Django 1.4 Modifying Custom Account Model for Uniqueness of E-mail Addresses
- # additional model to incorporate our custom fields to the auth user model
- class Account(models.Model):
- user = models.OneToOneField(User) #link (pointer) to the users other information in User model
- birthdate = models.DateField(blank = True, ) # True makes this field optional
- gender = models.CharField(max_length = 1, choices = GENDER_CHOICE, null = True, blank = True)
- def __unicode__(self): # define a unicode for the user to access
- return u'%s %s' % (self.user.first_name, self.user.last_name) # return first and last name in shell
- # custom form to gather information from the user for a new account
- class UserRegistration(UserCreationForm):
- #class RegistrationForm(forms.ModelForm):
- class Meta:
- model = User
- fields = ("first_name", "last_name", "email", "username", "password1", "password2",)
- # ensures uniqueness of user email addresses when registering
- def clean_email(self):
- print "In custom creation"
- email = self.cleaned_data.get(email = 'email')
- username = self.cleaned_data.get(username = 'username')
- # checks if email address already exists
- if User.objects.filter(email__iexact = self.cleaned_data['email']):
- print "Email exists"
- # if email and User.objects.filter(email__iexact = email).exclude(username=username).exists():
- raise forms.ValidationError(u'Email Address is currently used by another user.')
- return email
- def Main(request):
- if request.user.is_authenticated():
- latest_events = Event.objects.all().order_by('-created')[:10] # Returns latest 10 events
- my_events = Event.objects.filter(creator=request.user)[:10] # Returns up to 10 events created by current User
- my_calendars = Calendar.objects.filter(creator=request.user) # Returns all calendars created by the user
- authForm = None
- loginForm = None
- headerType = "header.html"
- else:
- latest_events = None
- my_events = None
- my_calendars = None
- headerType = "header_main.html"
- authForm = UserRegistration(request.POST or None)
- print "Creating account UserRegistration" # TESTING PRINT
- print "User email = %s " %(User._meta.get_field('email'))
- if request.method == 'POST':
- if authForm.is_valid():
- newUser = authForm.save(commit=False)
- newUser.save()
- newUser = authenticate(username=request.POST['username'], password=request.POST['password1'])
- login(request, newUser)
- return HttpResponseRedirect('/signup/')
- ....
- ....
- more code on success redirection
- ....
- ....
- # custom form to display additional sign up information
- class RegistrationForm(UserCreationForm):
- #class RegistrationForm(forms.ModelForm):
- class Meta:
- model = User
- fields = ("first_name", "last_name", "email", "username", "password1", "password2",)
- def clean_email(self):
- """ensures uniqueness of user email addresses when registering"""
- email = self.cleaned_data.get('email')
Add Comment
Please, Sign In to add comment