<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> from django import forms <EOL> from django . core import exceptions <EOL> from django . contrib . auth . models import User <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . import utils <EOL> from services . accounts import models as account_models <EOL> class BaseUserProfileForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> def clean_username ( self ) : <EOL> """<STR_LIT>""" <EOL> existing = User . objects . filter ( <EOL> username__iexact = self . cleaned_data [ '<STR_LIT:username>' ] <EOL> ) <EOL> if existing . exists ( ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> return self . cleaned_data [ '<STR_LIT:username>' ] <EOL> def clean_email ( self ) : <EOL> """<STR_LIT>""" <EOL> if User . objects . filter ( email__iexact = self . cleaned_data [ '<STR_LIT:email>' ] ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> if BaseUserProfileForm . validate_email ( self . cleaned_data [ '<STR_LIT:email>' ] ) : <EOL> return self . cleaned_data [ '<STR_LIT:email>' ] <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:password>' in self . cleaned_data and '<STR_LIT>' in self . cleaned_data : <EOL> if self . cleaned_data [ '<STR_LIT:password>' ] != self . cleaned_data [ '<STR_LIT>' ] : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) <EOL> ) <EOL> return self . cleaned_data <EOL> class RegistrationForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> """<STR_LIT>""" <EOL> model = account_models . UserProfile <EOL> fields = ( <EOL> '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> def signup ( self , request , user ) : <EOL> """<STR_LIT>""" <EOL> profile = account_models . UserProfile ( ) <EOL> profile . user_ptr = user <EOL> profile . organization = self . cleaned_data [ '<STR_LIT>' ] <EOL> profile . country = self . cleaned_data [ '<STR_LIT>' ] <EOL> profile . blocked = False <EOL> profile . is_verified = False <EOL> profile . save ( ) <EOL> user . username = self . cleaned_data [ '<STR_LIT:username>' ] <EOL> user . first_name = self . cleaned_data [ '<STR_LIT>' ] <EOL> user . last_name = self . cleaned_data [ '<STR_LIT>' ] <EOL> user . email = self . cleaned_data [ '<STR_LIT:email>' ] <EOL> user . is_superuser = False <EOL> user . is_staff = False <EOL> user . is_active = False <EOL> user . set_password ( self . cleaned_data [ '<STR_LIT>' ] ) <EOL> user . save ( ) <EOL> class ProfileUpdateForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> username = forms . CharField ( widget = forms . HiddenInput ) <EOL> class Meta : <EOL> """<STR_LIT>""" <EOL> model = account_models . UserProfile <EOL> fields = ( <EOL> '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> username = self . cleaned_data [ '<STR_LIT:username>' ] <EOL> new_email = self . cleaned_data [ '<STR_LIT:email>' ] <EOL> try : <EOL> user = User . objects . get ( username = username ) <EOL> except exceptions . ObjectDoesNotExist : <EOL> raise forms . ValidationError ( <EOL> _ ( <EOL> "<STR_LIT>" + username + "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> if user . email != new_email : <EOL> utils . validate_email ( self . cleaned_data [ '<STR_LIT:email>' ] ) <EOL> if User . objects . filter ( email__iexact = self . cleaned_data [ '<STR_LIT:email>' ] ) : <EOL> raise forms . ValidationError ( <EOL> _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> return self . cleaned_data </s>
