PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ifs/forms.py

https://gitlab.com/andreweua/timtec
Python | 166 lines | 134 code | 30 blank | 2 comment | 28 complexity | ab1e372fa5ecb2429197fb3e1abef3c9 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. from django.contrib.auth import get_user_model
  3. from django import forms
  4. from django.shortcuts import redirect
  5. from django.core.urlresolvers import reverse_lazy
  6. from django.conf import settings
  7. from django.utils.translation import ugettext_lazy as _
  8. from allauth.account.forms import LoginForm
  9. class BaseUserChangeForm(forms.ModelForm):
  10. def __init__(self, *args, **kwargs):
  11. super(BaseUserChangeForm, self).__init__(*args, **kwargs)
  12. self.fields['password1'].required = True
  13. self.fields['password2'].required = True
  14. email = forms.RegexField(label=_("email"), max_length=75, regex=r"^[\w.@+-]+$")
  15. password1 = forms.CharField(widget=forms.PasswordInput, label=_("Password"), required=False)
  16. password2 = forms.CharField(widget=forms.PasswordInput, label=_("Password (again)"), required=False)
  17. accept_terms = forms.BooleanField(label=_('Accept '), initial=False, required=False)
  18. def clean_password2(self):
  19. password1 = self.cleaned_data.get('password1')
  20. password2 = self.cleaned_data.get('password2')
  21. if password1 and password2:
  22. if password1 != password2:
  23. raise forms.ValidationError(_("The two password fields didn't match."))
  24. return password2
  25. def clean_accept_terms(self):
  26. data = self.cleaned_data.get('accept_terms')
  27. if settings.TERMS_ACCEPTANCE_REQUIRED and not data:
  28. raise forms.ValidationError(_('You must agree to the Terms of Use to use %(site_name)s.'),
  29. params={'site_name': settings.SITE_NAME},)
  30. return data
  31. class SignupStudentCompletion(BaseUserChangeForm):
  32. def __init__(self, *args, **kwargs):
  33. super(SignupStudentCompletion, self).__init__(*args, **kwargs)
  34. self.fields['first_name'].required = True
  35. self.fields['last_name'].required = True
  36. self.fields['city'].required = True
  37. self.fields['ifid'].required = True
  38. self.fields['course'].required = True
  39. self.fields['klass'].required = True
  40. self.fields['campus'].required = True
  41. class Meta:
  42. model = get_user_model()
  43. fields = ('ifid', 'first_name', 'last_name', 'email', 'campus', 'city', 'course', 'klass')
  44. def save(self, **kwargs):
  45. user = super(SignupStudentCompletion, self).save(commit=False)
  46. user.ifid = self.cleaned_data['ifid']
  47. user.course = self.cleaned_data['course']
  48. user.klass = self.cleaned_data['klass']
  49. user.campus = self.cleaned_data['campus']
  50. user.city = self.cleaned_data['city']
  51. if self.cleaned_data['password1']:
  52. user.set_password(self.cleaned_data['password1'])
  53. user.accepted_terms = self.cleaned_data['accept_terms']
  54. user.save()
  55. class SignupProfessorCompletion(BaseUserChangeForm):
  56. def __init__(self, *args, **kwargs):
  57. super(SignupProfessorCompletion, self).__init__(*args, **kwargs)
  58. self.fields['first_name'].required = True
  59. self.fields['last_name'].required = True
  60. self.fields['city'].required = True
  61. self.fields['siape'].required = True
  62. self.fields['cpf'].required = True
  63. self.fields['campus'].required = True
  64. class Meta:
  65. model = get_user_model()
  66. fields = ('first_name', 'last_name', 'email', 'campus', 'city', 'siape', 'cpf')
  67. def save(self, **kwargs):
  68. user = super(SignupProfessorCompletion, self).save(commit=False)
  69. user.ifid = self.cleaned_data['siape']
  70. user.course = self.cleaned_data['cpf']
  71. user.campus = self.cleaned_data['campus']
  72. user.city = self.cleaned_data['city']
  73. if self.cleaned_data['password2']:
  74. user.set_password(self.cleaned_data['password2'])
  75. user.accepted_terms = self.cleaned_data['accept_terms']
  76. user.save()
  77. class IfSignupForm(BaseUserChangeForm):
  78. def __init__(self, *args, **kwargs):
  79. super(IfSignupForm, self).__init__(*args, **kwargs)
  80. self.fields['first_name'].required = True
  81. self.fields['last_name'].required = True
  82. self.fields['city'].required = True
  83. # self.fields['campus'] = forms.ModelChoiceField(queryset=Campus.objects.all())
  84. if_student = forms.BooleanField(required=False)
  85. class Meta:
  86. model = get_user_model()
  87. fields = ('first_name', 'last_name', 'email', 'campus', 'city', 'course', 'klass')
  88. def clean_username(self):
  89. data = self.cleaned_data['username']
  90. if not data:
  91. if 'if_student' in self.data:
  92. raise forms.ValidationError('O campo código de matrícula é obrigatório para alunos do IFSUL.')
  93. else:
  94. super(IfSignupForm, self).clean_username(self)
  95. return data
  96. def clean_course(self):
  97. data = self.cleaned_data['course']
  98. if 'if_student' in self.data and not data:
  99. raise forms.ValidationError('O campo curso é obrigatório para alunos do IFSUL.')
  100. return data
  101. def clean_klass(self):
  102. data = self.cleaned_data['klass']
  103. if 'if_student' in self.data and not data:
  104. raise forms.ValidationError('O campo turma é obrigatório para alunos do IFSUL.')
  105. return data
  106. def clean_campus(self):
  107. data = self.cleaned_data['campus']
  108. if 'if_student' in self.data and not data:
  109. raise forms.ValidationError('O campo campus é obrigatório para alunos do IFSUL.')
  110. return data
  111. def signup(self, request, user):
  112. if 'if_student' in self.data:
  113. user.ifid = self.cleaned_data['username']
  114. user.course = self.cleaned_data['course']
  115. user.klass = self.cleaned_data['klass']
  116. user.campus = self.cleaned_data['campus']
  117. user.city = self.cleaned_data['city']
  118. user.is_if_staff = True
  119. user.accepted_terms = self.cleaned_data['accept_terms']
  120. user.save()
  121. class IfLoginForm(LoginForm):
  122. def login(self, request, redirect_url=None):
  123. response = super(IfLoginForm, self).login(request, redirect_url)
  124. if self.user.is_authenticated():
  125. if self.user.is_if_staff:
  126. if request.user.groups.filter(name='professors').exists():
  127. if not (self.user.campus and self.user.cpf and self.user.siape):
  128. url = reverse_lazy('signup_completion')
  129. url += '?next=' + response.url
  130. return redirect(url)
  131. else:
  132. if not (self.user.campus and self.user.klass and self.user.course and self.user.ifid):
  133. url = reverse_lazy('signup_completion')
  134. url += '?next=' + response.url
  135. return redirect(url)
  136. return response