def count_versions(obj): """ Return the number of versions given object has. This function works even when obj has `create_models` and `create_tables` versioned settings disabled. :: article = Article(name=u'Some article') count_versions(article) # 0 session.add(article) session.commit() count_versions(article) # 1 :param obj: SQLAlchemy declarative model object """ session = sa.orm.object_session(obj) if session is None: # If object is transient, we assume it has no version history. return 0 manager = get_versioning_manager(obj) table_name = manager.option(obj, 'table_name') % obj.__table__.name criteria = [ '%s = %r' % (pk, getattr(obj, pk)) for pk in get_primary_keys(obj) ] query = 'SELECT COUNT(1) FROM %s WHERE %s' % ( table_name, ' AND '.join(criteria) ) return session.execute(query).scalar()
def count_versions(obj): """ Return the number of versions given object has. This function works even when obj has `create_models` and `create_tables` versioned settings disabled. :: article = Article(name=u'Some article') count_versions(article) # 0 session.add(article) session.commit() count_versions(article) # 1 :param obj: SQLAlchemy declarative model object """ session = sa.orm.object_session(obj) if session is None: # If object is transient, we assume it has no version history. return 0 manager = get_versioning_manager(obj) table_name = manager.option(obj, 'table_name') % obj.__table__.name criteria = [ '%s = %r' % (pk, getattr(obj, pk)) for pk in get_primary_keys(obj) ] query = 'SELECT COUNT(1) FROM %s WHERE %s' % (table_name, ' AND '.join(criteria)) return session.execute(query).scalar()
def primary_key_conditions(self): conditions = [] for key in get_primary_keys(self.parent_cls).keys(): conditions.append( getattr(self.parent_cls, key) == getattr( self.translation_cls, key)) return conditions
def primary_key_conditions(self): conditions = [] for key in get_primary_keys(self.parent_cls).keys(): conditions.append( getattr(self.parent_cls, key) == getattr(self.translation_cls, key) ) return conditions
def __table_args__(cls): if has_inherited_table(cls): return tuple() else: names = list(get_primary_keys(parent_cls).keys()) return (sa.schema.ForeignKeyConstraint(names, [ '%s.%s' % (parent_cls.__tablename__, name) for name in names ], **foreign_key_args), )
def get_key_id(self, db_obj): # primary_keys = inspect(db_obj).primary_key # primary_keys = get_primary_keys(db_obj) # print(db_obj) primary_keys = list(get_primary_keys(db_obj).keys()) if primary_keys: key_name = primary_keys[0] return getattr(db_obj, key_name) else: column = db_obj.__table__.columns[0] return getattr(db_obj, column.name)
def __table_args__(cls): if has_inherited_table(cls): return tuple() else: names = list(get_primary_keys(parent_cls).keys()) return ( sa.schema.ForeignKeyConstraint( names, [ '%s.%s' % (parent_cls.__tablename__, name) for name in names ], **foreign_key_args ), )
def assign_single_translations(self): mapper = sa.orm.class_mapper(self.parent_cls) for locale in option(self.parent_cls, 'locales'): key = '_translation_%s' % locale if mapper.has_property(key): continue conditions = self.primary_key_conditions conditions.append(self.translation_cls.locale == locale) mapper.add_property( key, sa.orm.relationship(self.translation_cls, primaryjoin=sa.and_(*conditions), foreign_keys=list( get_primary_keys( self.parent_cls).values()), uselist=False, viewonly=True))
def assign_single_translations(self): mapper = sa.orm.class_mapper(self.parent_cls) for locale in option(self.parent_cls, 'locales'): key = '_translation_%s' % locale if mapper.has_property(key): continue conditions = self.primary_key_conditions conditions.append(self.translation_cls.locale == locale) mapper.add_property(key, sa.orm.relationship( self.translation_cls, primaryjoin=sa.and_(*conditions), foreign_keys=list( get_primary_keys(self.parent_cls).values() ), uselist=False, viewonly=True ))
def assign_current_translation(self): """ Assign the current translation relationship for translatable parent class. """ mapper = sa.orm.class_mapper(self.parent_cls) if not mapper.has_property('_current_translation'): conditions = self.primary_key_conditions conditions.append(self.translation_cls.locale == current_locale()) mapper.add_property( '_current_translation', sa.orm.relationship(self.translation_cls, primaryjoin=sa.and_(*conditions), foreign_keys=list( get_primary_keys( self.parent_cls).values()), viewonly=True, uselist=False))
def assign_translations(self): """ Assigns translations relationship for translatable model. The assigned attribute is a relationship to all translation locales. """ mapper = sa.orm.class_mapper(self.parent_cls) if not mapper.has_property('_translations'): foreign_keys = [ getattr(self.translation_cls, column_key) for column_key in get_primary_keys(self.parent_cls).keys() ] mapper.add_property('_translations', sa.orm.relationship( self.translation_cls, primaryjoin=sa.and_(*self.primary_key_conditions), foreign_keys=foreign_keys, collection_class=attribute_mapped_collection('locale'), comparator_factory=TranslationComparator, cascade='all, delete-orphan', passive_deletes=option(self.parent_cls, 'passive_deletes'), ))
def assign_current_translation(self): """ Assign the current translation relationship for translatable parent class. """ mapper = sa.orm.class_mapper(self.parent_cls) if not mapper.has_property('_current_translation'): conditions = self.primary_key_conditions conditions.append( self.translation_cls.locale == current_locale() ) mapper.add_property('_current_translation', sa.orm.relationship( self.translation_cls, primaryjoin=sa.and_(*conditions), foreign_keys=list( get_primary_keys(self.parent_cls).values() ), viewonly=True, uselist=False ))
def assign_translations(self): """ Assigns translations relationship for translatable model. The assigned attribute is a relationship to all translation locales. """ mapper = sa.orm.class_mapper(self.parent_cls) if not mapper.has_property('_translations'): foreign_keys = [ getattr(self.translation_cls, column_key) for column_key in get_primary_keys(self.parent_cls).keys() ] mapper.add_property( '_translations', sa.orm.relationship( self.translation_cls, primaryjoin=sa.and_(*self.primary_key_conditions), foreign_keys=foreign_keys, collection_class=attribute_mapped_collection('locale'), comparator_factory=TranslationComparator, cascade='all, delete-orphan', passive_deletes=True, ))
def get_translations_relationship_args(self): foreign_keys = [ getattr(self.translation_cls, column_key) for column_key in get_primary_keys(self.parent_cls).keys() ] relationship_args = copy( self.manager.option( self.parent_cls, 'translations_relationship_args' ) ) defaults = dict( primaryjoin=sa.and_(*self.primary_key_conditions), foreign_keys=foreign_keys, collection_class=attribute_mapped_collection('locale'), comparator_factory=TranslationComparator, cascade='all, delete-orphan', passive_deletes=True, ) for key, value in defaults.items(): relationship_args.setdefault(key, value) return relationship_args
def model_primary_keys(self): return get_primary_keys(self.opts.model)