/usermgr/forms.py

https://github.com/dmkm2011/vinylmgr · Python · 95 lines · 84 code · 10 blank · 1 comment · 8 complexity · ae3f6e4743ca9850e0ed616e47c760d5 MD5 · raw file

  1. from django.contrib.sites.models import Site
  2. from django.contrib.auth.models import User
  3. from usermgr.models import UserProfile
  4. from django.contrib.auth.tokens import default_token_generator
  5. from django.utils.http import int_to_base36
  6. from django.template import Context, loader
  7. from django import forms
  8. from django.core.mail import send_mail
  9. class UserCreationForm(forms.ModelForm):
  10. username = forms.RegexField(label="Username", max_length=30, regex=r'^[\w.@+-]+$',
  11. help_text="Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.",
  12. error_messages = {'invalid': "This value may contain only letters, numbers and @/./+/-/_ characters."})
  13. password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
  14. password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput,
  15. help_text = "Enter the same password as above, for verification.")
  16. email1 = forms.EmailField(label="Email", max_length=75)
  17. email2 = forms.EmailField(label="Email confirmation", max_length=75,
  18. help_text = "Enter your email address again. A confirmation email will be sent to this address.")
  19. firstname = forms.CharField(label="First name", widget=forms.TextInput)
  20. lastname = forms.CharField(label="Last name", widget=forms.TextInput)
  21. class Meta:
  22. model = UserProfile
  23. fields = ("username",)
  24. def clean_password2(self):
  25. password1 = self.cleaned_data.get("password1", "")
  26. password2 = self.cleaned_data["password2"]
  27. if password1 != password2:
  28. raise forms.ValidationError("The two password fields didn't match.")
  29. return password2
  30. def clean_email1(self):
  31. email1 = self.cleaned_data["email1"]
  32. users_found = User.objects.filter(email__iexact=email1)
  33. if len(users_found) >= 1:
  34. raise forms.ValidationError("A user with that email already exist.")
  35. return email1
  36. def clean_email2(self):
  37. email1 = self.cleaned_data.get("email1", "")
  38. email2 = self.cleaned_data["email2"]
  39. if email1 != email2:
  40. raise forms.ValidationError("The two email fields didn't match.")
  41. return email2
  42. def save(self, commit=True, domain_override=None,
  43. email_template_name='usermgr/password_signup_email.html',
  44. use_https=False, token_generator=default_token_generator):
  45. user = super(UserCreationForm, self).save(commit=False)
  46. user.first_name = self.cleaned_data["firstname"]
  47. user.last_name = self.cleaned_data["lastname"]
  48. user.set_password(self.cleaned_data["password1"])
  49. user.email = self.cleaned_data["email1"]
  50. user.is_active = False
  51. if commit:
  52. user.save()
  53. if not domain_override:
  54. current_site = Site.objects.get_current()
  55. site_name = current_site.name
  56. domain = current_site.domain
  57. else:
  58. site_name = domain = domain_override
  59. t = loader.get_template(email_template_name)
  60. c = {
  61. 'email': user.email,
  62. 'domain': domain,
  63. 'site_name': site_name,
  64. 'uid': int_to_base36(user.id),
  65. 'user': user,
  66. 'token': token_generator.make_token(user),
  67. 'protocol': use_https and 'https' or 'http',
  68. }
  69. send_mail("Confirmation link sent on %s" % site_name,
  70. t.render(Context(c)), 'DMKMvinylmgr@gmail.com', [user.email])
  71. return user
  72. class ProfileEditForm(forms.ModelForm):
  73. firstname = forms.CharField(label="First name", widget=forms.TextInput)
  74. lastname = forms.CharField(label="Last name", widget=forms.TextInput)
  75. class Meta:
  76. model = UserProfile
  77. fields = ('firstname', 'lastname', 'personal_page', 'avatar',
  78. 'published_tracklist', 'published_ownedlist', 'biography')
  79. def save(self):
  80. u = super(ProfileEditForm, self).save(commit=False)
  81. u.user.first_name = self.cleaned_data['firstname']
  82. u.user.last_name = self.cleaned_data['lastname']
  83. #u.user.is_active = False
  84. u.user.save()
  85. u.save()