Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. from django import forms
  2. from . import models
  3.  
  4.  
  5. class LoginForm(forms.Form):
  6. username = forms.CharField(
  7. widget=forms.TextInput(
  8. attrs={
  9. 'class': 'au-input au-input--full',
  10. 'placeholder': 'Username',
  11. 'required': True
  12. },
  13. )
  14. )
  15.  
  16. password = forms.CharField(
  17. widget=forms.PasswordInput(
  18. attrs={
  19. 'class': 'au-input au-input--full',
  20. 'placeholder': 'Password',
  21. 'required': True
  22. }
  23. )
  24. )
  25.  
  26.  
  27. class PositionSearchForm(forms.Form):
  28. search_field = forms.CharField(
  29. widget=forms.TextInput(
  30. attrs={
  31. 'class': 'form-control',
  32. 'placeholder': 'Search for positions',
  33. 'required': True
  34. },
  35. ),
  36. help_text='Minimum length is 3',
  37. min_length=3
  38. )
  39.  
  40.  
  41. class NewPositionForm(forms.ModelForm):
  42. class Meta:
  43. model = models.Positions
  44. fields = ('position_name',)
  45.  
  46. widgets = {
  47. 'position_name': forms.TextInput(
  48. attrs={
  49. 'class': 'form-control',
  50. 'placeholder': 'Position Name',
  51. 'required': True
  52. }
  53. )
  54. }
  55.  
  56. from django import forms
  57. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  58.  
  59. class RegisterForm(forms.ModelForm):
  60. password = forms.CharField(widget=forms.PasswordInput)
  61. password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
  62.  
  63. class Meta:
  64. model = models.User
  65. fields = ('email',)
  66.  
  67. def clean_email(self):
  68. email = self.cleaned_data.get('email')
  69. qs = models.User.objects.filter(email=email)
  70. if qs.exists():
  71. raise forms.ValidationError("email is taken")
  72. return email
  73.  
  74. def clean_password2(self):
  75. # Check that the two password entries match
  76. password1 = self.cleaned_data.get("password1")
  77. password2 = self.cleaned_data.get("password2")
  78. if password1 and password2 and password1 != password2:
  79. raise forms.ValidationError("Passwords don't match")
  80. return password2
  81.  
  82.  
  83. class UserAdminCreationForm(forms.ModelForm):
  84. """A form for creating new users. Includes all the required
  85. fields, plus a repeated password."""
  86. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  87. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  88.  
  89. class Meta:
  90. model = models.User
  91. fields = ('email',)
  92.  
  93. def clean_password2(self):
  94. # Check that the two password entries match
  95. password1 = self.cleaned_data.get("password1")
  96. password2 = self.cleaned_data.get("password2")
  97. if password1 and password2 and password1 != password2:
  98. raise forms.ValidationError("Passwords don't match")
  99. return password2
  100.  
  101. def save(self, commit=True):
  102. # Save the provided password in hashed format
  103. user = super(UserAdminCreationForm, self).save(commit=False)
  104. user.set_password(self.cleaned_data["password1"])
  105. if commit:
  106. user.save()
  107. return user
  108.  
  109.  
  110. class UserAdminChangeForm(forms.ModelForm):
  111. """A form for updating users. Includes all the fields on
  112. the user, but replaces the password field with admin's
  113. password hash display field.
  114. """
  115. password = ReadOnlyPasswordHashField()
  116.  
  117. class Meta:
  118. model = models.User
  119. fields = ('email', 'password', 'active', 'admin')
  120.  
  121. def clean_password(self):
  122. # Regardless of what the user provides, return the initial value.
  123. # This is done here, rather than on the field, because the
  124. # field does not have access to the initial value
  125. return self.initial["password"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement