Skip to content

Commit

Permalink
Document _ModelUtilitiesMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
epandurski committed Apr 2, 2019
1 parent eb5fda5 commit d5b0b4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Mixins
:members:


.. autoclass:: flask_signalbus.atomic._ModelUtilitiesMixin
:members:


Exceptions
``````````

Expand Down
23 changes: 20 additions & 3 deletions flask_signalbus/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
class _ModelUtilitiesMixin(object):
@classmethod
def get_instance(cls, instance_or_pk):
"""Return an instance in ``db.session`` when given any instance or a primary key."""
"""Return an instance in ``db.session`` when given any model instance or a primary key.
Note: A composite primary key should be passed as a tuple.
"""

if isinstance(instance_or_pk, cls):
if instance_or_pk in cls._flask_signalbus_sa.session:
Expand All @@ -29,7 +32,10 @@ def get_instance(cls, instance_or_pk):

@classmethod
def lock_instance(cls, instance_or_pk, read=False):
"""Return a locked instance in ``db.session`` when given any instance or a primary key."""
"""Return a locked instance in ``db.session`` when given any model instance or a primary key.
Note: A composite primary key should be passed as a tuple.
"""

mapper = inspect(cls)
pk_attrs = [mapper.get_property_by_column(c).class_attribute for c in mapper.primary_key]
Expand All @@ -39,7 +45,10 @@ def lock_instance(cls, instance_or_pk, read=False):

@classmethod
def get_pk_values(cls, instance_or_pk):
"""Return a primary key as a tuple when given any instance or primary key."""
"""Return a primary key as a tuple when given any model instance or primary key.
Note: A composite primary key should be passed as a tuple.
"""

if isinstance(instance_or_pk, cls):
cls._flask_signalbus_sa.session.flush()
Expand All @@ -61,9 +70,17 @@ class CustomSQLAlchemy(AtomicProceduresMixin, SQLAlchemy):
db = CustomSQLAlchemy()
# `AtomicProceduresMixin` method are available in `db`
Note that `AtomicProceduresMixin` should always come before
:class:`~flask_sqlalchemy.SQLAlchemy`.
In addition to `AtomicProceduresMixin` method being available in
``db``, the classmethods from
:class:`~flask_signalbus.atomic._ModelUtilitiesMixin` will be
available in the declarative base class (``db.Model``). This means
that they will also be available on every model instance.
"""

def apply_driver_hacks(self, app, info, options):
Expand Down

0 comments on commit d5b0b4d

Please sign in to comment.