+
+
+### Add text/prose tables
+
+default_lang = u'en'
+
+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'
+ foreign_key_name = foreign_table_class.__singlename__
+ if foreign_key_name == 'language':
+ foreign_key_name = 'lang'
+
+ table_name = foreign_table_class.__singlename__ + '_' + table_suffix_plural
+
+ class TranslatedStringsTable(object):
+ __tablename__ = table_name
+ _attrname = table_suffix_plural
+ _language_identifier = association_proxy('language', 'identifier')
+
+ for column_name, column_name_plural, column in columns:
+ column.name = column_name
+ if not column.nullable:
+ # A Python side default value, so that the strings can be set
+ # one by one without the DB complaining about missing values
+ column.default = ColumnDefault(u'')
+
+ table = Table(table_name, metadata,
+ Column(foreign_key_name + '_id', Integer, ForeignKey(foreign_table_class.id),
+ primary_key=True, nullable=False),
+ Column('language_id', Integer, ForeignKey(Language.id),
+ primary_key=True, index=True, nullable=False),
+ *(column for name, plural, column in columns)
+ )
+
+ mapper(TranslatedStringsTable, table,
+ properties={
+ "object_id": synonym(foreign_key_name + '_id'),
+ "language": relation(Language,
+ primaryjoin=table.c.language_id == Language.id,
+ ),
+ foreign_key_name: relation(foreign_table_class,
+ primaryjoin=(foreign_table_class.id == table.c[foreign_key_name + "_id"]),
+ backref=backref(table_suffix_plural,
+ collection_class=attribute_mapped_collection('language'),
+ lazy=lazy,
+ ),
+ ),
+ },
+ )
+
+ # The relation to the object
+ TranslatedStringsTable.object = getattr(TranslatedStringsTable, foreign_key_name)
+
+ # Link the tables themselves, so we can get them if needed
+ TranslatedStringsTable.foreign_table_class = foreign_table_class
+ setattr(foreign_table_class, table_suffix_singular + '_table', TranslatedStringsTable)
+
+ for column_name, column_name_plural, column in columns:
+ # Provide a property with all the names, and an English accessor
+ # for backwards compatibility
+ def text_string_creator(language_code, string):
+ row = TranslatedStringsTable()
+ row._language_identifier = language_code
+ setattr(row, column_name, string)
+ return row
+
+ setattr(foreign_table_class, column_name_plural,
+ association_proxy(table_suffix_plural, column_name, creator=text_string_creator))
+ setattr(foreign_table_class, column_name, DefaultLangProperty(column_name_plural))
+
+ if column_name == 'name':
+ foreign_table_class.name_table = TranslatedStringsTable
+
+ compile_mappers()
+ return TranslatedStringsTable
+
+class DefaultLangProperty(object):
+ def __init__(self, column_name):
+ self.column_name = column_name
+
+ def __get__(self, instance, cls):
+ if instance:
+ return getattr(instance, self.column_name)[default_lang]
+ else:
+ # TODO I think this is kind of broken
+ return getattr(cls, self.column_name)[default_lang]
+
+ def __set__(self, instance, value):
+ getattr(instance, self.colname)[default_lang] = value
+
+ def __delete__(self, instance):
+ del getattr(instance, self.colname)[default_lang]
+
+for table in list(table_classes):
+ # Find all the language-specific columns, keeping them in the order they
+ # were defined
+ all_columns = []
+ for colname in dir(table):
+ column = getattr(table, colname)
+ if isinstance(column, LanguageSpecificColumn):
+ all_columns.append((colname, column))
+ delattr(table, colname)
+ all_columns.sort(key=lambda pair: pair[1].order)
+
+ # Break them into text and prose columns
+ text_columns = []
+ prose_columns = []
+ for colname, column in all_columns:
+ spec = colname, column.plural, column.makeSAColumn()
+ if isinstance(column, TextColumn):
+ text_columns.append(spec)
+ elif isinstance(column, ProseColumn):
+ prose_columns.append(spec)
+
+ if (text_columns or prose_columns) and issubclass(table, LanguageSpecific):
+ raise AssertionError("Language-specific table %s shouldn't have explicit language-specific columns" % table)
+
+ if text_columns:
+ string_table = makeTextTable(table, 'texts', 'text', text_columns, lazy=False)
+ if prose_columns:
+ string_table = makeTextTable(table, 'prose', 'prose', prose_columns, lazy='select')
+
+### Add language relations
+for table in list(table_classes):
+ if issubclass(table, LanguageSpecific):
+ table.language = relation(Language, primaryjoin=table.language_id == Language.id)
+
+Move.effect = DefaultLangProperty('effects')
+Move.effects = markdown.MoveEffectsProperty('effect')
+Move.short_effect = DefaultLangProperty('short_effects')
+Move.short_effects = markdown.MoveEffectsProperty('short_effect')
+
+MoveChangelog.effect = DefaultLangProperty('effects')
+MoveChangelog.effects = markdown.MoveEffectsProperty('effect')
+MoveChangelog.short_effect = DefaultLangProperty('short_effects')
+MoveChangelog.short_effects = markdown.MoveEffectsProperty('short_effect')