From 391fd1c1ac04a75bb8e8695bc841580ee9b3d4ad Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Sun, 12 Feb 2012 23:19:40 +0100 Subject: [PATCH] Support association proxies --- pokedex/db/tables.py | 27 ++++++++++++++- pokedex/doc/tabledoc.py | 74 ++++++++++++++++++++++++----------------- 2 files changed, 69 insertions(+), 32 deletions(-) diff --git a/pokedex/db/tables.py b/pokedex/db/tables.py index a955722..58329f6 100644 --- a/pokedex/db/tables.py +++ b/pokedex/db/tables.py @@ -1337,8 +1337,21 @@ class PokemonForm(TableBase): @property def name(self): + """Name of this form: the form_name, if set; otherwise the 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', relation_lazy='joined', form_name = Column(Unicode(32), nullable=True, index=True, @@ -1818,7 +1831,19 @@ def add_relationships(): argument.relationship_info[backref_name] = backref_info setattr(cls, name, relationship(argument, secondary=secondary, **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() Ability.changelog = relationship(AbilityChangelog, diff --git a/pokedex/doc/tabledoc.py b/pokedex/doc/tabledoc.py index ee92040..a121152 100644 --- a/pokedex/doc/tabledoc.py +++ b/pokedex/doc/tabledoc.py @@ -79,8 +79,10 @@ def column_header(c, class_name=None, transl_name=None, show_type=True, for fk in c.foreign_keys: if fk.column in column_to_cls: 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) + elif relation_name: + result.append(u'(**%s** →' % c.name) else: result.append(u'(→') result.append(u':class:`~pokedex.db.tables.%s`.%s)' % ( @@ -168,8 +170,8 @@ def generate_columns(cls, remaining_attrs): relation_name = c.name[:-3] if c.name.endswith('_id') and relation_name in remaining_attrs: relation = getattr(cls, relation_name) - yield (column_header(c, name, - relation=relation, relation_name=relation_name) + ':') + yield column_header(c, name, + relation=relation, relation_name=relation_name) remaining_attrs.remove(relation_name) else: yield column_header(c, name) + ':' @@ -187,7 +189,7 @@ def generate_strings(cls, remaining_attrs): if c.name in common_columns: continue yield column_header(c, cls.__name__, - translation_class.__table__.name) + ':' + translation_class.__table__.name) yield u'' yield u' ' + unicode(c.info['description']) yield u'' @@ -204,33 +206,43 @@ def generate_relationships(cls, remaining_attrs): for rel_name, info in infos: if rel_name in remaining_attrs: info = cls.relationship_info.get(rel_name) - yield u'%s.\ **%s**' % (cls.__name__, rel_name) - class_name = u':class:`~pokedex.db.tables.%s`' % info['argument'].__name__ - if info.get('uselist', True): - class_name = u'[%s]' % class_name - yield u'(→ %s)' % class_name - if 'description' in info: - yield u'' - yield u' ' + unicode(info['description']) - ''' - if info.get('secondary') is not None: - yield u'' - yield ' Association table: ``%s``' % info['secondary'] - if 'primaryjoin' in info: - yield u'') - yield ' Join condition: ``%s``' % info['primaryjoin'] - if 'secondaryjoin' in info: - yield ' , ``%s``' % info['secondaryjoin'] - ''' - if 'order_by' in info: - yield u'' - try: - order = iter(info['order_by']) - except TypeError: - order = [info['order_by']] - yield u' ' - yield ' Ordered by: ' + u', '.join( - u'``%s``' % o for o in order) + if info['type'] in ('relationship', 'backref'): + yield u'%s.\ **%s**' % (cls.__name__, rel_name) + class_name = u':class:`~pokedex.db.tables.%s`' % info['argument'].__name__ + if info.get('uselist', True): + class_name = u'[%s]' % class_name + yield u'(→ %s)' % class_name + if 'description' in info: + yield u'' + yield u' ' + unicode(info['description']) + ''' + if info.get('secondary') is not None: + yield u'' + yield ' Association table: ``%s``' % info['secondary'] + if 'primaryjoin' in info: + yield u'') + yield ' Join condition: ``%s``' % info['primaryjoin'] + if 'secondaryjoin' in info: + yield ' , ``%s``' % info['secondaryjoin'] + ''' + if 'order_by' in info: + yield u'' + try: + order = iter(info['order_by']) + except TypeError: + order = [info['order_by']] + yield u' ' + yield ' Ordered by: ' + u', '.join( + 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'' remaining_attrs.remove(rel_name)