/yats/forms.py

https://bitbucket.org/vidya0911/yats · Python · 160 lines · 105 code · 27 blank · 28 comment · 5 complexity · f09e724dbd493f299a50e92d84365d6b MD5 · raw file

  1. from django import forms
  2. from django.forms.util import ErrorList
  3. from django.contrib.auth.models import User
  4. from django.forms import ModelForm, Textarea
  5. from onlinetest.models import Test , Option , Student , Associate
  6. #from yats.views import Take_Test
  7. class ContactForm(forms.Form):
  8. subject = forms.CharField()
  9. mail = forms.EmailField(required=False)
  10. message = forms.CharField()
  11. class QuestionForm(forms.Form):
  12. def __init__(self, *args, **kwargs):
  13. test_id = kwargs.pop('test_id', 0)
  14. super(QuestionForm, self).__init__(*args, **kwargs)
  15. self.fields['test_id'] = forms.CharField(initial=test_id, widget=forms.widgets.HiddenInput())
  16. # self.fields['choices'] = forms.ChoiceField(choices=get_my_choices(test_id))
  17. #widget=forms.widgets.HiddenInput()
  18. self.fields['choices'] = forms.ChoiceField(choices=get_my_choices(test_id), widget=forms.RadioSelect(), required=True)
  19. def get_my_choices(test_id):
  20. choice_list = Option.objects.filter(test_id=test_id)
  21. # TODO convert this into a proper format look at dynamic workflows code
  22. return choice_list
  23. class StudentSignupForm(ModelForm):
  24. error_messages = {
  25. 'duplicate_username': "A user with that username already exists.",
  26. 'password_mismatch': "The two password fields didn't match.",
  27. }
  28. username = forms.RegexField(label="Username", max_length=30,
  29. regex=r'^[\w.@_]+$',
  30. help_text="Required. 20 characters or fewer. Letters, digits and "
  31. "@/_/. only.",
  32. error_messages={
  33. 'invalid': "This value may contain only letters, numbers and "
  34. "@/_/. characters."})
  35. password1 = forms.CharField(label="Password",
  36. widget=forms.PasswordInput)
  37. # password2 = forms.CharField(label="Password confirmation",
  38. # widget=forms.PasswordInput,
  39. # help_text="Enter the same password as above, for verification.")
  40. class Meta:
  41. model = Student
  42. fields = ("username", "password1",
  43. "first_name", "last_name",
  44. "email", "mobile",
  45. "address", "city", "state" , "pincode" , "country")
  46. widgets = {
  47. 'address': Textarea(attrs={'cols': 40, 'rows': 4}),
  48. }
  49. def clean_username(self):
  50. # Since User.username is unique, this check is redundant,
  51. # but it sets a nicer error message than the ORM. See #13147.
  52. username = self.cleaned_data["username"]
  53. try:
  54. User.objects.get(username=username)
  55. except User.DoesNotExist:
  56. return username
  57. raise forms.ValidationError(self.error_messages['duplicate_username'])
  58. # def clean_password2(self):
  59. # password1 = self.cleaned_data.get("password1", "")
  60. # password2 = self.cleaned_data["password2"]
  61. # if password1 != password2:
  62. # raise forms.ValidationError(
  63. # self.error_messages['password_mismatch'])
  64. # return password2
  65. def save(self, commit=True):
  66. student = super(StudentSignupForm, self).save(commit=False)
  67. student.set_password(self.cleaned_data["password1"])
  68. if commit:
  69. student.save()
  70. return student
  71. def as_mytable(self):
  72. return self._html_output(
  73. normal_row=u'<tr%(html_class_attr)s><td>%(label)s</td><td>%(errors)s%(field)s%(help_text)s</td></tr>',
  74. error_row=u'<tr><td colspan="2">%s</td></tr>',
  75. row_ender=u'</td></tr>',
  76. help_text_html=u'<br /><span class="helptext">%s</span>',
  77. errors_on_separate_row=False)
  78. class AssociateSignupForm(ModelForm):
  79. error_messages = {
  80. 'duplicate_username': "A user with that username already exists.",
  81. 'password_mismatch': "The two password fields didn't match.",
  82. }
  83. username = forms.RegexField(label="Username ", max_length=30,
  84. regex=r'^[\w.@_]+$',
  85. help_text="Required. 20 characters or fewer. Letters, digits and "
  86. "@/_/. only.",
  87. error_messages={
  88. 'invalid': "This value may contain only letters, numbers and "
  89. "@/_/. characters."})
  90. password1 = forms.CharField(label="Password",
  91. widget=forms.PasswordInput)
  92. # password2 = forms.CharField(label="Password confirmation",
  93. # widget=forms.PasswordInput,
  94. # help_text="Enter the same password as above, for verification.")
  95. class Meta:
  96. model = Associate
  97. fields = ("username", "password1",
  98. "first_name", "last_name",
  99. "email", "mobile",
  100. "address", "city", "state", "pincode", "country")
  101. widgets = {
  102. 'address': Textarea(attrs={'cols': 40, 'rows': 4}),
  103. }
  104. def clean_username(self):
  105. # Since User.username is unique, this check is redundant,
  106. # but it sets a nicer error message than the ORM. See #13147.
  107. username = self.cleaned_data["username"]
  108. try:
  109. User.objects.get(username=username)
  110. except User.DoesNotExist:
  111. return username
  112. raise forms.ValidationError(self.error_messages['duplicate_username'])
  113. # def clean_password2(self):
  114. # password1 = self.cleaned_data.get("password1", "")
  115. # password2 = self.cleaned_data["password2"]
  116. # if password1 != password2:
  117. # raise forms.ValidationError(
  118. # self.error_messages['password_mismatch'])
  119. # return password2
  120. def save(self):
  121. associate = super(AssociateSignupForm, self).save(commit=False)
  122. associate.set_password(self.cleaned_data["password1"])
  123. associate.is_staff=True
  124. associate.is_superuser=True
  125. associate.save()
  126. return associate
  127. def as_mytable(self):
  128. return self._html_output(
  129. normal_row=u'<tr%(html_class_attr)s><td>%(label)s</td><td>%(errors)s%(field)s%(help_text)s</td></tr>',
  130. error_row=u'<tr><td colspan="2">%s</td></tr>',
  131. row_ender=u'</td></tr>',
  132. help_text_html=u'<br /><span class="helptext">%s</span>',
  133. errors_on_separate_row=False)