Added utility functions identity, is_auto_assigned_date_column and declarative_base

This commit is contained in:
Konsta Vesterinen
2013-07-23 11:27:26 +03:00
parent 3162f4cd06
commit 6beffc8d96
5 changed files with 69 additions and 2 deletions

View File

@@ -4,6 +4,12 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release. Here you can see the full list of changes between each SQLAlchemy-Utils release.
0.15.1 (2013-07-22)
^^^^^^^^^^^^^^^^^^^
- Added utility functions declarative_base, identity and is_auto_assigned_date_column
0.15.0 (2013-07-22) 0.15.0 (2013-07-22)
^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^

View File

@@ -206,6 +206,10 @@ API Documentation
.. autofunction:: escape_like .. autofunction:: escape_like
.. autofunction:: non_indexed_foreign_keys .. autofunction:: non_indexed_foreign_keys
.. autofunction:: is_indexed_foreign_key .. autofunction:: is_indexed_foreign_key
.. autofunction:: identity
.. autofunction:: is_auto_assigned_date_column
.. autofunction:: declarative_base
.. include:: ../CHANGES.rst .. include:: ../CHANGES.rst

View File

@@ -24,7 +24,7 @@ class PyTest(Command):
setup( setup(
name='SQLAlchemy-Utils', name='SQLAlchemy-Utils',
version='0.15.0', version='0.15.1',
url='https://github.com/kvesteri/sqlalchemy-utils', url='https://github.com/kvesteri/sqlalchemy-utils',
license='BSD', license='BSD',
author='Konsta Vesterinen', author='Konsta Vesterinen',

View File

@@ -26,7 +26,7 @@ from .types import (
) )
__version__ = '0.15.0' __version__ = '0.15.1'
__all__ = ( __all__ = (

View File

@@ -1,4 +1,5 @@
from collections import defaultdict from collections import defaultdict
import sqlalchemy as sa
from sqlalchemy.orm import defer from sqlalchemy.orm import defer
from sqlalchemy.orm.mapper import Mapper from sqlalchemy.orm.mapper import Mapper
from sqlalchemy.orm.query import _ColumnEntity from sqlalchemy.orm.query import _ColumnEntity
@@ -295,3 +296,59 @@ def is_indexed_foreign_key(constraint):
if index_column_names == set(constraint.columns): if index_column_names == set(constraint.columns):
return True return True
return False return False
def declarative_base(model):
"""
Returns the declarative base for given model class.
:param model: SQLAlchemy declarative model
"""
for parent in model.__bases__:
try:
parent.metadata
return declarative_base(parent)
except AttributeError:
pass
return model
def is_auto_assigned_date_column(column):
"""
Returns whether or not given SQLAlchemy Column object's is auto assigned
DateTime or Date.
:param column: SQLAlchemy Column object
"""
return (
(
isinstance(column.type, sa.DateTime) or
isinstance(column.type, sa.Date)
)
and
(
column.default or
column.server_default or
column.onupdate or
column.server_onupdate
)
)
def identity(obj):
"""
Return the identity of given sqlalchemy declarative model instance as a
tuple. This differs from obj._sa_instance_state.identity in a way that it
always returns the identity even if object is still in transient state (
new object that is not yet persisted into database).
:param obj: SQLAlchemy declarative model object
"""
id_ = []
for attr in obj._sa_class_manager.values():
prop = attr.property
if isinstance(prop, sa.orm.ColumnProperty):
column = prop.columns[0]
if column.primary_key:
id_.append(getattr(obj, column.name))
return tuple(id_)