Files
notifications-api/app/history_meta.py
Alexey Bezhan 17f5742e2c Use model constructors when creating history instances
`create_history` used to initialise an empty model instance and
assign attributes to it one by one. Assigned attributes are
collected by iterating over internal state of the modified record
and stored in a dictionary, so the order in which they are set on
the history instance is undefined.

This works fine for most model instances, but recent changes to the
Template models require `template_type` to be set before `reply_to`,
otherwise Template doesn't know which column to use to persist the
`reply_to` value.

This means that we need to order the attributes when they're being
assigned. We achieved this by defining a constructor for Template
objects that assigns `template_type` first.

In order to make it work with `create_history` we replace individual
`setattr` calls with a single call to the Template constructor which
passes the entire data dictionary as keyword arguments.

This works because all persisted attributes are defined as columns
and SQLAlchemy accepts all column values as constructor arguments.
2017-11-30 10:57:13 +00:00

7.3 KiB