mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Support association proxies
This commit is contained in:
parent
ef3fb2f536
commit
391fd1c1ac
2 changed files with 69 additions and 32 deletions
|
@ -1337,8 +1337,21 @@ class PokemonForm(TableBase):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
"""Name of this form: the form_name, if set; otherwise the species name"""
|
||||||
return self.pokemon_name or self.species.name
|
return self.pokemon_name or self.species.name
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _add_relationships(add_relationship, add_association_proxy, **kwargs):
|
||||||
|
add_relationship('pokemon', Pokemon,
|
||||||
|
primaryjoin=PokemonForm.pokemon_id==Pokemon.id,
|
||||||
|
innerjoin=True, lazy='joined')
|
||||||
|
add_association_proxy('species', 'pokemon', 'species')
|
||||||
|
add_relationship('version_group', VersionGroup,
|
||||||
|
innerjoin=True)
|
||||||
|
add_relationship('pokeathlon_stats', PokemonFormPokeathlonStat,
|
||||||
|
order_by=PokemonFormPokeathlonStat.pokeathlon_stat_id,
|
||||||
|
backref='pokemon_form')
|
||||||
|
|
||||||
create_translation_table('pokemon_form_names', PokemonForm, 'names',
|
create_translation_table('pokemon_form_names', PokemonForm, 'names',
|
||||||
relation_lazy='joined',
|
relation_lazy='joined',
|
||||||
form_name = Column(Unicode(32), nullable=True, index=True,
|
form_name = Column(Unicode(32), nullable=True, index=True,
|
||||||
|
@ -1818,7 +1831,19 @@ def add_relationships():
|
||||||
argument.relationship_info[backref_name] = backref_info
|
argument.relationship_info[backref_name] = backref_info
|
||||||
setattr(cls, name, relationship(argument, secondary=secondary,
|
setattr(cls, name, relationship(argument, secondary=secondary,
|
||||||
**kwargs))
|
**kwargs))
|
||||||
add_relationships(add_relationship=add_relationship)
|
def add_association_proxy(name, target_collection, attr, **kwargs):
|
||||||
|
cls.relationship_info.setdefault('_order', [])
|
||||||
|
cls.relationship_info['_order'].append(name)
|
||||||
|
cls.relationship_info[name] = info = kwargs.pop('info', {})
|
||||||
|
info.update(kwargs)
|
||||||
|
info.update(dict(
|
||||||
|
type='association_proxy',
|
||||||
|
target_collection=target_collection,
|
||||||
|
attr=attr))
|
||||||
|
setattr(cls, name, association_proxy(name, target_collection,
|
||||||
|
**kwargs))
|
||||||
|
add_relationships(add_relationship=add_relationship,
|
||||||
|
add_association_proxy=add_association_proxy)
|
||||||
add_relationships()
|
add_relationships()
|
||||||
|
|
||||||
Ability.changelog = relationship(AbilityChangelog,
|
Ability.changelog = relationship(AbilityChangelog,
|
||||||
|
|
|
@ -79,8 +79,10 @@ def column_header(c, class_name=None, transl_name=None, show_type=True,
|
||||||
for fk in c.foreign_keys:
|
for fk in c.foreign_keys:
|
||||||
if fk.column in column_to_cls:
|
if fk.column in column_to_cls:
|
||||||
foreign_cls = column_to_cls[fk.column]
|
foreign_cls = column_to_cls[fk.column]
|
||||||
if relation_name:
|
if relation_name and relation_name + '_id' == c.name:
|
||||||
result.append(u'(%s →' % c.name)
|
result.append(u'(%s →' % c.name)
|
||||||
|
elif relation_name:
|
||||||
|
result.append(u'(**%s** →' % c.name)
|
||||||
else:
|
else:
|
||||||
result.append(u'(→')
|
result.append(u'(→')
|
||||||
result.append(u':class:`~pokedex.db.tables.%s`.%s)' % (
|
result.append(u':class:`~pokedex.db.tables.%s`.%s)' % (
|
||||||
|
@ -168,8 +170,8 @@ def generate_columns(cls, remaining_attrs):
|
||||||
relation_name = c.name[:-3]
|
relation_name = c.name[:-3]
|
||||||
if c.name.endswith('_id') and relation_name in remaining_attrs:
|
if c.name.endswith('_id') and relation_name in remaining_attrs:
|
||||||
relation = getattr(cls, relation_name)
|
relation = getattr(cls, relation_name)
|
||||||
yield (column_header(c, name,
|
yield column_header(c, name,
|
||||||
relation=relation, relation_name=relation_name) + ':')
|
relation=relation, relation_name=relation_name)
|
||||||
remaining_attrs.remove(relation_name)
|
remaining_attrs.remove(relation_name)
|
||||||
else:
|
else:
|
||||||
yield column_header(c, name) + ':'
|
yield column_header(c, name) + ':'
|
||||||
|
@ -187,7 +189,7 @@ def generate_strings(cls, remaining_attrs):
|
||||||
if c.name in common_columns:
|
if c.name in common_columns:
|
||||||
continue
|
continue
|
||||||
yield column_header(c, cls.__name__,
|
yield column_header(c, cls.__name__,
|
||||||
translation_class.__table__.name) + ':'
|
translation_class.__table__.name)
|
||||||
yield u''
|
yield u''
|
||||||
yield u' ' + unicode(c.info['description'])
|
yield u' ' + unicode(c.info['description'])
|
||||||
yield u''
|
yield u''
|
||||||
|
@ -204,6 +206,7 @@ def generate_relationships(cls, remaining_attrs):
|
||||||
for rel_name, info in infos:
|
for rel_name, info in infos:
|
||||||
if rel_name in remaining_attrs:
|
if rel_name in remaining_attrs:
|
||||||
info = cls.relationship_info.get(rel_name)
|
info = cls.relationship_info.get(rel_name)
|
||||||
|
if info['type'] in ('relationship', 'backref'):
|
||||||
yield u'%s.\ **%s**' % (cls.__name__, rel_name)
|
yield u'%s.\ **%s**' % (cls.__name__, rel_name)
|
||||||
class_name = u':class:`~pokedex.db.tables.%s`' % info['argument'].__name__
|
class_name = u':class:`~pokedex.db.tables.%s`' % info['argument'].__name__
|
||||||
if info.get('uselist', True):
|
if info.get('uselist', True):
|
||||||
|
@ -231,6 +234,15 @@ def generate_relationships(cls, remaining_attrs):
|
||||||
yield u' '
|
yield u' '
|
||||||
yield ' Ordered by: ' + u', '.join(
|
yield ' Ordered by: ' + u', '.join(
|
||||||
u'``%s``' % o for o in order)
|
u'``%s``' % o for o in order)
|
||||||
|
elif info['type'] == 'association_proxy':
|
||||||
|
yield u'%s.\ **%s**:' % (cls.__name__, rel_name)
|
||||||
|
yield '``{info[attr]}`` of ``self.{info[target_collection]}``'.format(
|
||||||
|
info=info)
|
||||||
|
if 'description' in info:
|
||||||
|
yield u''
|
||||||
|
yield u' ' + unicode(info['description'])
|
||||||
|
else:
|
||||||
|
continue
|
||||||
yield u''
|
yield u''
|
||||||
remaining_attrs.remove(rel_name)
|
remaining_attrs.remove(rel_name)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue