/usr/lib/python3/dist-packages/sqlalchemy_utils/models.py is in python3-sqlalchemy-utils 0.32.21-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.util.langhelpers import symbol
class Timestamp(object):
"""Adds `created` and `updated` columns to a derived declarative model.
The `created` column is handled through a default and the `updated`
column is handled through a `before_update` event that propagates
for all derived declarative models.
::
import sqlalchemy as sa
from sqlalchemy_utils import Timestamp
class SomeModel(Base, Timestamp):
__tablename__ = 'somemodel'
id = sa.Column(sa.Integer, primary_key=True)
"""
created = sa.Column(sa.DateTime, default=datetime.utcnow, nullable=False)
updated = sa.Column(sa.DateTime, default=datetime.utcnow, nullable=False)
@sa.event.listens_for(Timestamp, 'before_update', propagate=True)
def timestamp_before_update(mapper, connection, target):
# When a model with a timestamp is updated; force update the updated
# timestamp.
target.updated = datetime.utcnow()
NO_VALUE = symbol('NO_VALUE')
NOT_LOADED_REPR = '<not loaded>'
def _generic_repr_method(self, fields):
state = sa.inspect(self)
field_reprs = []
if not fields:
fields = state.mapper.columns.keys()
for key in fields:
value = state.attrs[key].loaded_value
if value == NO_VALUE:
value = NOT_LOADED_REPR
else:
value = repr(value)
field_reprs.append('='.join((key, value)))
return '%s(%s)' % (self.__class__.__name__, ', '.join(field_reprs))
def generic_repr(*fields):
"""Adds generic ``__repr__()`` method to a decalrative SQLAlchemy model.
In case if some fields are not loaded from a database, it doesn't
force their loading and instead repesents them as ``<not loaded>``.
In addition, user can provide field names as arguments to the decorator
to specify what fields should present in the string representation
and in what order.
Example::
import sqlalchemy as sa
from sqlalchemy_utils import generic_repr
@generic_repr
class MyModel(Base):
__tablename__ = 'mymodel'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
category = sa.Column(sa.String)
session.add(MyModel(name='Foo', category='Bar'))
session.commit()
foo = session.query(MyModel).options(sa.orm.defer('category')).one(s)
assert repr(foo) == 'MyModel(id=1, name='Foo', category=<not loaded>)'
"""
if len(fields) == 1 and callable(fields[0]):
target = fields[0]
target.__repr__ = lambda self: _generic_repr_method(self, fields=None)
return target
else:
def decorator(cls):
cls.__repr__ = lambda self: _generic_repr_method(
self,
fields=fields
)
return cls
return decorator
|