|
1 import os |
|
2 import PIL |
|
3 |
|
4 from django import forms |
|
5 |
|
6 from registration.forms import RegistrationFormUniqueEmail |
|
7 from registration.models import RegistrationProfile |
|
8 |
|
9 from pytask.utils import make_key |
|
10 from pytask.profile.models import GENDER_CHOICES, Profile |
|
11 |
|
12 class CustomRegistrationForm(RegistrationFormUniqueEmail): |
|
13 """Used instead of RegistrationForm used by default django-registration |
|
14 backend, this adds aboutme, dob, gender, address, phonenum to the default |
|
15 django-registration RegistrationForm""" |
|
16 |
|
17 full_name = forms.CharField(required=True, max_length=50, |
|
18 label="Name as on your bank account", |
|
19 help_text="Any DD/Cheque will be issued on \ |
|
20 this name") |
|
21 |
|
22 aboutme = forms.CharField(required=True, max_length=1000, label=u"About Me", |
|
23 help_text="A write up about yourself to aid the\ |
|
24 reviewer in judging your eligibility for a task.\ |
|
25 It can have your educational background, CGPA,\ |
|
26 field of interests etc.," |
|
27 ) |
|
28 |
|
29 |
|
30 dob = forms.DateField(help_text = "YYYY-MM-DD", required=True, label=u'date of birth') |
|
31 gender = forms.ChoiceField(choices = GENDER_CHOICES, required=True, label=u'gender') |
|
32 |
|
33 address = forms.CharField(required=True, max_length=200, help_text="This \ |
|
34 information will be used while sending DD/Cheque") |
|
35 phonenum = forms.CharField(required=True, max_length=10, |
|
36 label="Phone Number") |
|
37 |
|
38 def clean_aboutme(self): |
|
39 """ Empty not allowed """ |
|
40 |
|
41 data = self.cleaned_data['aboutme'] |
|
42 if not data.strip(): |
|
43 raise forms.ValidationError("Please write something about\ |
|
44 yourself") |
|
45 |
|
46 return data |
|
47 |
|
48 def clean_address(self): |
|
49 """ Empty not allowed """ |
|
50 |
|
51 data = self.cleaned_data['address'] |
|
52 if not data.strip(): |
|
53 raise forms.ValidationError("Please enter an address") |
|
54 |
|
55 return data |
|
56 |
|
57 def clean_phonenum(self): |
|
58 """ should be of 10 digits """ |
|
59 |
|
60 data = self.cleaned_data['phonenum'] |
|
61 |
|
62 if (not data.strip()) or \ |
|
63 (data.strip("1234567890")) or \ |
|
64 (len(data)!= 10): |
|
65 raise forms.ValidationError("This is not a valid phone number") |
|
66 |
|
67 return data |
|
68 |
|
69 |
|
70 def save(self,profile_callback=None): |
|
71 |
|
72 new_user = RegistrationProfile.objects.create_inactive_user( |
|
73 username=self.cleaned_data['username'], |
|
74 password=self.cleaned_data['password1'], |
|
75 email=self.cleaned_data['email']) |
|
76 |
|
77 new_profile = Profile(user=new_user, |
|
78 aboutme=self.cleaned_data['aboutme'], |
|
79 dob=self.cleaned_data['dob'], |
|
80 gender=self.cleaned_data['gender'], |
|
81 address=self.cleaned_data['address'], |
|
82 phonenum=self.cleaned_data['phonenum'], |
|
83 uniq_key=make_key(Profile), |
|
84 ) |
|
85 new_profile.save() |
|
86 |
|
87 return new_user |
|
88 |
|
89 class EditProfileForm(forms.ModelForm): |
|
90 |
|
91 class Meta: |
|
92 model = Profile |
|
93 fields = ['full_name', 'aboutme', 'gender', 'dob', 'address', 'phonenum'] |