forms.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from models import *
  4. MAX_UPLOAD_SIZE = 2500000
  5. class RegistrationForm(forms.Form):
  6. first_name = forms.CharField(max_length=20)
  7. last_name = forms.CharField(max_length=20)
  8. username = forms.CharField(max_length = 20)
  9. email = forms.EmailField(max_length = 100)
  10. password1 = forms.CharField(max_length = 200,
  11. label='Password',
  12. widget = forms.PasswordInput())
  13. password2 = forms.CharField(max_length = 200,
  14. label='Confirm password',
  15. widget = forms.PasswordInput())
  16. # Customizes form validation for properties that apply to more
  17. # than one field. Overrides the forms.Form.clean function.
  18. def clean(self):
  19. # Calls our parent (forms.Form) .clean function, gets a dictionary
  20. # of cleaned data as a result
  21. cleaned_data = super(RegistrationForm, self).clean()
  22. # Confirms that the two password fields match
  23. password1 = cleaned_data.get('password1')
  24. password2 = cleaned_data.get('password2')
  25. if password1 and password2 and password1 != password2:
  26. raise forms.ValidationError("Passwords did not match.")
  27. # We must return the cleaned data we got from our parent.
  28. return cleaned_data
  29. def clean_email(self):
  30. email = self.cleaned_data.get('email')
  31. # TODO: Confirms that the username is not already present in the
  32. # User model database.
  33. # if User.objects.filter(email__exact=email):
  34. # raise forms.ValidationError("Email is already taken.")
  35. return email
  36. # Customizes form validation for the username field.
  37. def clean_username(self):
  38. # Confirms that the username is not already present in the
  39. # User model database.
  40. username = self.cleaned_data.get('username')
  41. if User.objects.filter(username__exact=username):
  42. raise forms.ValidationError("Username is already taken.")
  43. # We must return the cleaned data we got from the cleaned_data
  44. # dictionary
  45. return username
  46. class EditProfile(forms.ModelForm):
  47. class Meta:
  48. model= Profile
  49. exclude = (
  50. 'user','content_type','followusers','email','picture_url')
  51. def clean(self):
  52. # Calls our parent (forms.Form) .clean function, gets a dictionary
  53. # of cleaned data as a result
  54. cleaned_data = super(EditProfile, self).clean()
  55. #check age
  56. age= cleaned_data.get('age')
  57. bio = cleaned_data.get('bio')
  58. if age<0:
  59. raise forms.ValidationError("Enter positive age")
  60. if len(bio)>430:
  61. raise forms.ValidationError("Maximum size of bio is 430 characters")
  62. return cleaned_data
  63. def clean_picture(self):
  64. picture = self.cleaned_data['picture']
  65. if not picture:
  66. return None
  67. if not picture.content_type or not picture.content_type.startswith('image'):
  68. raise profile_forms.ValidationError('File type is not image')
  69. if picture.size > MAX_UPLOAD_SIZE:
  70. raise profile_forms.ValidationError('File too big (max size is {0} bytes)'.format(MAX_UPLOAD_SIZE))
  71. return picture
  72. picture=forms.FileField(required=False)
  73. class EditRegistrationForm(forms.Form):
  74. first_name = forms.CharField(max_length=20)
  75. last_name = forms.CharField(max_length=20)
  76. # Customizes form validation for properties that apply to more
  77. # than one field. Overrides the forms.Form.clean function.
  78. def clean(self):
  79. # Calls our parent (forms.Form) .clean function, gets a dictionary
  80. # of cleaned data as a result
  81. cleaned_data = super(EditRegistrationForm, self).clean()
  82. return cleaned_data
  83. class AddPostForm(forms.ModelForm):
  84. class Meta:
  85. model = Posts
  86. exclude = ('user', 'date_time')
  87. def clean(self):
  88. cleaned_data = super(AddPostForm, self).clean()
  89. post_content = cleaned_data.get('post_content')
  90. if len(post_content)>160:
  91. raise forms.ValidationError("Maximum size of post is 160 characters")
  92. return cleaned_data