forms.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django import forms
  4. from django.contrib.auth.forms import PasswordResetForm, ReadOnlyPasswordHashField
  5. from django.forms.utils import ErrorList
  6. from django.forms.forms import BoundField
  7. from coin.members.models import Member
  8. from registration.forms import RegistrationForm
  9. class MemberRegistrationForm(RegistrationForm):
  10. # Protect against robot
  11. trap = forms.CharField(required=False, label='Trap',
  12. widget=forms.TextInput(attrs={'style' : 'display:none'}),
  13. help_text="Si vous êtes humain ignorez ce champ")
  14. def __init__(self, *args, **kwargs):
  15. super(MemberRegistrationForm, self).__init__(*args, **kwargs)
  16. for fieldname in ['email', 'organization_name', 'password2']:
  17. self.fields[fieldname].help_text = None
  18. def is_valid(self):
  19. valid = super(MemberRegistrationForm,self).is_valid()
  20. avoid_trap = not self.data['trap']
  21. if valid and avoid_trap:
  22. return True
  23. else:
  24. return False
  25. def as_p(self):
  26. """"
  27. We rewrite the as_p method to apply a style on the <p> tag related to
  28. trap field. Indeed, it seeems there is no cleaner way to do it.
  29. """
  30. def css_classes(self, extra_classes=None):
  31. return 'captcha'
  32. func_type = type(BoundField.css_classes)
  33. self['trap'].css_classes = func_type(css_classes, self, BoundField)
  34. return super(MemberRegistrationForm, self).as_p()
  35. class Meta:
  36. model = Member
  37. fields = ['type', 'first_name', 'last_name',
  38. 'organization_name', 'email', 'username', 'trap']
  39. class MemberCreationForm(forms.ModelForm):
  40. """
  41. This form was inspired from django.contrib.auth.forms.UserCreationForm
  42. and adapted to coin specificities
  43. """
  44. username = forms.RegexField(required=False,
  45. label="Nom d'utilisateur", max_length=30, regex=r"^[\w.@+-]+$",
  46. help_text="Laisser vide pour le générer automatiquement à partir du "
  47. "nom d'usage, nom et prénom, ou nom de l'organisme")
  48. password = forms.CharField(
  49. required=False, label='Mot de passe', widget=forms.PasswordInput,
  50. help_text="Laisser vide et envoyer un mail de bienvenue pour que "
  51. "l'utilisateur choisisse son mot de passe lui-même")
  52. class Meta:
  53. model = Member
  54. fields = '__all__'
  55. def save(self, commit=True):
  56. """
  57. Save member, then set his password
  58. """
  59. member = super(MemberCreationForm, self).save(commit=False)
  60. member.set_password(self.cleaned_data["password"])
  61. if commit:
  62. member.member()
  63. return member
  64. class AbstractMemberChangeForm(forms.ModelForm):
  65. """
  66. This form was inspired from django.contrib.auth.forms.UserChangeForm
  67. and adapted to coin specificities
  68. """
  69. class Meta:
  70. model = Member
  71. fields = '__all__'
  72. def __init__(self, *args, **kwargs):
  73. super(AbstractMemberChangeForm, self).__init__(*args, **kwargs)
  74. f = self.fields.get('user_permissions', None)
  75. if f is not None:
  76. f.queryset = f.queryset.select_related('content_type')
  77. def clean_password(self):
  78. # Regardless of what the user provides, return the initial value.
  79. # This is done here, rather than on the field, because the
  80. # field does not have access to the initial value
  81. return self.initial["password"]
  82. def clean_username(self):
  83. # idem clean_password
  84. return self.initial["username"]
  85. class AdminMemberChangeForm(AbstractMemberChangeForm):
  86. password = ReadOnlyPasswordHashField()
  87. class SpanError(ErrorList):
  88. def __unicode__(self):
  89. return self.as_spans()
  90. def __str__(self):
  91. return self.as_spans()
  92. def as_spans(self):
  93. if not self: return ''
  94. return ''.join(['<span class="error">%s</span>' % e for e in self])
  95. class PersonMemberChangeForm(AbstractMemberChangeForm):
  96. """
  97. Form use to allow natural person to change their info
  98. """
  99. class Meta:
  100. model = Member
  101. fields = ['first_name', 'last_name', 'email', 'nickname',
  102. 'home_phone_number', 'mobile_phone_number',
  103. 'address', 'postal_code', 'city', 'country']
  104. def __init__(self, *args, **kwargs):
  105. super(PersonMemberChangeForm, self).__init__(*args, **kwargs)
  106. self.error_class = SpanError
  107. for fieldname in self.fields:
  108. self.fields[fieldname].help_text = None
  109. class OrganizationMemberChangeForm(AbstractMemberChangeForm):
  110. """
  111. Form use to allow organization to change their info
  112. """
  113. class Meta:
  114. model = Member
  115. fields = ['organization_name', 'email', 'nickname',
  116. 'home_phone_number', 'mobile_phone_number',
  117. 'address', 'postal_code', 'city', 'country']
  118. def __init__(self, *args, **kwargs):
  119. super(OrganizationChangeForm, self).__init__(*args, **kwargs)
  120. self.error_class = SpanError
  121. for fieldname in self.fields:
  122. self.fields[fieldname].help_text = None
  123. class MemberPasswordResetForm(PasswordResetForm):
  124. pass