Add has_any_changes function
This commit is contained in:
@@ -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.26.3 (2014-06-25)
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
- Added has_any_changes
|
||||||
|
|
||||||
|
|
||||||
0.26.2 (2014-05-29)
|
0.26.2 (2014-05-29)
|
||||||
^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
@@ -52,6 +52,12 @@ query_entities
|
|||||||
.. autofunction:: query_entities
|
.. autofunction:: query_entities
|
||||||
|
|
||||||
|
|
||||||
|
has_any_changes
|
||||||
|
^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. autofunction:: has_any_changes
|
||||||
|
|
||||||
|
|
||||||
has_changes
|
has_changes
|
||||||
^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
|
|
||||||
|
@@ -19,6 +19,8 @@ from .functions import (
|
|||||||
get_referencing_foreign_keys,
|
get_referencing_foreign_keys,
|
||||||
get_tables,
|
get_tables,
|
||||||
group_foreign_keys,
|
group_foreign_keys,
|
||||||
|
has_any_changes,
|
||||||
|
has_changes,
|
||||||
has_index,
|
has_index,
|
||||||
identity,
|
identity,
|
||||||
merge_references,
|
merge_references,
|
||||||
@@ -94,6 +96,8 @@ __all__ = (
|
|||||||
get_referencing_foreign_keys,
|
get_referencing_foreign_keys,
|
||||||
get_tables,
|
get_tables,
|
||||||
group_foreign_keys,
|
group_foreign_keys,
|
||||||
|
has_any_changes,
|
||||||
|
has_changes,
|
||||||
has_index,
|
has_index,
|
||||||
identity,
|
identity,
|
||||||
instrumented_list,
|
instrumented_list,
|
||||||
|
@@ -26,6 +26,7 @@ from .orm import (
|
|||||||
get_primary_keys,
|
get_primary_keys,
|
||||||
get_tables,
|
get_tables,
|
||||||
getdotattr,
|
getdotattr,
|
||||||
|
has_any_changes,
|
||||||
has_changes,
|
has_changes,
|
||||||
identity,
|
identity,
|
||||||
naturally_equivalent,
|
naturally_equivalent,
|
||||||
@@ -50,6 +51,7 @@ __all__ = (
|
|||||||
'get_tables',
|
'get_tables',
|
||||||
'getdotattr',
|
'getdotattr',
|
||||||
'group_foreign_keys',
|
'group_foreign_keys',
|
||||||
|
'has_any_changes',
|
||||||
'has_changes',
|
'has_changes',
|
||||||
'identity',
|
'identity',
|
||||||
'is_auto_assigned_date_column',
|
'is_auto_assigned_date_column',
|
||||||
|
@@ -475,6 +475,7 @@ def has_changes(obj, attr):
|
|||||||
|
|
||||||
:param obj: SQLAlchemy declarative model object
|
:param obj: SQLAlchemy declarative model object
|
||||||
:param attr: Name of the attribute
|
:param attr: Name of the attribute
|
||||||
|
.. seealso:: :func:`has_any_changes`
|
||||||
"""
|
"""
|
||||||
return (
|
return (
|
||||||
sa.inspect(obj)
|
sa.inspect(obj)
|
||||||
@@ -485,6 +486,33 @@ def has_changes(obj, attr):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def has_any_changes(model, columns):
|
||||||
|
"""
|
||||||
|
Simple shortcut function for checking if any of the given attributes of
|
||||||
|
given declarative model object have changes.
|
||||||
|
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
|
||||||
|
from sqlalchemy_utils import has_any_changes
|
||||||
|
|
||||||
|
|
||||||
|
user = User()
|
||||||
|
|
||||||
|
has_any_changes(user, ('name', )) # False
|
||||||
|
|
||||||
|
user.name = u'someone'
|
||||||
|
|
||||||
|
has_any_changes(user, ('name', 'age')) # True
|
||||||
|
|
||||||
|
|
||||||
|
:param obj: SQLAlchemy declarative model object
|
||||||
|
:param attrs: Names of the attributes
|
||||||
|
"""
|
||||||
|
return any(has_changes(model, column) for column in columns)
|
||||||
|
|
||||||
|
|
||||||
def identity(obj_or_class):
|
def identity(obj_or_class):
|
||||||
"""
|
"""
|
||||||
Return the identity of given sqlalchemy declarative model class or instance
|
Return the identity of given sqlalchemy declarative model class or instance
|
||||||
|
24
tests/functions/test_has_any_changes.py
Normal file
24
tests/functions/test_has_any_changes.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
|
from sqlalchemy_utils import has_any_changes
|
||||||
|
|
||||||
|
|
||||||
|
class TestHasAnyChanges(object):
|
||||||
|
def setup_method(self, method):
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class Article(Base):
|
||||||
|
__tablename__ = 'article_translation'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
title = sa.Column(sa.String(100))
|
||||||
|
|
||||||
|
self.Article = Article
|
||||||
|
|
||||||
|
def test_without_changed_attr(self):
|
||||||
|
article = self.Article()
|
||||||
|
assert not has_any_changes(article, ['title'])
|
||||||
|
|
||||||
|
def test_with_changed_attr(self):
|
||||||
|
article = self.Article(title='Some title')
|
||||||
|
assert has_any_changes(article, ['title', 'id'])
|
Reference in New Issue
Block a user