Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. class BasicRegister(forms.Form):
  2.     def __init__(self, *args, **kwargs):
  3.         super(BasicRegister, self).__init__(*args, **kwargs)
  4.         self.type = None        
  5.        
  6.     username = forms.CharField(label=u'Username', max_length=30)
  7.     email = forms.EmailField(label=u'Email')
  8.     password1 = forms.CharField(
  9.         label=u'Password',
  10.         widget=forms.PasswordInput()
  11.     )    
  12.     password2 = forms.CharField(
  13.         label=u'Confirm Password',
  14.         widget=forms.PasswordInput()
  15.     )
  16.    
  17.     def clean_password2(self):
  18.         if 'password1' in self.cleaned_data:
  19.             password1 = self.cleaned_data['password1']
  20.             password2 = self.cleaned_data['password2']
  21.             if password1 == password2:
  22.                 return password2
  23.        
  24.         raise forms.ValidationError('Passwords do not match')
  25.        
  26.     def clean_username(self):
  27.         username = self.cleaned_data['username']
  28.         if not re.search(r'^\w+$', username):
  29.             raise forms.ValidationError('Username must be alphanumeric')
  30.            
  31.         try:
  32.             User.objects.get(username=username)
  33.         except User.DoesNotExist:
  34.             return username
  35.         raise forms.ValidationError('Username is already taken')
  36.        
  37.     def save(self, *args, **kwargs):
  38.         u = User.objects.create_user(
  39.             username = self.cleaned_data['username'],
  40.             password = self.cleaned_data['password1'],
  41.             email = self.cleaned_data['email']
  42.         )
  43.        
  44.         if self.type is not None:
  45.             if self.type == 'broker':
  46.                 broker = Broker(
  47.                     phone = self.cleaned_data['phone'],
  48.                     company_name = self.cleaned_data['company_name'],
  49.                     address1 = self.cleaned_data['address1'],
  50.                     address2 = self.cleaned_data['address2'],
  51.                     city = self.cleaned_data['city']
  52.                 )
  53.                 broker.save()
  54.                
  55.                 obj=broker
  56.                
  57.             elif self.type == 'agent':
  58.                 content_obj = Agent(**self.cleaned_data)
  59.                
  60.             profile = MyUser(user=u, content_object=obj)
  61.             profile.save()
  62.            
  63.        
  64.        
  65. class BrokerForm(BasicRegister):
  66.     def __init__(self, *args, **kwargs):
  67.         super(BrokerForm, self).__init__(*args, **kwargs)
  68.         self.type = 'broker'
  69.        
  70.     phone = forms.CharField()
  71.     company_name = forms.CharField()
  72.     address1 = forms.CharField()
  73.     address2 = forms.CharField()
  74.     city = forms.ModelChoiceField(queryset=City.objects, label='City')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement