Completed
Pull Request — master (#326)
by
unknown
01:25
created

ContactUsForm.clean_contact_number()   A

Complexity

Conditions 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
1
from django import forms
2
from django.contrib.auth import get_user_model
3
from django.contrib.auth.forms import AuthenticationForm
4
from django.core.exceptions import ValidationError
5
from django.utils.translation import ugettext_lazy as _
6
7
from simplemathcaptcha.fields import MathCaptchaField
8
9
from wye.base.constants import ContactFeedbackType
10
11
from . import models
12
13
14
class UserAuthenticationForm(AuthenticationForm):
15
    username = forms.CharField(label=_("Email"), max_length=100, required=True)
16
    password = forms.CharField(label=_("Password"),
17
                               widget=forms.PasswordInput,
18
                               required=True)
19
20
21
class SignupForm(forms.ModelForm):
22
23
    def __init__(self, *args, **kwargs):
24
        super(SignupForm, self).__init__(*args, **kwargs)
25
        mandatory_field(self)
26
27
    mobile = forms.CharField(
28
        label=_("Mobile"),
29
        max_length=10,
30
        widget=forms.TextInput(
31
            attrs={'placeholder': 'Mobile'}
32
        )
33
    )
34
35
    def clean_mobile(self):
36
        mobile = self.cleaned_data['mobile']
37
        error_message = []
38
        if not mobile.isdigit():
39
            error_message.append(
40
                "Contact Number should only consist digits")
41
        if not len(mobile) == 10:
42
            error_message.append(
43
                "Contact Number should be of 10 digits")
44
45
        if error_message:
46
            raise ValidationError(error_message)
47
        return mobile
48
49
    class Meta:
50
        model = get_user_model()
51
        fields = ['first_name', 'last_name']
52
        widgets = {
53
            'first_name': forms.TextInput(attrs={'placeholder': 'First Name', 'autofocus': 'on'}),
54
            'last_name': forms.TextInput(attrs={'placeholder': 'Last Name'}),
55
        }
56
57
    def signup(self, request, user):
58
        profile = user.profile
59
        profile.mobile = self.cleaned_data['mobile']
60
        profile.save()
61
62
63
class UserProfileForm(forms.ModelForm):
64
    queryset = models.UserType.objects.exclude(
65
        slug__in=['admin', 'lead', 'coordinator'])
66
    usertype = forms.ModelMultipleChoiceField(
67
        label="Usertype", queryset=queryset)
68
69
    def __init__(self, *args, **kwargs):
70
        super(UserProfileForm, self).__init__(*args, **kwargs)
71
        mandatory_field(self)
72
73
    class Meta:
74
        model = models.Profile
75
        exclude = ('user', 'slug')
76
77
78
class ContactUsForm(forms.Form):
79
80
    name = forms.CharField(label='Your name*', required=True)
81
    email = forms.EmailField(label='Your email*', required=True)
82
    feedback_type = forms.ChoiceField(label='Feedback on*', required=True,
83
                                      choices=ContactFeedbackType.CHOICES)
84
    comments = forms.CharField(
85
        label='Comments*', required=True, widget=forms.Textarea)
86
    contact_number = forms.CharField(
87
        label='Your contact number', required=False)
88
    captcha = MathCaptchaField()
89
90
    def clean_contact_number(self):
91
        contact_number = self.cleaned_data['contact_number']
92
        error_message = []
93
        if not contact_number.isdigit():
94
            error_message.append(
95
                "Contact Number should only consist digits")
96
        if not len(contact_number) == 10:
97
            error_message.append(
98
                "Contact Number should be of 10 digits")
99
100
        if error_message:
101
            raise ValidationError(error_message)
102
        return contact_number
103
104
105
def mandatory_field(self):
106
    for v in filter(lambda x: x.required, self.fields.values()):
107
        v.label = str(v.label) + "*"
108