+
+
+### Add name tables
+for table in list(table_classes):
+ if issubclass(table, OfficiallyNamed):
+ cls = TextColumn
+ info=dict(description="The name", format='plaintext', official=True)
+ elif issubclass(table, UnofficiallyNamed):
+ cls = ProseColumn
+ info=dict(description="The name", format='plaintext', official=False)
+ else:
+ continue
+ table.name = cls(Unicode(class_mapper(table).c.identifier.type.length),
+ plural='names', nullable=False, info=info)
+
+### Add text/prose tables
+
+def makeTextTable(object_table, name_plural, name_singular, columns, lazy):
+ # With "Language", we'd have two language_id. So, rename one to 'lang'
+ safe_name = object_table.__singlename__
+ if safe_name == 'language':
+ safe_name = 'lang'
+
+ tablename = object_table.__singlename__ + '_' + name_plural
+ singlename = object_table.__singlename__ + '_' + name_singular
+
+ class Strings(object):
+ __tablename__ = tablename
+ __singlename__ = singlename
+
+ for name, plural, column in columns:
+ column.name = name
+
+ table = Table(tablename, metadata,
+ Column(safe_name + '_id', Integer, ForeignKey(object_table.id),
+ primary_key=True, nullable=False),
+ Column('language_id', Integer, ForeignKey(Language.id),
+ primary_key=True, nullable=False),
+ *(column for name, plural, column in columns)
+ )
+
+ mapper(Strings, table,
+ properties={
+ "object_id": synonym(safe_name + '_id'),
+ "language": relation(
+ Language,
+ primaryjoin=table.c.language_id == Language.id,
+ ),
+ },
+ )
+
+ # The relation to the object
+ setattr(object_table, name_plural, relation(
+ Strings,
+ primaryjoin=(object_table.id == Strings.object_id),
+ backref=safe_name,
+ collection_class=attribute_mapped_collection('language'),
+ lazy=lazy,
+ ))
+ Strings.object = getattr(Strings, safe_name)
+
+ # Link the tables themselves, so we can get to them
+ Strings.object_table = object_table
+ setattr(object_table, name_singular + '_table', Strings)
+
+ for colname, pluralname, column in columns:
+ # Provide a relation with all the names, and an English accessor
+ # for backwards compatibility
+ def scope(colname, pluralname, column):
+ def get_strings(self):
+ return dict(
+ (l, getattr(t, colname))
+ for l, t in getattr(self, name_plural).items()
+ )
+
+ def get_english_string(self):
+ try:
+ return get_strings(self)['en']
+ except KeyError:
+ raise AttributeError(colname)
+
+ setattr(object_table, pluralname, property(get_strings))
+ setattr(object_table, colname, property(get_english_string))
+ scope(colname, pluralname, column)
+
+ if colname == 'name':
+ object_table.name_table = Strings
+
+ return Strings
+
+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))
+ 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=True)
+
+### Add language relations
+for table in list(table_classes):
+ if issubclass(table, LanguageSpecific):
+ table.language = relation(Language, primaryjoin=table.language_id == Language.id)