Guest User

Untitled

a guest
Sep 10th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. Django 1.4 Modifying Custom Account Model for Uniqueness of E-mail Addresses
  2. # additional model to incorporate our custom fields to the auth user model
  3. class Account(models.Model):
  4. user = models.OneToOneField(User) #link (pointer) to the users other information in User model
  5. birthdate = models.DateField(blank = True, ) # True makes this field optional
  6. gender = models.CharField(max_length = 1, choices = GENDER_CHOICE, null = True, blank = True)
  7.  
  8. def __unicode__(self): # define a unicode for the user to access
  9. return u'%s %s' % (self.user.first_name, self.user.last_name) # return first and last name in shell
  10.  
  11.  
  12. # custom form to gather information from the user for a new account
  13. class UserRegistration(UserCreationForm):
  14. #class RegistrationForm(forms.ModelForm):
  15. class Meta:
  16. model = User
  17. fields = ("first_name", "last_name", "email", "username", "password1", "password2",)
  18.  
  19. # ensures uniqueness of user email addresses when registering
  20. def clean_email(self):
  21. print "In custom creation"
  22. email = self.cleaned_data.get(email = 'email')
  23. username = self.cleaned_data.get(username = 'username')
  24. # checks if email address already exists
  25. if User.objects.filter(email__iexact = self.cleaned_data['email']):
  26. print "Email exists"
  27. # if email and User.objects.filter(email__iexact = email).exclude(username=username).exists():
  28. raise forms.ValidationError(u'Email Address is currently used by another user.')
  29. return email
  30.  
  31. def Main(request):
  32. if request.user.is_authenticated():
  33. latest_events = Event.objects.all().order_by('-created')[:10] # Returns latest 10 events
  34. my_events = Event.objects.filter(creator=request.user)[:10] # Returns up to 10 events created by current User
  35. my_calendars = Calendar.objects.filter(creator=request.user) # Returns all calendars created by the user
  36. authForm = None
  37. loginForm = None
  38. headerType = "header.html"
  39. else:
  40. latest_events = None
  41. my_events = None
  42. my_calendars = None
  43. headerType = "header_main.html"
  44.  
  45. authForm = UserRegistration(request.POST or None)
  46. print "Creating account UserRegistration" # TESTING PRINT
  47. print "User email = %s " %(User._meta.get_field('email'))
  48. if request.method == 'POST':
  49. if authForm.is_valid():
  50. newUser = authForm.save(commit=False)
  51. newUser.save()
  52. newUser = authenticate(username=request.POST['username'], password=request.POST['password1'])
  53. login(request, newUser)
  54.  
  55. return HttpResponseRedirect('/signup/')
  56. ....
  57. ....
  58. more code on success redirection
  59. ....
  60. ....
  61.  
  62. # custom form to display additional sign up information
  63.  
  64. class RegistrationForm(UserCreationForm):
  65. #class RegistrationForm(forms.ModelForm):
  66. class Meta:
  67. model = User
  68. fields = ("first_name", "last_name", "email", "username", "password1", "password2",)
  69.  
  70. def clean_email(self):
  71. """ensures uniqueness of user email addresses when registering"""
  72. email = self.cleaned_data.get('email')
Add Comment
Please, Sign In to add comment