Rename some meta-schema variables for ease of debugging.

This commit is contained in:
Eevee 2011-03-18 17:15:34 -07:00
parent a417a0a9e1
commit 4645349133

View file

@ -34,7 +34,7 @@ from sqlalchemy.ext.declarative import (
) )
from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import ( from sqlalchemy.orm import (
backref, eagerload_all, relation, class_mapper, synonym, mapper, backref, compile_mappers, eagerload_all, relation, class_mapper, synonym, mapper,
) )
from sqlalchemy.orm.session import Session, object_session from sqlalchemy.orm.session import Session, object_session
from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy.orm.collections import attribute_mapped_collection
@ -1862,45 +1862,43 @@ VersionGroup.pokedex = relation(Pokedex, back_populates='version_groups')
default_lang = u'en' default_lang = u'en'
def makeTextTable(object_table, name_plural, name_singular, columns, lazy): def makeTextTable(foreign_table_class, table_suffix_plural, table_suffix_singular, columns, lazy):
# With "Language", we'd have two language_id. So, rename one to 'lang' # With "Language", we'd have two language_id. So, rename one to 'lang'
safe_name = object_table.__singlename__ foreign_key_name = foreign_table_class.__singlename__
if safe_name == 'language': if foreign_key_name == 'language':
safe_name = 'lang' foreign_key_name = 'lang'
tablename = object_table.__singlename__ + '_' + name_plural table_name = foreign_table_class.__singlename__ + '_' + table_suffix_plural
singlename = object_table.__singlename__ + '_' + name_singular
class Strings(object): class TranslatedStringsTable(object):
__tablename__ = tablename __tablename__ = table_name
__singlename__ = singlename _attrname = table_suffix_plural
_attrname = name_plural
_language_identifier = association_proxy('language', 'identifier') _language_identifier = association_proxy('language', 'identifier')
for name, plural, column in columns: for column_name, column_name_plural, column in columns:
column.name = name column.name = column_name
if not column.nullable: if not column.nullable:
# A Python side default value, so that the strings can be set # A Python side default value, so that the strings can be set
# one by one without the DB complaining about missing values # one by one without the DB complaining about missing values
column.default = ColumnDefault(u'') column.default = ColumnDefault(u'')
table = Table(tablename, metadata, table = Table(table_name, metadata,
Column(safe_name + '_id', Integer, ForeignKey(object_table.id), Column(foreign_key_name + '_id', Integer, ForeignKey(foreign_table_class.id),
primary_key=True, nullable=False), primary_key=True, nullable=False),
Column('language_id', Integer, ForeignKey(Language.id), Column('language_id', Integer, ForeignKey(Language.id),
primary_key=True, index=True, nullable=False), primary_key=True, index=True, nullable=False),
*(column for name, plural, column in columns) *(column for name, plural, column in columns)
) )
mapper(Strings, table, mapper(TranslatedStringsTable, table,
properties={ properties={
"object_id": synonym(safe_name + '_id'), "object_id": synonym(foreign_key_name + '_id'),
"language": relation(Language, "language": relation(Language,
primaryjoin=table.c.language_id == Language.id, primaryjoin=table.c.language_id == Language.id,
), ),
safe_name: relation(object_table, foreign_key_name: relation(foreign_table_class,
primaryjoin=(object_table.id == table.c[safe_name + "_id"]), primaryjoin=(foreign_table_class.id == table.c[foreign_key_name + "_id"]),
backref=backref(name_plural, backref=backref(table_suffix_plural,
collection_class=attribute_mapped_collection('language'), collection_class=attribute_mapped_collection('language'),
lazy=lazy, lazy=lazy,
), ),
@ -1909,39 +1907,41 @@ def makeTextTable(object_table, name_plural, name_singular, columns, lazy):
) )
# The relation to the object # The relation to the object
Strings.object = getattr(Strings, safe_name) TranslatedStringsTable.object = getattr(TranslatedStringsTable, foreign_key_name)
# Link the tables themselves, so we can get them if needed # Link the tables themselves, so we can get them if needed
Strings.object_table = object_table TranslatedStringsTable.foreign_table_class = foreign_table_class
setattr(object_table, name_singular + '_table', Strings) setattr(foreign_table_class, table_suffix_singular + '_table', TranslatedStringsTable)
for colname, pluralname, column in columns: for column_name, column_name_plural, column in columns:
# Provide a property with all the names, and an English accessor # Provide a property with all the names, and an English accessor
# for backwards compatibility # for backwards compatibility
def text_string_creator(language_code, string): def text_string_creator(language_code, string):
row = Strings() row = TranslatedStringsTable()
row._language_identifier = language_code row._language_identifier = language_code
setattr(row, colname, string) setattr(row, column_name, string)
return row return row
setattr(object_table, pluralname, setattr(foreign_table_class, column_name_plural,
association_proxy(name_plural, colname, creator=text_string_creator)) association_proxy(table_suffix_plural, column_name, creator=text_string_creator))
setattr(object_table, colname, DefaultLangProperty(pluralname)) setattr(foreign_table_class, column_name, DefaultLangProperty(column_name_plural))
if colname == 'name': if column_name == 'name':
object_table.name_table = Strings foreign_table_class.name_table = TranslatedStringsTable
return Strings compile_mappers()
return TranslatedStringsTable
class DefaultLangProperty(object): class DefaultLangProperty(object):
def __init__(self, colname): def __init__(self, column_name):
self.colname = colname self.column_name = column_name
def __get__(self, instance, cls): def __get__(self, instance, cls):
if instance: if instance:
return getattr(instance, self.colname)[default_lang] return getattr(instance, self.column_name)[default_lang]
else: else:
return getattr(cls, self.colname)[default_lang] # TODO I think this is kind of broken
return getattr(cls, self.column_name)[default_lang]
def __set__(self, instance, value): def __set__(self, instance, value):
getattr(instance, self.colname)[default_lang] = value getattr(instance, self.colname)[default_lang] = value
@ -1976,7 +1976,7 @@ for table in list(table_classes):
if text_columns: if text_columns:
string_table = makeTextTable(table, 'texts', 'text', text_columns, lazy=False) string_table = makeTextTable(table, 'texts', 'text', text_columns, lazy=False)
if prose_columns: if prose_columns:
string_table = makeTextTable(table, 'prose', 'prose', prose_columns, lazy=True) string_table = makeTextTable(table, 'prose', 'prose', prose_columns, lazy='select')
### Add language relations ### Add language relations
for table in list(table_classes): for table in list(table_classes):