/wiwitasprofile/forms.py

https://github.com/scale2/wiwitasapps · Python · 109 lines · 56 code · 28 blank · 25 comment · 4 complexity · d4efe19d3b495830d1ca671bd8ad7d0c MD5 · raw file

  1. from django import forms
  2. from registration.forms import RegistrationForm
  3. from courses.models import CourseOfStudies
  4. from django.contrib.auth.models import User
  5. from django.utils.translation import ugettext as _
  6. from wiwitasprofile.models import WiwiTasProfile
  7. from registration.models import RegistrationProfile
  8. attrs_dict = { 'class': 'required' }
  9. class WiwiTasRegistrationForm(RegistrationForm):
  10. """
  11. Subclass of ``RegistrationForm`` which ...
  12. """
  13. gender = forms.ChoiceField(choices=WiwiTasProfile.GENDER_CHOICES)
  14. username = forms.CharField(widget=forms.HiddenInput, required=False)
  15. first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
  16. maxlegth=50)),
  17. label=_(u'First name'))
  18. last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
  19. maxlegth=50)),
  20. label=_(u'Last name'))
  21. matriculation_number = forms.RegexField(regex=r'^108\d{9}$',
  22. max_length=12,
  23. widget=forms.TextInput(attrs=attrs_dict),
  24. initial='108',
  25. label=_(u'Matriculation number'))
  26. course_of_studies = forms.ModelChoiceField(queryset=CourseOfStudies.objects.all())
  27. def clean_username(self):
  28. "This function is required to overwrite an inherited username clean"
  29. return self.cleaned_data['username']
  30. allowed_domains = ['rub.de', 'ruhr-uni-bochum.de']
  31. def clean_email(self):
  32. """
  33. Validate that the supplied email address is unique for the site.
  34. """
  35. if User.objects.filter(email__iexact=self.cleaned_data['email'].split('@')[0]):
  36. raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.'))
  37. # Check if the supplied email address is a rub address
  38. email_domain = self.cleaned_data['email'].split('@')[1]
  39. if email_domain not in self.allowed_domains:
  40. raise forms.ValidationError(_(u'Registration using non RUB email addresses is prohibited. Please supply a RUB email address.'))
  41. return self.cleaned_data['email']
  42. def clean_matriculation_number(self):
  43. """
  44. Validate that the supplied matriculation number is unique for the site.
  45. """
  46. if WiwiTasProfile.objects.filter(matriculation_number=self.cleaned_data['matriculation_number']):
  47. raise forms.ValidationError(_(u'This matriculation number is already in use. Please supply a different matriculation number. If this ist in fact your matriculation number and you didn\'t register an account, please contact the support.'))
  48. return self.cleaned_data['matriculation_number']
  49. def clean(self):
  50. if not self.errors:
  51. #set first part of the email adresse as username
  52. self.cleaned_data['username'] = '%s' % (self.cleaned_data['email'].split('@',1)[0])
  53. super(WiwiTasRegistrationForm, self).clean()
  54. return self.cleaned_data
  55. def save(self, profile_callback=None):
  56. """
  57. Create the new ``User`` and ``RegistrationProfile``, and
  58. returns the ``User``.
  59. This is essentially a light wrapper around
  60. ``RegistrationProfile.objects.create_inactive_user()``,
  61. feeding it the form data and a profile callback (see the
  62. documentation on ``create_inactive_user()`` for details) if
  63. supplied.
  64. """
  65. new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
  66. password=self.cleaned_data['password1'],
  67. email=self.cleaned_data['email'],
  68. profile_callback=profile_callback,
  69. send_email=False)
  70. wiwitas_profile = WiwiTasProfile.objects.create(user=new_user,
  71. first_name=self.cleaned_data['first_name'],
  72. last_name=self.cleaned_data['last_name'],
  73. gender=self.cleaned_data['gender'],
  74. matriculation_number=self.cleaned_data['matriculation_number'],
  75. course_of_studies=self.cleaned_data['course_of_studies'])
  76. return new_user