endi_base.models package

Submodules

endi_base.models.base module

Database access base objects DBSESSION : database session factory DBBASE : base object for models

class endi_base.models.base.ORMClass

Bases : object

Base class for our models providing usefull query and get methods

classmethod get(id_)

Return a query

classmethod query(*args)

return a query

endi_base.models.base.record_to_appstruct(self)

Transform a SQLAlchemy object into a deform compatible dict usefull to autofill an editform directly from db recovered datas

endi_base.models.initialize module

Initialization functions

endi_base.models.initialize.configure_warnings()

Python warning system setup

Turns the sqla warning about implicit cartesian product into an excetion, to be sure not to miss’em.

If cartesian product is intentional, make it explicit. https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#change-4737

endi_base.models.initialize.initialize_sql(engine)

Initialize the database engine

endi_base.models.mixins module

Various models mixins adding new features/columns to SQLAlchemy models

class endi_base.models.mixins.DuplicableMixin

Bases : object

Factorize duplication logic on sqlalchemy models

__duplicable_fields__ attr should contain the list of attributes to be copied from a model to its duplicate.

duplicate(factory=None, **kwargs)

Duplicate all fields listed in __duplicable_fields__ plus optional ad-hoc values.

Paramètres:
  • factory (class) – The new instance class

  • kwargs – allow to provide ad-hoc values for the attrs of the new instance. They will take precedence over copied values.

Renvoie:

new instance combining fields copied from self, and ad-hoc args. The instance is not saved.

class endi_base.models.mixins.OfficialNumberMixin

Bases : object

Models that implements that mixin are then able to be numbered using numbering sequence mechanisms with guarantees on non-duplicate.

Models are required to be child of endi.models.node.Node

Features :

  • templated creation of official_number that may use one or more numeric sequences

  • guarantees on official_number (unicity and no gap in sequences)

  • instances with an official_number set should not be deleted

Inheriting class must:

  • ensure that once official_number is set, instances are not deleted

Inheriting class may:

  • redefine official_number and company_id as long as name and type are kept as-is

  • redefine validation_date_column if the validation is not stored into self.date

company_id = Column(None, Integer(), ForeignKey('company.id'), table=None, nullable=False)
classmethod get_validation_date_column() Column
official_number = Column(None, String(length=255), table=None)
property validation_date: date
validation_date_column = 'date'
class endi_base.models.mixins.PersistentACLMixin

Bases : object

Extend pyramid ACL mechanism to offer row-level ACL

Subclasses may set the following attributes :

  • .__default_acl__ as an ACL or callable returning an ACL, traditionaly set at class level.

  • .__acl__ as an ACL or callable returning an ACL, this can be set on class or directly on instance.

If both are defined, __acl__, the more specific, is prefered.

Use actual callables, not properties, in subclasses to prevent pyramid from silently ignoring any AttributeError your callable should trigger.

cache_acl()
parent: Any = None
class endi_base.models.mixins.TimeStampedMixin

Bases : object

created_at = Column(None, DateTime(), table=None, default=ColumnDefault(<function datetime.now>))
updated_at = Column(None, DateTime(), table=None, onupdate=ColumnDefault(<function datetime.now>), default=ColumnDefault(<function datetime.now>))

endi_base.models.types module

Custom types and usefull functions

class endi_base.models.types.ACLType(*args, **kwargs)

Bases : JsonEncodedList

all_permissions_serialized = '__ALL_PERMISSIONS__'
process_bind_param(value, dialect)

Process params when setting the value

process_result_value(value, dialect)

Processing the value when getting the value

class endi_base.models.types.JsonEncodedDict(*args, **kwargs)

Bases : TypeDecorator

Stores a dict as a json string in the database

impl

alias de Text

process_bind_param(value, dialect)

Process params when setting the value

process_result_value(value, dialect)

Processing the value when getting the value

class endi_base.models.types.JsonEncodedList(*args, **kwargs)

Bases : TypeDecorator

Stores a list as a json string

impl

alias de Text

process_bind_param(value, dialect)

Process params when setting the value

process_result_value(value, dialect)

Processing the value when getting the value

class endi_base.models.types.MutableDict

Bases : Mutable, dict

Allows sqlalchemy to check if a data has changed in our dict If not used, the dbsession will never detect modifications

Noteonly associating a value to one key will work (no subdict

handling)

classmethod coerce(key, value)

Convert plain dictionaries to MutableDict.

class endi_base.models.types.MutableList(iterable=(), /)

Bases : Mutable, list

Allows sqlalchemy to check if a data has changed in our list If not used, the dbsession will never detect modifications

append(value)

Detect list append changes

classmethod coerce(key, value)

Convert list to mutablelist

extend(value)

Detect list append changes

remove(value)

Detect list remove change

endi_base.models.utils module

Usefull functions

endi_base.models.utils.format_to_taskdate(value)

format a datetime.date object to a “taskdate” format: an integer composed from a string YYYYmmdd Sorry …. it’s not my responsability

endi_base.models.utils.get_current_timestamp()

returns current time

endi_base.models.utils.non_null_sum(column, *args, **kwargs)

Cast the sqlalchemy built with func.sum to the same type as the column being summed

Usefull to avoid getting decimal when we expect an int :param obj column: The SqlAlchemy column (e.g : Payment.amount) :returns: A SqlAlchemy query option (passed to Dbsession.query)

Module contents