class Book(BaseRecord): title = StringField(size=300) pub_date = DateTimeField() avail_since = DateTimeField(auto_now_add=True) abstract = StringField(size=1000) isbn = StringField(size=50) author = ManyToOneRelation(Author, backref="books")
def load_field_info(self, field_info): field_datatype = field_info["Type"] is_key = field_info['Key'] in ('P', 'U') field_name = field_info['Field'] if field_datatype == "NUMBER" and (field_info['Precision'] == 0 or field_info['Precision'] == None): return IntField(field_name, is_key=is_key) elif field_datatype == 'NUMBER' and field_info['Precision'] > 0: return DecimalField(field_name, is_key=is_key) elif field_datatype == 'NVARCHAR2': return StringField(field_name, is_key=is_key) elif field_datatype == 'VARCHAR2': return StringField(field_name, is_key=is_key) elif field_datatype == 'FLOAT': return DecimalField(field_name, is_key=is_key) elif field_datatype == 'CHAR': return StringField(field_name, is_key=is_key) elif field_datatype == 'TIMESTAMP(6)': return DatetimeField(field_name, is_key=is_key) elif field_datatype == 'CLOB': return StringField(field_name, is_key=is_key) elif field_datatype == 'DATE': return DatetimeField(field_name, is_key=is_key) else: raise Exception('Unsupportted type ' + field_datatype)
class Person(Model): __table__ = 'person' id = AutoPrimaryField(length=11) last_name = StringField(length=255) first_name = StringField(length=255) address = StringField(length=255) city = StringField(length=255)
class Comment(Model): __table__ = 'comments' id = StringField(primary_key=True, default=next_id, ddl='varchar(50)') blog_id = StringField(ddl='varchar(50)') user_id = StringField(ddl='varchar(50)') user_name = StringField(ddl='varchar(50)') user_image = StringField(ddl='varchar(500)') content = TextField() created_at = FloatField(default=time.time)
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)
def __init__(self): super().__init__() self.status = ForeignKeyField(title='Статус', col_name='status', target_model_class=LogStatusModel, target_fields=(('name', 'Статус'),)) self.logged_table_name = StringField(col_name='table_name', title='Таблица') self.logged_table_pk = IntegerField(col_name='table_pk', title='Ключ') # TODO: fix creation self.datetime = TimestampField(col_name='change_time', title='Время')
class MultiLevelFieldGroup(BaseFieldGroup): cls = MultiLevelData cls_ctor_args = {} key2field = { "pos": Coord3DFieldGroup(), "direction": Vector3DFieldGroup(), "info": StringField(size=200) }
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', )
class Blog(Model): __table__ = 'blogs' id = StringField(primary_key=True, default=next_id, ddl='varchar(50)') user_id = StringField(ddl='varchar(50)') user_name = StringField(ddl='varchar(50)') user_image = StringField(ddl='varchar(500)') name = StringField(ddl='varchar(50)') summary = StringField(ddl='varchar(200)') content = TextField() created_at = FloatField(default=time.time)
def __init__(self): super().__init__() self.name = StringField(title='Предмет', col_name='name')
class Address(BaseRecord): author = OneToOneRelation(Author, backref="address") city = StringField(size=100) street = StringField(size=100) street_no = IntegerField() postal_code = IntegerField()
class Grade(Model): __table__ = 'grade' id = AutoPrimaryField(length=11) name = StringField(length=255)
class Author(BaseRecord): name = StringField(size=50) family_name = StringField(size=50) avail_since = DateTimeField(auto_now_add=True) birthday = DateTimeField()
def __init__(self): super().__init__() self.name = StringField(title='Группа', col_name='name')
def __init__(self): super().__init__() self.name = StringField(col_name='name', title='Название')
class Man(User): sex = StringField() class Meta: table_name = 'Man' order_by = ('-name', 'coins', 'sex')
class MyModel(BaseRecord): word = StringField(size=40)
def __init__(self): super().__init__() self.name = StringField('Название', col_name='name') self.order_number = IntegerField('Порядковый номер', col_name='order_number')
def __init__(self): super().__init__() self.name = StringField('Статус', col_name='name')
def prepare_query_value(self, op, value): if isinstance(value, basestring): from fields import StringField return StringField().prepare_query_value(op, value) return self.to_es(value)
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')