/registration/forms.py

https://github.com/kstosiek/zapisy_zosia · Python · 165 lines · 109 code · 35 blank · 21 comment · 18 complexity · 73ed8e5f0c65c409bed7fd38d710845f MD5 · raw file

  1. # -*- coding: UTF-8 -*-
  2. from django import forms
  3. from models import SHIRT_SIZE_CHOICES, SHIRT_TYPES_CHOICES
  4. from models import getOrgChoices as organization_choices
  5. from django.utils.translation import ugettext as _
  6. def lambda_clean_meal(meal,p1,p2,p3,z):
  7. # ok, this if fuckin hacky function... so stay tuned
  8. # meal is name of field (without suffix) that we wanna validate
  9. # p1, p2, p3 are _pais_ of _numeral suffixes_ (meal_suffix, day_suffix)
  10. #
  11. # we return validator method for (field_3) validating
  12. # if meals are bought for respective hotel nights
  13. #
  14. # this field is reused several times, so its kinda
  15. # hacky path for DRY principle ;)
  16. #
  17. # usecase (in class body):
  18. # clean_supper_3 = lambda_clean_meal('supper',(1,1),(2,2),(3,3))
  19. #
  20. def f(s):
  21. for n,d in [p1,p2,p3]:
  22. mealx = "%s_%s" % (meal,n)
  23. dayx = "%s_%s" % ("day", d)
  24. x = s.cleaned_data.get(mealx)
  25. d = s.cleaned_data.get(dayx)
  26. if x and not d:
  27. raise forms.ValidationError(_("You can buy meal only for adequate hotel night."))
  28. return s.cleaned_data.get("%s_%i" % (meal,z))
  29. return lambda(s):f(s)
  30. class RegisterForm(forms.Form):
  31. def __init__(self, *args, **kwargs) :
  32. super(forms.Form, self) .__init__(*args, **kwargs)
  33. self.fields['organization_1'].choices = organization_choices()
  34. email = forms.EmailField(required=True)
  35. password = forms.CharField(widget=forms.PasswordInput())
  36. password2 = forms.CharField(widget=forms.PasswordInput())
  37. name = forms.CharField()
  38. surname = forms.CharField()
  39. organization_1 = forms.ChoiceField(choices=organization_choices())
  40. organization_2 = forms.CharField(required=False)
  41. day_1 = forms.BooleanField(required=False, initial=True)
  42. day_2 = forms.BooleanField(required=False, initial=True)
  43. day_3 = forms.BooleanField(required=False, initial=True)
  44. breakfast_2 = forms.BooleanField(required=False, initial=True)
  45. breakfast_3 = forms.BooleanField(required=False, initial=True)
  46. breakfast_4 = forms.BooleanField(required=False, initial=True)
  47. dinner_1 = forms.BooleanField(required=False, initial=True)
  48. dinner_2 = forms.BooleanField(required=False, initial=True)
  49. dinner_3 = forms.BooleanField(required=False, initial=True)
  50. vegetarian = forms.BooleanField(required=False)
  51. shirt_size = forms.ChoiceField(choices=SHIRT_SIZE_CHOICES)
  52. shirt_type = forms.ChoiceField(choices=SHIRT_TYPES_CHOICES)
  53. bus = forms.BooleanField(required=False)
  54. def validate_nonempty(self,x):
  55. x = self.cleaned_data.get(x, '').strip()
  56. x = x.replace(' ','') # remove spaces
  57. # TODO: regex here!
  58. # [a-ż]+(-[a-ż]+)* for surnames ? ;)
  59. if len(x) == 0:
  60. raise forms.ValidationError("Be nice, fill this field properly.")
  61. return x
  62. def clean_name(self):
  63. return self.validate_nonempty('name')
  64. def clean_surname(self):
  65. return self.validate_nonempty('surname')
  66. def clean_password2(self):
  67. password = self.cleaned_data.get('password', '')
  68. password2 = self.cleaned_data.get('password2', '')
  69. if password != password2:
  70. raise forms.ValidationError("Ops, you made a typo in your password.")
  71. return password2
  72. def clean_password(self):
  73. password = self.cleaned_data.get('password', '')
  74. if len(password) < 6:
  75. raise forms.ValidationError("Password should be at least 6 characters long.")
  76. return password
  77. def clean_day_3(self):
  78. day1 = self.cleaned_data.get('day_1')
  79. day2 = self.cleaned_data.get('day_2')
  80. day3 = self.cleaned_data.get('day_3')
  81. if day1 or day2 or day3:
  82. return day3
  83. else:
  84. raise forms.ValidationError(_("At least one day should be selected."))
  85. clean_dinner_3 = lambda_clean_meal('dinner', (1,1), (2,2), (3,3), 3 )
  86. clean_breakfast_4 = lambda_clean_meal('breakfast', (2,1), (3,2), (4,3), 4 )
  87. # grrr, this REALLY should not be copied'n'pasted but
  88. # correct form class hierarchy should be developed
  89. # no time :(
  90. class ChangePrefsForm(forms.Form):
  91. def __init__(self, *args, **kwargs) :
  92. super(forms.Form, self) .__init__(*args, **kwargs)
  93. self.fields['organization_1'].choices = organization_choices()[:-1]
  94. def add_bad_org(self,prefs):
  95. if not prefs.org.accepted:
  96. self.fields['organization_1'].choices.append( (prefs.org.id,prefs.org.name) )
  97. def initialize(self,prefs):
  98. # change values depending on given user preferences
  99. for key in prefs.__dict__.keys():
  100. if self.fields.has_key(key):
  101. self.fields[key].initial = prefs.__dict__[key]
  102. # organization selection
  103. self.add_bad_org(prefs)
  104. self.fields['organization_1'].initial = prefs.org.id
  105. organization_1 = forms.ChoiceField(choices=organization_choices())
  106. day_1 = forms.BooleanField(required=False)
  107. day_2 = forms.BooleanField(required=False)
  108. day_3 = forms.BooleanField(required=False)
  109. breakfast_2 = forms.BooleanField(required=False)
  110. breakfast_3 = forms.BooleanField(required=False)
  111. breakfast_4 = forms.BooleanField(required=False)
  112. dinner_1 = forms.BooleanField(required=False)
  113. dinner_2 = forms.BooleanField(required=False)
  114. dinner_3 = forms.BooleanField(required=False)
  115. vegetarian = forms.BooleanField(required=False)
  116. shirt_size = forms.ChoiceField(choices=SHIRT_SIZE_CHOICES)
  117. shirt_type = forms.ChoiceField(choices=SHIRT_TYPES_CHOICES)
  118. bus = forms.BooleanField(required=False)
  119. paid = False
  120. def set_paid(self,b): self.paid = b
  121. def clean_day_3(self):
  122. day3 = self.cleaned_data.get('day_3')
  123. day1 = self.cleaned_data.get('day_1')
  124. day2 = self.cleaned_data.get('day_2')
  125. if day1 or day2 or day3 or self.paid:
  126. return day3
  127. else:
  128. raise forms.ValidationError(_("At least one day should be selected."))
  129. clean_dinner_3 = lambda_clean_meal('dinner', (1,1), (2,2), (3,3), 3 )
  130. clean_breakfast_4 = lambda_clean_meal('breakfast', (2,1), (3,2), (4,3), 4 )