def test_python_value(self): field = fields.IDField() p_v = field.python_value(100) self.assertEqual(p_v, '100')
def __new__(mcs, name, base, attrs): cls = super().__new__(mcs, name, base, attrs) if 'Meta' not in attrs: cls.Meta = None class Meta: fields = {} managers_map = {} def __init__(self, model_class): self.model_class = model_class self.collection_name = utils.convert_name( cls.__name__ ) self.abstract = False def get_id_field_name(self): for _name, field in self.fields.items(): if isinstance(field, fields.IDField): return _name def get_field(self, f_name): if f_name in self.fields: return self.fields[f_name] raise AttributeError('Field name %s not found' % f_name) def add_manager(self, manager): self.managers_map[manager.name] = manager def add_field(self, field): self.fields[field.name] = field def get_field_by_column_name(self, f_name): for field in self.fields.values(): if f_name in [field.name, field.db_column_name]: return field raise AttributeError('Field name %s not found' % f_name) def set_from_model_meta(self, model_meta): for m_name, m_val in model_meta.__dict__.items(): if m_name == 'collection_name': self.collection_name = m_val if m_name == 'abstract': self.abstract = m_val _meta = Meta(cls) setattr(cls, '_meta', _meta) for bc in base: if not bc._meta.abstract: if bc != Model: raise AttributeError( "Can't inherit from non abstract class" ) continue for name, attr in bc._meta.fields.items(): if isinstance(attr, fields.IDField): continue attr.contribute_to_class(cls, name) for name, attr in cls.__dict__.items(): if ( isinstance(attr, type) and name == 'Meta' ): _meta.set_from_model_meta(attr) if ( isinstance(attr, (managers.BaseManager, fields.Field)) ): if isinstance(attr, fields.IDField): raise AttributeError( "Manually added IDField is forbidden." "It will be created automatic" ) attr.contribute_to_class(cls, name) if 'objects' not in cls.__dict__: manager = managers.Manager() manager.contribute_to_class(cls, 'objects') if hasattr(cls, '__unicode__'): setattr(cls, '__repr__', lambda self: '<%s: %s>' % ( self.__class__.__name__, self.__unicode__())) if _meta.abstract: return cls pk = fields.IDField() pk.contribute_to_class(cls, 'id') return cls
def test_lookup_value(self): field = fields.IDField() db_v = field.lookup_value(None, 10) self.assertEqual(db_v, '10')