Add a mapped_classes list, and a translation_classes list to each mapped class
authorPetr Viktorin <encukou@gmail.com>
Tue, 29 Mar 2011 16:53:16 +0000 (19:53 +0300)
committerEevee <git@veekun.com>
Fri, 1 Apr 2011 22:59:52 +0000 (15:59 -0700)
pokedex/db/multilang.py
pokedex/db/tables.py

index f3438dd..8e067c6 100644 (file)
@@ -145,6 +145,9 @@ def create_translation_table(_table_name, foreign_class, relation_name,
         setattr(foreign_class, name + '_map',
             association_proxy(relation_name, name, creator=creator))
 
         setattr(foreign_class, name + '_map',
             association_proxy(relation_name, name, creator=creator))
 
+    # Add to the list of translation classes
+    foreign_class.translation_classes.append(Translations)
+
     # Done
     return Translations
 
     # Done
     return Translations
 
index d1404ca..24413fb 100644 (file)
@@ -25,7 +25,7 @@ import collections
 from functools import partial
 
 from sqlalchemy import Column, ForeignKey, MetaData, PrimaryKeyConstraint, Table, UniqueConstraint
 from functools import partial
 
 from sqlalchemy import Column, ForeignKey, MetaData, PrimaryKeyConstraint, Table, UniqueConstraint
-from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta
 from sqlalchemy.ext.associationproxy import association_proxy
 from sqlalchemy.orm import backref, relation
 from sqlalchemy.orm.session import Session
 from sqlalchemy.ext.associationproxy import association_proxy
 from sqlalchemy.orm import backref, relation
 from sqlalchemy.orm.session import Session
@@ -60,8 +60,16 @@ class TableSuperclass(object):
     def __str__(self):
         return unicode(self).encode('utf8')
 
     def __str__(self):
         return unicode(self).encode('utf8')
 
+mapped_classes = []
+class TableMetaclass(DeclarativeMeta):
+    def __init__(cls, name, bases, attrs):
+        super(TableMetaclass, cls).__init__(name, bases, attrs)
+        if hasattr(cls, '__tablename__'):
+            mapped_classes.append(cls)
+            cls.translation_classes = []
+
 metadata = MetaData()
 metadata = MetaData()
-TableBase = declarative_base(metadata=metadata, cls=TableSuperclass)
+TableBase = declarative_base(metadata=metadata, cls=TableSuperclass, metaclass=TableMetaclass)
 
 
 ### Need Language first, to create the partial() below
 
 
 ### Need Language first, to create the partial() below