def test_boolean_field(self): f = BooleanField() for item in [1, "1", "true", "True", True]: self.assertEqual(f.validate(item), True) for item in [0, "0", "false", "False", False]: self.assertEqual(f.validate(item), False) for item in [2, "2", {}]: self.assertRaises(ValidationError, f.validate, item)
def add_fields(self, form, index): """A hook for adding extra fields on to each form instance.""" if self.can_order: # Only pre-fill the ordering field for initial forms. if index < self.initial_form_count(): form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_(u'Order'), initial=index+1, required=False) else: form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_(u'Order'), required=False) if self.can_delete: form.fields[DELETION_FIELD_NAME] = BooleanField(label=_(u'Delete'), required=False)
class User(Model): __table__ = 'users' id = StringField(primary_key=True, default=next_id, ddl='varchar(50)') email = StringField(ddl='varchar(50)') passwd = StringField(ddl='varchar(50)') admin = BooleanField() name = StringField(ddl='varchar(50)') image = StringField(ddl='varchar(500)') created_at = FloatField(default=time.time)
class AccountForm(Form): from_address = CharField() host_name = CharField() port = IntegerField() max_size = IntegerField() max_size_type = CharField() use_auth = BooleanField(required=False) username = CharField(required=False) password = CharField(required=False) security = CharField()
def add_fields(self, form, index): """Using the dojango form fields instead of the django ones""" is_dojo_1_0 = getattr(self, "_total_form_count", False) if self.can_order: # Only pre-fill the ordering field for initial forms. # before django 1.1 _total_form_count was used! if index < (is_dojo_1_0 and self._total_form_count or self.total_form_count()): form.fields[ORDERING_FIELD_NAME] = IntegerField( label=_(u'Order'), initial=index + 1, required=False) else: form.fields[ORDERING_FIELD_NAME] = IntegerField( label=_(u'Order'), required=False) if self.can_delete: form.fields[DELETION_FIELD_NAME] = BooleanField(label=_(u'Delete'), required=False)
def add_fields(self, form, index): """A hook for adding extra fields on to each form instance.""" if self.can_order: # Only pre-fill the ordering field for initial forms. if index < self._initial_form_count: form.fields[ORDERING_FIELD_NAME] = DecimalField( ORDERING_FIELD_NAME, label='Order', initial=index + 1, required=False) else: form.fields[ORDERING_FIELD_NAME] = DecimalField( ORDERING_FIELD_NAME, label='Order', required=False) if self.can_delete: form.fields[DELETION_FIELD_NAME] = BooleanField( form.add_prefix(DELETION_FIELD_NAME), label='Delete', required=False, title='unchecked')
class User(Model): name = StringField(required=True) description = StringField() date_added = DateField() age = IntField() coins = FloatField() is_superuser = BooleanField() def __str__(self): return 'User {}'.format(self.name, self.age) def __repr__(self): return '<User {}>'.format(self.name, self.age) def update(self): pass class Meta: table_name = 'ormtable' order_by = ('name', )
def build_field_row(self, field, errors=None, for_javascript=False): if for_javascript: label_str = REPLACE_ME_WITH_LABEL + ':' else: label_str = self.get_label_name(field) negation_field = BooleanField('negation|' + field.name, field.negation) if for_javascript: # Needed for detecting presence of fields such as checkboxes # and multiple selects, because they do not send data if they # are not checked or no option is selected. presention_field = HiddenField('presention|' + field.name, 'on') else: # Dtto. presention_field = HiddenField('presention|' + field.name, '%03d' % self.field_counter) self.field_counter += 1 if not isinstance(field, CompoundFilterField): return notag(td(label_str), td(presention_field, errors, field), td(negation_field, 'NOT'))
def test_fields(self): f = BaseField(name='test', model=MockModel(), unique=True) data = f.validate(data=None, _id='507f1f77bcf86cd799439011') self.assertIsNone(data) model = MockModel(count=10) with self.assertRaises(Exception): f = BaseField(name='test', model=model, unique=True) f.validate(data=None, _id='507f1f77bcf86cd799439011') f = AutoBaseField(name='test', model=MockModel()) f.get_original('507f1f77bcf86cd799439011') with self.assertRaises(NotImplementedError): f.set(None) with self.assertRaises(Exception): f.validate(None) s = StringField('test', model=MockModel()) data = s.validate('test') self.assertEqual(data, 'test') with self.assertRaises(Exception): s.validate(None) dt = DateTimeField('test', model=MockModel()) dt.validate('2018-12-10 15:00:00.123') with self.assertRaises(Exception): dt.validate('invalid') nf = NumberField('test', model=MockModel()) data = nf.validate(10) self.assertEqual(data, 10) with self.assertRaises(Exception): nf.validate('test') af = AutoField('test', model=MockModel()) val = af.set() self.assertEqual(val, 1) self.assertEqual(af.set(_id='507f1f77bcf86cd799439011'), {}) adt = AutoDateTimeField('test', model=MockModel(), create_only=True) today = datetime.now().today() time = adt.set() self.assertEqual(today.date(), time.date()) new_time = adt.set(_id='507f1f77bcf86cd799439011') self.assertEqual(new_time, {}) bf = BooleanField('test', model=MockModel()) data = bf.validate(True) self.assertTrue(data) with self.assertRaises(Exception): bf.validate('test') fk = ForeignKeyField('test', model=MockModel(), to='test') with self.assertRaises(Exception): fk.validate('507f1f77bcf86cd799439011')