-VersionGroup.pokedex = relation(Pokedex, back_populates='version_groups')
-
-
-### Add text/prose tables
-
-default_lang = u'en'
-
-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
- _attrname = name_plural
- _language_identifier = association_proxy('language', 'identifier')
-
- for name, plural, column in columns:
- column.name = 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(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, index=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,
- ))
- str(getattr(object_table, name_plural)) # [MORE MAGIC]. aka do not remove, or entire app fails. XXX what the fuck man
- Strings.object = getattr(Strings, safe_name)
-
- # Link the tables themselves, so we can get them if needed
- Strings.object_table = object_table
- setattr(object_table, name_singular + '_table', Strings)
-
- for colname, pluralname, column in columns:
- # Provide a property with all the names, and an English accessor
- # for backwards compatibility
- setattr(object_table, pluralname, StringProperty(
- object_table, Strings, colname,
- ))
- setattr(object_table, colname, DefaultLangProperty(pluralname))
-
- if colname == 'name':
- object_table.name_table = Strings
-
- return Strings
-
-class StringProperty(object):
- def __init__(self, cls, stringclass, colname):
- self.cls = cls
- self.colname = colname
- self.stringclass = stringclass
-
- def __get__(self, instance, cls):
- if instance:
- return StringMapping(instance, self)
- else:
- return self
-
- def __getitem__(self, lang):
- return StringExpression(self, lang)
-
- def __str__(self):
- return '<StringDict %s.%s>' % (self.cls, self.colname)
-
-class StringMapping(collections.MutableMapping):
- def __init__(self, instance, prop):
- self.stringclass = prop.stringclass
- self.instance = instance
- self.strings = getattr(instance, prop.stringclass._attrname)
- self.colname = prop.colname
-
- def __len__(self):
- return len(self.strings)
-
- def __iter__(self):
- return iter(self.strings)
-
- def __contains__(self, lang):
- return lang in self.strings
-
- def __getitem__(self, lang):
- return getattr(self.strings[lang], self.colname)
-
- def __setitem__(self, lang, value):
- try:
- # Modifying an existing row
- row = self.strings[lang]
- except KeyError:
- # We need do add a whole row for the language
- row = self.stringclass()
- row.object_id = self.instance.id
- session = object_session(self.instance)
- if isinstance(lang, basestring):
- lang = session.query(Language).filter_by(
- identifier=lang).one()
- row.language = lang
- self.strings[lang] = row
- session.add(row)
- return setattr(row, self.colname, value)
-
- def __delitem__(self, lang):
- raise NotImplementedError('Cannot delete a single string. '
- 'Perhaps you wan to delete all of %s.%s?' %
- (self.instance, self.stringclass._attrname)
- )
-
-class StringExpression(ColumnOperators):
- def __init__(self, prop, lang):
- self.prop = prop
- self.column = getattr(prop.stringclass, prop.colname)
- self.lang_column = prop.stringclass._language_identifier
- if isinstance(lang, basestring):
- self.lang = lang
- else:
- self.lang = lang.identifier
-
- def operate(self, op, *values, **kwargs):
- return getattr(self.prop.cls, self.prop.stringclass._attrname).any(and_(
- self.lang_column == self.lang,
- op(self.column, *values, **kwargs),
- ))
-
-class DefaultLangProperty(object):
- def __init__(self, colname):
- self.colname = colname
-
- def __get__(self, instance, cls):
- if instance:
- return getattr(instance, self.colname)[default_lang]
- else:
- return getattr(cls, self.colname)[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=True)
-
-### 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')