<s> import re <EOL> import urllib2 <EOL> from urlparse import urlparse <EOL> from django import forms <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . sites . models import Site <EOL> from django . core . files . base import ContentFile <EOL> from django . core . files . storage import default_storage <EOL> from django . core . validators import URLValidator <EOL> from django . forms import ModelForm <EOL> from django . utils . translation import ugettext as _ , ugettext_lazy <EOL> from django . conf import settings <EOL> from recaptcha . client import captcha <EOL> from registration . forms import RegistrationFormUniqueEmail <EOL> from registration . models import RegistrationProfile <EOL> from onadata . apps . main . models import UserProfile <EOL> from onadata . apps . viewer . models . data_dictionary import upload_to <EOL> from onadata . libs . utils . country_field import COUNTRIES <EOL> from onadata . libs . utils . logger_tools import publish_xls_form <EOL> FORM_LICENSES_CHOICES = ( <EOL> ( '<STR_LIT>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , <EOL> ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , <EOL> ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ) <EOL> DATA_LICENSES_CHOICES = ( <EOL> ( '<STR_LIT>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , <EOL> ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , <EOL> ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , <EOL> ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ) <EOL> PERM_CHOICES = ( <EOL> ( '<STR_LIT>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ) <EOL> class DataLicenseForm ( forms . Form ) : <EOL> value = forms . ChoiceField ( choices = DATA_LICENSES_CHOICES , <EOL> widget = forms . Select ( <EOL> attrs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' } ) ) <EOL> class FormLicenseForm ( forms . Form ) : <EOL> value = forms . ChoiceField ( choices = FORM_LICENSES_CHOICES , <EOL> widget = forms . Select ( <EOL> attrs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' } ) ) <EOL> class PermissionForm ( forms . Form ) : <EOL> for_user = forms . CharField ( <EOL> widget = forms . TextInput ( <EOL> attrs = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> ) <EOL> perm_type = forms . ChoiceField ( choices = PERM_CHOICES , widget = forms . Select ( ) ) <EOL> def __init__ ( self , username ) : <EOL> self . username = username <EOL> super ( PermissionForm , self ) . __init__ ( ) <EOL> class UserProfileForm ( ModelForm ) : <EOL> class Meta : <EOL> model = UserProfile <EOL> exclude = ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> email = forms . EmailField ( widget = forms . TextInput ( ) ) <EOL> class UserProfileFormRegister ( forms . Form ) : <EOL> REGISTRATION_REQUIRE_CAPTCHA = settings . REGISTRATION_REQUIRE_CAPTCHA <EOL> RECAPTCHA_PUBLIC_KEY = settings . RECAPTCHA_PUBLIC_KEY <EOL> RECAPTCHA_HTML = captcha . displayhtml ( settings . RECAPTCHA_PUBLIC_KEY , <EOL> use_ssl = settings . RECAPTCHA_USE_SSL ) <EOL> name = forms . CharField ( widget = forms . TextInput ( ) , required = True , <EOL> max_length = <NUM_LIT:255> ) <EOL> city = forms . CharField ( widget = forms . TextInput ( ) , required = False , <EOL> max_length = <NUM_LIT:255> ) <EOL> country = forms . ChoiceField ( widget = forms . Select ( ) , required = False , <EOL> choices = COUNTRIES , initial = '<STR_LIT>' ) <EOL> organization = forms . CharField ( widget = forms . TextInput ( ) , required = False , <EOL> max_length = <NUM_LIT:255> ) <EOL> home_page = forms . CharField ( widget = forms . TextInput ( ) , required = False , <EOL> max_length = <NUM_LIT:255> ) <EOL> twitter = forms . CharField ( widget = forms . TextInput ( ) , required = False , <EOL> max_length = <NUM_LIT:255> ) <EOL> recaptcha_challenge_field = forms . CharField ( required = False , max_length = <NUM_LIT> ) <EOL> recaptcha_response_field = forms . CharField ( <EOL> max_length = <NUM_LIT:100> , required = settings . REGISTRATION_REQUIRE_CAPTCHA ) <EOL> def save ( self , new_user ) : <EOL> new_profile = UserProfile ( user = new_user , name = self . cleaned_data [ '<STR_LIT:name>' ] , <EOL> city = self . cleaned_data [ '<STR_LIT>' ] , <EOL> country = self . cleaned_data [ '<STR_LIT>' ] , <EOL> organization = self . cleaned_data [ '<STR_LIT>' ] , <EOL> home_page = self . cleaned_data [ '<STR_LIT>' ] , <EOL> twitter = self . cleaned_data [ '<STR_LIT>' ] ) <EOL> new_profile . save ( ) <EOL> return new_profile <EOL> class RegistrationFormUserProfile ( RegistrationFormUniqueEmail , <EOL> UserProfileFormRegister ) : <EOL> class Meta : <EOL> model = User <EOL> fields = ( '<STR_LIT:username>' , '<STR_LIT:email>' ) <EOL> _reserved_usernames = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:data>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:wb>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> username = forms . CharField ( widget = forms . TextInput ( ) , max_length = <NUM_LIT:30> ) <EOL> email = forms . EmailField ( widget = forms . TextInput ( ) ) <EOL> legal_usernames_re = re . compile ( "<STR_LIT>" ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( UserProfileFormRegister , self ) . clean ( ) <EOL> if not self . REGISTRATION_REQUIRE_CAPTCHA : <EOL> if '<STR_LIT>' in self . _errors : <EOL> del self . _errors [ '<STR_LIT>' ] <EOL> return cleaned_data <EOL> response = captcha . submit ( <EOL> cleaned_data . get ( '<STR_LIT>' ) , <EOL> cleaned_data . get ( '<STR_LIT>' ) , <EOL> settings . RECAPTCHA_PRIVATE_KEY , <EOL> None ) <EOL> if not response . is_valid : <EOL> raise forms . ValidationError ( _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) ) <EOL> return cleaned_data <EOL> @ classmethod <EOL> def validate_username ( cls , username ) : <EOL> username = username . lower ( ) <EOL> if username in cls . _reserved_usernames : <EOL> raise forms . ValidationError ( <EOL> _ ( u'<STR_LIT>' ) % username ) <EOL> elif not cls . legal_usernames_re . search ( username ) : <EOL> raise forms . ValidationError ( <EOL> _ ( u'<STR_LIT>' <EOL> u'<STR_LIT>' ) ) <EOL> try : <EOL> User . objects . get ( username = username ) <EOL> except User . DoesNotExist : <EOL> return username <EOL> raise forms . ValidationError ( _ ( u'<STR_LIT>' ) % username ) <EOL> def clean_username ( self ) : <EOL> return self . validate_username ( self . cleaned_data [ '<STR_LIT:username>' ] ) <EOL> class SourceForm ( forms . Form ) : <EOL> source = forms . FileField ( label = ugettext_lazy ( u"<STR_LIT>" ) , <EOL> required = True ) <EOL> class SupportDocForm ( forms . Form ) : <EOL> doc = forms . FileField ( label = ugettext_lazy ( u"<STR_LIT>" ) , <EOL> required = True ) <EOL> class MediaForm ( forms . Form ) : <EOL> media = forms . FileField ( label = ugettext_lazy ( u"<STR_LIT>" ) , <EOL> required = True ) <EOL> def clean_media ( self ) : <EOL> data_type = self . cleaned_data [ '<STR_LIT>' ] . content_type <EOL> if data_type not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise forms . ValidationError ( '<STR_LIT>' ) <EOL> class MapboxLayerForm ( forms . Form ) : <EOL> map_name = forms . CharField ( widget = forms . TextInput ( ) , required = True , <EOL> max_length = <NUM_LIT:255> ) <EOL> attribution = forms . CharField ( widget = forms . TextInput ( ) , required = False , <EOL> max_length = <NUM_LIT:255> ) <EOL> link = forms . URLField ( label = ugettext_lazy ( u'<STR_LIT>' ) , <EOL> required = True ) <EOL> class QuickConverterFile ( forms . Form ) : <EOL> xls_file = forms . FileField ( <EOL> label = ugettext_lazy ( u'<STR_LIT>' ) , required = False ) <EOL> class QuickConverterURL ( forms . Form ) : <EOL> xls_url = forms . URLField ( label = ugettext_lazy ( '<STR_LIT>' ) , <EOL> required = False ) <EOL> class QuickConverterDropboxURL ( forms . Form ) : <EOL> dropbox_xls_url = forms . URLField ( <EOL> label = ugettext_lazy ( '<STR_LIT>' ) , required = False ) <EOL> class QuickConverterTextXlsForm ( forms . Form ) : <EOL> text_xls_form = forms . CharField ( <EOL> label = ugettext_lazy ( '<STR_LIT>' ) , required = False ) <EOL> class QuickConverter ( QuickConverterFile , QuickConverterURL , <EOL> QuickConverterDropboxURL , QuickConverterTextXlsForm ) : <EOL> validate = URLValidator ( ) <EOL> def publish ( self , user , id_string = None ) : <EOL> if self . is_valid ( ) : <EOL> if '<STR_LIT>' in self . cleaned_data and self . cleaned_data [ '<STR_LIT>' ] . strip ( ) : <EOL> csv_data = self . cleaned_data [ '<STR_LIT>' ] <EOL> import random <EOL> rand_name = "<STR_LIT>" % '<STR_LIT>' . join ( <EOL> random . sample ( "<STR_LIT>" , <NUM_LIT:6> ) ) <EOL> cleaned_xls_file = default_storage . save ( <EOL> upload_to ( None , rand_name , user . username ) , <EOL> ContentFile ( csv_data ) ) <EOL> else : <EOL> cleaned_xls_file = self . cleaned_data [ '<STR_LIT>' ] <EOL> if not cleaned_xls_file : <EOL> cleaned_url = self . cleaned_data [ '<STR_LIT>' ] <EOL> if cleaned_url . strip ( ) == u'<STR_LIT>' : <EOL> cleaned_url = self . cleaned_data [ '<STR_LIT>' ] <EOL> cleaned_xls_file = urlparse ( cleaned_url ) <EOL> cleaned_xls_file = '<STR_LIT:_>' . join ( cleaned_xls_file . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:2> : ] ) <EOL> if cleaned_xls_file [ - <NUM_LIT:4> : ] != '<STR_LIT>' : <EOL> cleaned_xls_file += '<STR_LIT>' <EOL> cleaned_xls_file = upload_to ( None , cleaned_xls_file , user . username ) <EOL> self . validate ( cleaned_url ) <EOL> xls_data = ContentFile ( urllib2 . urlopen ( cleaned_url ) . read ( ) ) <EOL> cleaned_xls_file = default_storage . save ( cleaned_xls_file , xls_data ) <EOL> return publish_xls_form ( cleaned_xls_file , user , id_string ) <EOL> class ActivateSMSSupportFom ( forms . Form ) : <EOL> enable_sms_support = forms . TypedChoiceField ( coerce = lambda x : x == '<STR_LIT:True>' , <EOL> choices = ( ( False , '<STR_LIT>' ) , <EOL> ( True , '<STR_LIT>' ) ) , <EOL> widget = forms . Select , <EOL> label = ugettext_lazy ( <EOL> u"<STR_LIT>" ) ) <EOL> sms_id_string = forms . CharField ( max_length = <NUM_LIT:50> , required = True , <EOL> label = ugettext_lazy ( u"<STR_LIT>" ) ) <EOL> def clean_sms_id_string ( self ) : <EOL> sms_id_string = self . cleaned_data . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> if not re . match ( r'<STR_LIT>' , sms_id_string ) : <EOL> raise forms . ValidationError ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> return sms_id_string <EOL> class ExternalExportForm ( forms . Form ) : <EOL> template_name = forms . CharField ( label = '<STR_LIT>' , max_length = <NUM_LIT:20> ) <EOL> template_token = forms . URLField ( label = '<STR_LIT>' , max_length = <NUM_LIT:100> ) </s>
