mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Give every db table a __str__.
This commit is contained in:
parent
b61acaff69
commit
a0f5c53193
1 changed files with 21 additions and 12 deletions
|
@ -56,25 +56,34 @@ class TableMetaclass(DeclarativeMeta):
|
||||||
if hasattr(cls, '__tablename__'):
|
if hasattr(cls, '__tablename__'):
|
||||||
table_classes.append(cls)
|
table_classes.append(cls)
|
||||||
|
|
||||||
metadata = MetaData()
|
class TableSuperclass(object):
|
||||||
TableBase = declarative_base(metadata=metadata, metaclass=TableMetaclass)
|
"""Superclass for declarative tables, to give them some generic niceties
|
||||||
|
like stringification.
|
||||||
### Helper classes
|
"""
|
||||||
# XXX this stuff isn't covered anywhere; maybe put it in TableBase??
|
|
||||||
class Named(object):
|
|
||||||
"""Mixin for objects that have names"""
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
|
"""Be as useful as possible. Show the primary key, and an identifier
|
||||||
|
if we've got one.
|
||||||
|
"""
|
||||||
|
typename = u'.'.join((__name__, type(self).__name__))
|
||||||
|
|
||||||
|
pk_constraint = self.__table__.primary_key
|
||||||
|
if not pk_constraint:
|
||||||
|
return u"<%s object at %x>" % (typename, id(self))
|
||||||
|
|
||||||
|
pk = u', '.join(unicode(getattr(self, column.name))
|
||||||
|
for column in pk_constraint.columns)
|
||||||
try:
|
try:
|
||||||
return '<%s: %s>' % (type(self).__name__, self.identifier)
|
return u"<%s object (%s): %s>" % (typename, pk, self.identifier)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return '<%s>' % type(self).__name__
|
return u"<%s object (%s)>" % (typename, pk)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return unicode(self).encode('utf-8')
|
return unicode(self).encode('utf8')
|
||||||
|
|
||||||
def __repr__(self):
|
metadata = MetaData()
|
||||||
return str(self)
|
TableBase = declarative_base(metadata=metadata, cls=TableSuperclass, metaclass=TableMetaclass)
|
||||||
|
|
||||||
|
### Helper classes
|
||||||
class LanguageSpecific(object):
|
class LanguageSpecific(object):
|
||||||
"""Mixin for prose and text tables"""
|
"""Mixin for prose and text tables"""
|
||||||
@declared_attr
|
@declared_attr
|
||||||
|
|
Loading…
Reference in a new issue