def parse_phone(self, txt): f = usforms.USPhoneNumberField() try: return f.clean(txt) except: return None
class ActiveDirectoryUserProfileForm(forms.Form): required_css_class = 'required' alternate_email = forms.EmailField( label='Alternate Email Address (voting)', required=False, error_messages=ALTERNATE_EMAIL_MESSAGES) phone = us_forms.USPhoneNumberField(label="Mobile Phone (voting)", required=False, error_messages=PHONE_MESSAGES) tshirt = forms.ModelChoiceField(label='T-Shirt Size', error_messages=SHIRT_MESSAGES, queryset=Tshirt.objects.all()) diet = forms.ModelChoiceField(label='Dietary Preference', error_messages=DIET_MESSAGES, queryset=Diet.objects.all()) location = forms.ModelChoiceField(label='Location', error_messages=LOCATION_MESSAGES, queryset=Location.objects.all()) description = forms.CharField(label='Describe Yourself', widget=forms.Textarea, required=False) # notes = forms.CharField(label='Mission Notes', # widget=forms.Textarea, required=False) notify_by_email = forms.BooleanField( label="Email me when there's a new post", required=False) dinner_required = forms.BooleanField( label="I will be staying for dinner on Thursday", required=False) breakfast_required = forms.BooleanField( label="I will be having breakfast on Friday", required=False)
class ContactEditorForm(forms.ModelForm): phone_number = us_forms.USPhoneNumberField(required=False) state = us_forms.USStateField(widget=us_forms.USStateSelect, required=False) zip_code = us_forms.USZipCodeField(label='ZIP Code', required=False) class Meta: model = Contact exclude = ('user', )
class InquiryForm(forms.ModelForm): class Meta: model = Applicant fields = ('fname', 'lname', 'mname', 'sex', 'bday', 'street', 'city', 'state', 'zip', 'parent_email', 'family_preferred_language', 'siblings', 'year', 'ethnicity', 'hs_grad_yr', 'hs_grad_yr', 'religion', 'country_of_birth', 'heard_about_us', 'present_school_typed', 'present_school_type_typed') ethnicity_other = forms.CharField(required=False) language_other = forms.CharField(required=False) religion_other = forms.CharField(required=False) # Parent p_lname = forms.CharField(required=False) p_fname = forms.CharField(required=False) p_relationship_to_child = forms.ChoiceField(required=False, choices=(('Mother', 'Mother'), ('Father', 'Father'), ('Guardian', 'Guardian'))) p_address = forms.CharField(required=False) p_city = forms.CharField(required=False) p_state = us_forms.USStateField(required=False, widget=forms.Select(choices=STATE_CHOICES)) p_zip = forms.CharField(required=False) p_home = us_forms.USPhoneNumberField(required=False) p_work = us_forms.USPhoneNumberField(required=False) p_work_ext = forms.CharField( required=False, widget=forms.TextInput(attrs={'style': 'width:3em;'})) p_mobile = us_forms.USPhoneNumberField(required=False) p_email = forms.EmailField(required=False) spam_regex = re.compile(r'^[5\-]+$') spam = forms.CharField(required=True, validators=[RegexValidator(regex=spam_regex)])
class ContactForm(BotScoutForm, forms.Form): name = forms.CharField(label=_('Name'), max_length=255, required=True) email = forms.EmailField(label=_('Email'), max_length=255, required=False) phone = us_forms.USPhoneNumberField(label=_('Phone #'), required=False) comments = forms.CharField(label=_('Comments'), required=True, widget=forms.Textarea) def clean(self): email = self.cleaned_data.get('email', '').strip() phone = self.cleaned_data.get('phone', '').strip() if not any([email, phone]): raise forms.ValidationError('You must provide either an email ' 'address or phone number.') return super(ContactForm, self).clean()
class UserProfileForm(forms.Form): first_name = forms.CharField(label='First Name') last_name = forms.CharField(label='Last Name') email = forms.EmailField(label='Email Address') alternate_email = forms.EmailField(label='Alternate Email Address (voting)', required=False) phone = us_forms.USPhoneNumberField(label="Mobile Phone (voting)", required=False) tshirt = forms.ModelChoiceField(label='T-Shirt Size', queryset=Tshirt.objects.all()) diet = forms.ModelChoiceField(label='Dietary Preference', queryset=Diet.objects.all()) location = forms.ModelChoiceField(label='Location', queryset=Location.objects.all()) description = forms.CharField(label='Describe Yourself', widget=forms.Textarea, required=False) notify_by_email = forms.BooleanField( label="Email me when there's a new post", required=False) password = forms.CharField(label='Password', widget=forms.PasswordInput, required=False)
class SignUpForm(forms.Form): user_name = forms.SlugField(label='User Name', error_messages={'required': 'Please enter a user name.', 'invalid': 'Please enter a user name containing only letters, numbers, underscores, and hyphens.'}) password = forms.CharField(label='Password', widget=forms.PasswordInput) first_name = forms.CharField(label='First Name') last_name = forms.CharField(label='Last Name') email = forms.EmailField(label='Email Address') alternate_email = forms.EmailField(label='Alternate Email Address (voting)', required=False) phone = us_forms.USPhoneNumberField(label="Mobile Phone (voting)", required=False) tshirt = forms.ModelChoiceField(label='T-Shirt Size', queryset=Tshirt.objects.all()) diet = forms.ModelChoiceField(label='Dietary Preference', queryset=Diet.objects.all()) location = forms.ModelChoiceField(label='Location', queryset=Location.objects.all()) description = forms.CharField(label='Describe Yourself', widget=forms.Textarea, required=False) notify_by_email = forms.BooleanField( label="Email me when there's a new post", required=False)
class SignUpForm(forms.Form): required_css_class = 'required' user_name = forms.SlugField(label='User Name', error_messages=USERNAME_MESSAGES) password = forms.CharField(label='Password', widget=forms.PasswordInput, error_messages=PASSWORD_MESSAGES) first_name = forms.CharField(label='First Name', error_messages=FIRST_NAME_MESSAGES) last_name = forms.CharField(label='Last Name', error_messages=LAST_NAME_MESSAGES) email = forms.EmailField(label='Email Address', error_messages=EMAIL_MESSAGES) alternate_email = forms.EmailField( label='Alternate Email Address (voting)', required=False, error_messages=ALTERNATE_EMAIL_MESSAGES) phone = us_forms.USPhoneNumberField(label="Mobile Phone (voting)", required=False, error_messages=PHONE_MESSAGES) tshirt = forms.ModelChoiceField(label='T-Shirt Size', error_messages=SHIRT_MESSAGES, queryset=Tshirt.objects.all()) diet = forms.ModelChoiceField(label='Dietary Preference', error_messages=DIET_MESSAGES, queryset=Diet.objects.all()) location = forms.ModelChoiceField(label='Location', error_messages=LOCATION_MESSAGES, queryset=Location.objects.all()) description = forms.CharField(label='Describe Yourself', widget=forms.Textarea, required=False) notify_by_email = forms.BooleanField( label="Email me when there's a new post", required=False) dinner_required = forms.BooleanField( label="I will be staying for dinner on Thursday", required=False) breakfast_required = forms.BooleanField( label="I will be having breakfast on Friday", required=False)
def __init__(self, instance, show_fields=None, optional_fields=None, *args, **kwargs): self.instance = instance # an instance of UserProfile if show_fields is None: show_fields = [ 'email', 'name', 'phone_number', 'birth_date', 'image' ] # 'phone_number', if optional_fields is None: optional_fields = [ 'email', 'name', 'phone_number', 'birth_date', 'image' ] # 'phone_number', self.show_fields = show_fields self.optional_fields = optional_fields super(UserProfileForm, self).__init__(*args, **kwargs) email = self.instance.user.email.lower() is_fb_email = email.endswith('facebook.com') default_username = self.instance.user.username if is_sso_email(email) or is_fb_email: default_email = u'' else: default_email = email if self.instance.is_sso: default_username = self.instance.sso_username if True: # default_email or is_fb_email: self.fields['permission'] = forms.CharField( label=_('Who can see my profile'), initial=self.instance.permission, widget=forms.Select(choices=UserProfile.PERMISSION_CHOICES), ) self.fields['send_reminders'] = forms.BooleanField( initial=self.instance.send_reminders, label=_( "Email me daily reminders of upcoming events I am in for"), required=False) self.fields['send_favorites'] = forms.BooleanField( initial=self.instance.send_favorites, label= _("Email me daily reminders of upcoming events my friends are in for" ), required=False) if 'email' in show_fields: self.fields['email'] = forms.EmailField(label=_('Email'), initial=default_email, required='email' not in optional_fields) if not self.instance.is_sso and is_fb_email and ( self.instance.send_reminders or self.instance.send_favorites): self.fields[ 'email'].help_text = u'''Event reminder emails are being sent to your Facebook account.<br/> Add a different email address above or leave it empty to continue using your Facebook account.''' if 'name' in show_fields: self.fields['username'] = forms.RegexField( label=_("Username"), max_length=30, regex=r'^\w+$', initial=default_username, error_message= _("This value must contain only letters, numbers and underscores." ), min_length=4, help_text= _(u'Between 4 and 30 alphanumeric characters (letters, digits and underscores).' )) self.fields['first_name'] = forms.CharField( label=_('First name'), max_length=30, initial=self.instance.user.first_name, required='name' not in optional_fields) self.fields['last_name'] = forms.CharField( label=_('Last name'), max_length=30, initial=self.instance.user.last_name, required='name' not in optional_fields) if 'birth_date' in show_fields: self.fields['birth_date'] = forms.DateField( label=_('Date of birth'), input_formats=['%m/%d/%Y'], error_messages={ 'invalid': _('Enter a valid date in this format: MM/DD/YYYY.') }, help_text=_('In the format M/D/YYYY. For example, 8/21/1985.'), initial=self.instance.birth_date and self.instance.birth_date.strftime('%m/%d/%Y') or None, required='birth_date' not in optional_fields) if 'phone_number' in show_fields: self.fields['phone_number'] = us_forms.USPhoneNumberField( label=_('Phone number'), initial=self.instance.phone_number, help_text=_('In the format: xxx-xxx-xxxx.'), required='phone_number' not in optional_fields) if 'address' in show_fields: try: adr = self.instance.address except Address.DoesNotExist: adr = Address() self.fields['address1'] = forms.CharField(label=_('Address 1'), initial=adr.address1) self.fields['address2'] = forms.CharField(label=_('Address 2'), required=False, initial=adr.address2) self.fields['city'] = forms.CharField(label=_('City'), max_length=100, initial=adr.city) self.fields['state'] = us_forms.USStateField( label=_('State'), initial=adr.state, widget=us_forms.USStateSelect()) self.fields['postal_code'] = us_forms.USZipCodeField( label=_('ZIP code'), initial=adr.postal_code, help_text=_( 'U.S. zip code in the format XXXXX or XXXXX-XXXX.')) #self.fields['country'] = forms.CharField(label=_('Country'), max_length=75, initial=adr.country) #self.fields['country'].widget.attrs.update({'class':'textInput'}) if 'image' in show_fields: self.fields['image'] = forms.ImageField(label=_("Profile image"), required=False, help_text=AVATAR_HELP_TEXT) if 'name' in show_fields: self.fields['has_opted_in'] = forms.BooleanField( initial=self.instance.has_opted_in, label=_('Receive e-mail with news about %s.' % settings.UI_SETTINGS['UI_SITE_TITLE']), required=False)
class UserProfile(forms.ModelForm): phone = us.USPhoneNumberField(required=False) class Meta: model = models.UserProfile exclude = ('user', )
class PlaceForm(forms.ModelForm): phone = us_forms.USPhoneNumberField(required=False) class Meta: model = Place
class ArtistProfileUpdateForm(ArtistPaymentSetupForm, AvatarImageCleaner): """Build an artist profile update form. Artist profile requires contact information and band related information. It also requires payment setup information. """ # Artist/Band fields name = forms.CharField(label=_('Artist / Band name'), max_length=64) name.widget.attrs.update({'class': 'textInput'}) # num_members = forms.IntegerField(label=_('Number of band members'), min_value=1) # num_members.widget.attrs.update({'class':'textInput'}) url = forms.RegexField( label=_('IlliusRock URL'), regex=r"""^[a-zA-Z0-9\-]+$""", max_length=25, help_text=_( 'If you enter "pink-floyd" as your url, your public homepage will be at "%spink-floyd". Only English letters, numbers, and dashes are allowed.' % DISPLAY_SITE_URL)) url.widget.attrs.update({'class': 'textInput'}) website = forms.URLField(label=_('Artist / Band website'), required=False, verify_exists=True, max_length=150, help_text=ArtistProfile.WEBSITE_HELP_TEXT) website.widget.attrs.update({'class': 'textInput'}) genres = forms.MultipleChoiceField( label=_('Genres'), choices=_GENRE_CHOICES, help_text=_( 'Ctrl-click or Option-click if you wish to select multiple genres.' )) genres.widget.attrs.update({'class': 'multiselectInput'}) # Name and contact fields phone_number = us_forms.USPhoneNumberField( label=_('Phone number'), help_text=_('In the format: XXX-XXX-XXXX.')) phone_number.widget.attrs.update({'class': 'textInput'}) phone_number.pre_html = _( 'Please provide contact info for yourself or your band:') first_name = forms.CharField(label=_('First name'), max_length=30) first_name.widget.attrs.update({'class': 'textInput'}) last_name = forms.CharField(label=_('Last name'), max_length=30) last_name.widget.attrs.update({'class': 'textInput'}) address1 = forms.CharField(label=_('Address 1'), required=False) address1.widget.attrs.update({'class': 'textInput'}) address2 = forms.CharField(label=_('Address 2'), required=False) address2.widget.attrs.update({'class': 'textInput'}) city = forms.CharField(label=_('City'), max_length=100, required=False) city.widget.attrs.update({'class': 'textInput'}) state = us_forms.USStateField(label=_('State'), widget=us_forms.USStateSelect(), required=False) state.widget.attrs.update({'class': 'textInput selectWider'}) postal_code = us_forms.USZipCodeField( label=_('ZIP code'), required=False, help_text=_('U.S. zip code in the format XXXXX or XXXXX-XXXX.')) postal_code.widget.attrs.update({'class': 'textInput'}) image = forms.ImageField(label=_("Profile image"), required=False, help_text=AVATAR_HELP_TEXT) image.widget.attrs.update({'class': 'filebrowserInput'}) has_opted_in = forms.BooleanField( label=_('Receive e-mails about upcoming campaigns and news about %s.' % settings.UI_SETTINGS['UI_SITE_TITLE']), required=False) has_opted_in.widget.attrs.update({'class': 'checkboxInput'}) class Media: _append = "?v=%s" % settings.UI_SETTINGS['UI_JS_VERSION'] js = (settings.ADMIN_MEDIA_PREFIX + 'js/urlify.js' + _append, ) def __init__(self, *args, **kwargs): user_profile_instance = kwargs.pop('user_profile_instance', None) self.user_profile_instance = user_profile_instance if not user_profile_instance: super(ArtistProfileUpdateForm, self).__init__(*args, **kwargs) else: # Gather initial values based on existing data. artist_profile = self.user_profile_instance.artist if artist_profile: if artist_profile: initial_genres = [ g.id for g in artist_profile.genres.all() ] else: artist_profile = ArtistProfile() initial_genres = [] else: artist_profile = ArtistProfile() initial_genres = [] try: adr = self.user_profile_instance.address except Address.DoesNotExist: adr = Address() # Populate initial values. initial = {} initial.update(self.user_profile_instance.user.__dict__) initial.update(self.user_profile_instance.__dict__) initial.update(adr.__dict__) initial.update(artist_profile.__dict__) initial['genres'] = initial_genres if artist_profile.paypal: initial.update(artist_profile.paypal.__dict__) initial.update( {'paypal_email2': artist_profile.paypal.paypal_email}) if artist_profile.google: initial.update(artist_profile.google.__dict__) initial.update(kwargs.pop('initial', {})) super(ArtistProfileUpdateForm, self).__init__(initial=initial, *args, **kwargs) if artist_profile.paypal: self.fields[ 'paypal_email'].help_text = _PAYPAL_EMAIL_HELP_WITH_LINKS if artist_profile.google: self.fields['google_merchant_id'].help_text = get_gco_help() def clean_name(self): return force_unicode(strip_tags(self.cleaned_data['name'])) def clean_first_name(self): return force_unicode(strip_tags(self.cleaned_data['first_name'])) def clean_last_name(self): return force_unicode(strip_tags(self.cleaned_data['last_name'])) def clean_url(self): url = self.cleaned_data['url'].lower() if not is_artist_url_available( url, getattr(self, 'user_profile_instance', None)): raise forms.ValidationError( _('That url is not available. Please try another.')) return url def save(self, profile=None, commit=True): if not profile: profile = self.user_profile_instance avatar_img = self.cleaned_data.get('image', None) if avatar_img: avatar_img_field = profile._meta.get_field('avatar_image') avatar_img_field.save_form_data(profile, avatar_img) artist_profile = self.save_artist_profile(profile, commit=commit) ArtistPaymentSetupForm.save(self, artist_profile=artist_profile, commit=commit) if avatar_img: profile._create_resized_images(raw_field=None, save=commit) return artist_profile def save_artist_profile(self, profile, commit=True): """`profile` is an instance of UserProfile""" user = profile.user profile.is_artist = True profile.phone_number = self.cleaned_data['phone_number'] if 'has_opted_in' in self.fields: profile.has_opted_in = self.cleaned_data.get('has_opted_in', False) if commit: profile.save() user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] if commit: user.save() adr1 = self.cleaned_data.get('address1', '') city = self.cleaned_data.get('city', '') state = self.cleaned_data.get('state', '') postal_code = self.cleaned_data.get('postal_code', '') if adr1 and city and state and postal_code: adr = Address(user_profile=profile) adr.address1 = adr1 adr.address2 = self.cleaned_data.get('address2', '') adr.city = city adr.state = state adr.postal_code = postal_code if commit: adr.save() artist_profile = ArtistProfile(user_profile=profile) artist_profile.name = self.cleaned_data['name'] artist_profile.num_members = self.cleaned_data.get('num_members', 2) artist_profile.url = self.cleaned_data['url'] artist_profile.website = self.cleaned_data.get('website', None) if commit: artist_profile.save() artist_profile.genres = list( Genre.objects.filter(id__in=self.cleaned_data['genres'])) return artist_profile