我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用wtforms.fields.StringField()。
def test_populate_errors_when_formfield(self): class SubMyForm(BaseForm): subattribute1 = StringField() class MyForm(BaseForm): attribute1 = FormField(SubMyForm) errors = {'attribute1': {'subattribute1': 'suberror1'}} with app.test_request_context(): form = MyForm() form.populate_errors(errors) assert_that(form, has_properties( attribute1=has_properties(form=has_properties( subattribute1=has_properties(errors=contains('suberror1')) )) ))
def test_populate_errors_when_fieldlist_of_formfield(self): class SubMyForm(BaseForm): subattribute1 = StringField() class MyForm(BaseForm): attribute1 = FieldList(FormField(SubMyForm), min_entries=2) errors = {'attribute1': {'1': {'subattribute1': 'suberror1'}}} with app.test_request_context(): form = MyForm() form.populate_errors(errors) assert_that(form, has_properties( attribute1=contains( has_properties(form=has_properties( subattribute1=has_properties(errors=empty()) )), has_properties(form=has_properties( subattribute1=has_properties(errors=contains('suberror1')) ))) ))
def convert_str(self, field, field_args): """ ???????????? ????????? ??? ???? Str :param field: ???? Str LightMagic :param field_args: ????????? WTF """ field_min = getattr(field, 'min_length', None) field_max = getattr(field, 'max_length', None) kwargs = {} if field_min is not None: kwargs['min'] = field_min if field_max is not None: kwargs['max'] = field_max if kwargs: field_args['validators'].append(validators.Length(**kwargs)) field_regex = getattr(field, 'regexp', None) if field_regex: field_args['validators'].append(validators.Regexp(regex=field_regex)) return wtforms_fields.StringField(**field_args)
def _create_dynamic_destination_form(self, destination): class DynamicForm(BaseForm): pass for key in destination[destination[self.select_field]]: setattr(DynamicForm, key, StringField()) options = dict(name=destination[self.select_field], prefix=self._prefix, translations=self.meta.get_translations(self)) field = self.meta.bind_field(self, FormField(DynamicForm), options) self._fields[destination[self.select_field]] = field self.added_dynamic_choice = (destination[self.select_field], destination[self.select_field])
def test_to_dict_with_csrf_token(self): class MyForm(BaseForm): attribute1 = StringField() csrf_token = StringField() with app.test_request_context(method='POST', data={'attribute1': 'value', 'csrf_token': '123-abcd'}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute1='value'))
def test_to_dict_with_submitfield(self): class MyForm(BaseForm): attribute1 = StringField() submit = SubmitField() with app.test_request_context(method='POST', data={'attribute1': 'value', 'submit': 'true'}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute1='value'))
def test_to_dict_with_default_and_empty_data(self): class MyForm(BaseForm): attribute1 = StringField(default='default') with app.test_request_context(method='POST', data={'attribute1': ''}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute1='default'))
def test_to_dict_with_default_and_data(self): class MyForm(BaseForm): attribute1 = StringField(default='default') with app.test_request_context(method='POST', data={'attribute1': 'value'}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute1='value'))
def test_to_dict_with_default_to_false_and_no_data(self): class MyForm(BaseForm): attribute1 = StringField(default=False) with app.test_request_context(): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute1=False))
def test_to_dict_with_no_data(self): class MyForm(BaseForm): attribute1 = StringField() with app.test_request_context(): form = MyForm() result = form.to_dict() assert_that(result, not_(has_key('attribute1')))
def test_to_dict_with_empty_data(self): class MyForm(BaseForm): attribute1 = StringField() with app.test_request_context(method='POST', data={'attribute1': ''}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute1=None))
def test_to_dict_with_formfield(self): class SubMyForm(BaseForm): subattribute = StringField() class MyForm(BaseForm): attribute = FormField(SubMyForm) with app.test_request_context(method='POST', data={'attribute-subattribute': 'subvalue'}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute=has_entries(subattribute='subvalue')))
def test_to_dict_with_listfield_of_fieldform(self): class SubMyForm(BaseForm): subattribute = StringField() class MyForm(BaseForm): attribute = FieldList(FormField(SubMyForm)) with app.test_request_context(method='POST', data={'attribute-0-subattribute': 'subvalue'}): form = MyForm() result = form.to_dict() assert_that(result, has_entries(attribute=contains(has_entries(subattribute='subvalue'))))
def test_populate_errors_when_error_not_match_field(self): class MyForm(BaseForm): attribute1 = StringField() errors = {'attribute2': 'error3'} with app.test_request_context(): form = MyForm() form.populate_errors(errors) assert_that(form, has_properties(attribute1=has_properties(errors=empty()))) assert_that(form, not_(has_property('attribute2')))
def test_populate_errors_when_errors_is_initialize_with_tuple(self): class MyForm(BaseForm): attribute1 = StringField() attribute2 = StringField() with app.test_request_context(): form = MyForm() form.attribute1.errors = tuple() errors = {'attribute1': 'error1'} form.populate_errors(errors) assert_that(form, has_properties( attribute1=has_properties(errors=instance_of(list)) ))
def conv_String(self, field_args, **extra): self._string_common(field_args=field_args, **extra) return wtforms_fields.StringField(**field_args)
def conv_MSYear(self, field_args, **extra): field_args['validators'].append(validators.NumberRange(min=1901, max=2155)) return wtforms_fields.StringField(**field_args)
def conv_PGInet(self, field_args, **extra): field_args.setdefault('label', 'IP Address') field_args['validators'].append(validators.IPAddress()) return wtforms_fields.StringField(**field_args)
def conv_PGMacaddr(self, field_args, **extra): field_args.setdefault('label', 'MAC Address') field_args['validators'].append(validators.MacAddress()) return wtforms_fields.StringField(**field_args)
def conv_PGUuid(self, field_args, **extra): field_args.setdefault('label', 'UUID') field_args['validators'].append(validators.UUID()) return wtforms_fields.StringField(**field_args)
def get_params_data(form_instance): result = [] type_mapping = \ { 'hudson.model.StringParameterDefinition': ('StringField', {}) , 'hudson.model.TextParameterDefinition': ('StringField', {}) , 'hudson.model.BooleanParameterDefinition': ('BooleanField', {}) , 'hudson.model.ChoiceParameterDefinition': ('SelectField', {}) } xml_config = get_config(form_instance) if not xml_config: return result tree = etree.fromstring(xml_config) params_root = tree.findall('properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions') if params_root: params = params_root[0].getchildren() for param in params: name = getattr(param.find('name'), 'text', None) if not name: continue field_type, kwargs = type_mapping.get(param.tag, None) if not type: continue choices = [(node.text, node.text) for node in param.findall('choices/a/string')] if choices: choices = [('', u'????????'), ] + choices result.append\ ( { 'name': name , 'type': field_type , 'description': getattr(param.find('description'), 'text', None) , 'default': getattr(param.find('defaultValue'), 'text', None) , 'choices': choices , 'kwargs': kwargs } ) return result
def jenkins_form_factory(*args, **kwargs): attributes = dict\ ( signature = fields.HiddenField\ () , view = fields.SelectField\ ( u'???' , choices=JenkinsViewChoices() , default=None ) , parent = InlineFieldList\ ( InlineFormField ( ParentForm ) , label=u'????????' , description=u'??????? ???????????? ?????' , validators= [ validators.DataRequired() , ] ) , node = fields.SelectField\ ( u'????' , choices=JenkinsNodeChoices() , validators= [ validators.DataRequired() , ] ) , config = fields.StringField\ ( u'???????? ??????' , widget=ReadonlyTextArea() ) ) form_instance = type('Form', (form.Form, ), attributes)(*args, **kwargs) if form_instance.parent.data: attributes['param'] = InlineFormField\ ( jenkins_param_form_factory(form_instance) , u'?????????' ) kwargs['csrf_enabled'] = False return type('Form', (form.Form, ), attributes)(*args, **kwargs)
def conv_String(self, model, field, kwargs): if field.regex: kwargs['validators'].append(validators.Regexp(regex=field.regex)) self._string_common(model, field, kwargs) password_field = kwargs.pop('password', False) textarea_field = kwargs.pop('textarea', False) or not field.max_length if password_field: return f.PasswordField(**kwargs) if textarea_field: return f.TextAreaField(**kwargs) return f.StringField(**kwargs)
def submit(db): # TODO: break this out along with others in to an excuses package. class SubmissionForm(Form): attribution_name = StringField( 'Your Name (for future attribution purposes)', [ validators.InputRequired(), validators.Length( min=3, max=50, message="Srsly, give us a decent username " "(%(min)d - %(max)d chars)," " doesn't even have to be real." ) ] ) excuse = TextAreaField( 'What\'s YOUR excuse !?!?', [ validators.Length( min=5, max=140, message="Please provide %(min)d - %(max)d " "characters"), ] ) nocaptcha = NoCaptchaField( public_key=settings.RECAPTCHA_SITE_KEY, private_key=settings.RECAPTCHA_SECRET_KEY, secure=True, ) form = SubmissionForm(request.POST, nocaptcha={'ip_address': '127.0.0.1'}) submitted = False if request.method == 'POST' and form.validate(): excuse_record = Excuse(form.attribution_name.data, form.excuse.data) db.add(excuse_record) submitted = True return template('submit', form=form, submitted=submitted)