def populate_choices(self): """Populate choices for sector and category drop downs.""" self.sector.choices = Sector.list() cat_sector = 1 if self.sector.data is None else self.sector.data self.category.choices = Category.list(cat_sector) self.address.state.choices = State.list() return self
class AddressField(FlaskForm): """Form to capture user or business address. Fields: line1 (str): street address line 1 (i.e. 100 Main St) line2 (str): optional, 2nd line of street address (i.e. apt 3b) city (str): address city state (select): address state zip (str): 5 digit zip code Methods: validate_zip: check zip code, verifies 5 digits and only numbers in zip code """ line1 = StringField( "Street Address", default='', validators=[InputRequired(message="Street address is required.")]) line2 = StringField("Address Line 2", default='', validators=[Optional()]) city = StringField("City", default='', validators=[InputRequired(message="City is required.")]) state = SelectField( "State", choices=State.list(), coerce=int, default=0, validators=[InputRequired(message="State is required.")]) zip = StringField( "Zip", default='', validators=[InputRequired(message="Zip code is required.")]) def validate_zip(form, field): if len(field.data) != 5: raise ValidationError('Please enter a 5 digit zip code.') elif not field.data.isdigit(): raise ValidationError('Only include numbers in zip code.') def populate_choices(self): self.state.choices = State.list() return self
def make_form(self, formDict): """Override FormTest make form to populate choices""" super().make_form(formDict) self.form.state.choices = State.list()
def populate_choices(self): self.state.choices = State.list() return self
def populate_choices(self): """Populate state drop down list for address field.""" self.address.state.choices = State.list() return self
def populate_choices(self, sector=None): """Populate choices for sector and category drop downs.""" self.sector.choices = Sector.list() self.category.choices = Category.list(sector) self.state.choices = State.list() return self
class ProviderSuggestionForm(FlaskForm): """Form to add new provider. Fields: name (str): name of business is_not_active (bool): indicates that business is no longer active sector (select): macro sector that business belongs to category_updated (bool): indicates change to category/sector category (select): categories (w/in sector) that business belongs to contact_info_updated (bool): indicates change to contact information email (str): optional, email address of business website (str): optional, website of business telephone (str): telephone number of business address_updated (bool): indicates change to address line1 (str): 1st address line line2 (str): optional, 2nd address line city (str): city name state (select): address state, select is combination of id,name zip (str): zip/postal code coordinate_error (bool): whether map coordinates match actual location other (str): additional comments Methods: validate_name: validator, checks that provider does not already exist by checking to see if another provider exists with same name and address """ id = IntegerField( "Business ID", validators=[InputRequired(message="Business ID is required.")]) name = StringField( "Business Name", validators=[InputRequired(message="Business name is required.")]) is_not_active = BooleanField("Is Not Active", validators=[AnyOf([True, False])], default=False, false_values=[None, False, 'false', 0, '0']) category_updated = BooleanField( "Category Updated", default=False, validators=[AnyOf([True, False])], false_values=[None, False, 'false', 0, '0']) sector = SelectField("Sector", choices=Sector.list(), coerce=int, id="suggestion_sector", validators=[requiredIf('category_updated')]) category = SelectMultipleField("Category", choices=Category.list(None), coerce=int, validators=[requiredIf('category_updated')], id="suggestion_category") contact_info_updated = BooleanField( "Contact Info Update", validators=[AnyOf([True, False])], default=False, false_values=[None, False, 'false', 0, '0']) email = StringField( "Email Address", validators=[Email(), Optional(), requiredIf('contact_info_updated')]) website = StringField( "Website", validators=[validate_website, requiredIf('contact_info_updated')]) telephone = StringField( "Telephone", validators=[ Regexp("[(]?[0-9]{3}[)-]{0,2}\s*[0-9]{3}[-]?[0-9]{4}"), requiredIf('contact_info_updated') ]) address_updated = BooleanField("Address Updated", validators=[AnyOf([True, False])], default=False, false_values=[None, False, 'false', 0, '0']) line1 = StringField("Street Address", validators=[requiredIf('address_updated')]) line2 = StringField("Address Line 2", validators=[Optional()]) city = StringField("City", validators=[requiredIf('address_updated')]) state = SelectField("State", choices=State.list(), coerce=int, validators=[requiredIf('address_updated')]) zip = StringField("Zip Code", validators=[validate_zip, requiredIf('address_updated')]) coordinate_error = BooleanField( "Coordinate_Error", validators=[Optional()], default=False, false_values=[None, False, 'false', 0, '0']) other = StringField("other", validators=[Optional()]) def validate_address_updated(self, address_updated): current = Provider.query.filter_by(id=self.id.data).first().address if address_updated: check = (self.line1.data == current.line1 and self.line2.data == current.line2 and self.city.data == current.city and self.state.data == current.state_id and self.zip.data == current.zip and self.coordinate_error.data is False) if check: raise ValidationError( "Address updated selected without any changes to address.") def validate_contact_info_updated(self, contact_info_updated): provider = Provider.query.filter_by(id=self.id.data).first() if contact_info_updated: if (self.telephone.data == provider.telephone and self.website.data == provider.website and self.email.data == provider.email): raise ValidationError( "Contact info updated selected without any changes to" " email, telephone or website.") def validate_category_updated(self, category_updated): provider = Provider.query.filter_by(id=self.id.data).first() cat_ids = [cat.id for cat in provider.categories] if category_updated and self.category.data == cat_ids: raise ValidationError( "Category updated selected without any changes to category or" " sector.") def populate_choices(self, sector=None): """Populate choices for sector and category drop downs.""" self.sector.choices = Sector.list() self.category.choices = Category.list(sector) self.state.choices = State.list() return self
def populate_choices(self): """Populate choices in state drop down.""" self.state.choices = State.list()