Update Fit.modified whenever modules change, allowing proper "recent" timestamps
This commit is contained in:
@@ -17,9 +17,12 @@
|
||||
# along with eos. If not, see <http://www.gnu.org/licenses/>.
|
||||
# ===============================================================================
|
||||
|
||||
import datetime
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.ext.associationproxy import association_proxy
|
||||
from sqlalchemy.orm.collections import attribute_mapped_collection
|
||||
from sqlalchemy.sql import and_
|
||||
from sqlalchemy.event import listen
|
||||
from sqlalchemy.orm import relation, reconstructor, mapper, relationship
|
||||
from sqlalchemy import ForeignKey, Column, Integer, String, Table, Boolean, DateTime
|
||||
import sqlalchemy.sql.functions as func
|
||||
@@ -244,3 +247,24 @@ mapper(ProjectedFit, projectedFits_table,
|
||||
)
|
||||
|
||||
mapper(CommandFit, commandFits_table)
|
||||
|
||||
|
||||
def rel_listener(target, value, initiator):
|
||||
if not target or (isinstance(value, Module) and value.isEmpty):
|
||||
return
|
||||
|
||||
print "{} has has has a relationship changes :(".format(target)
|
||||
target.modified = datetime.datetime.now()
|
||||
|
||||
|
||||
def load_listener(target, context):
|
||||
# We only want to se these events when the fit is first loaded (otherwise events will fire during the initial
|
||||
# population of data). This sets listeners for all the relationships on fits. This allows us to update the fit's
|
||||
# modified date whenever something is added/removed from fit
|
||||
# See http://docs.sqlalchemy.org/en/rel_1_0/orm/events.html#sqlalchemy.orm.events.InstanceEvents.load
|
||||
for rel in inspect(es_Fit).relationships:
|
||||
listen(rel, 'append', rel_listener)
|
||||
listen(rel, 'remove', rel_listener)
|
||||
|
||||
listen(Module, 'load', load_listener)
|
||||
|
||||
|
||||
@@ -17,10 +17,14 @@
|
||||
# along with eos. If not, see <http://www.gnu.org/licenses/>.
|
||||
# ===============================================================================
|
||||
|
||||
from sqlalchemy import Table, Column, Integer, ForeignKey, CheckConstraint, Boolean, DateTime
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy import Table, Column, Integer, ForeignKey, CheckConstraint, Boolean, DateTime, select
|
||||
from sqlalchemy.orm import relation, mapper
|
||||
from sqlalchemy.event import listen
|
||||
import sqlalchemy.sql.functions as func
|
||||
|
||||
import datetime
|
||||
|
||||
from eos.db import saveddata_meta
|
||||
from eos.saveddata.module import Module
|
||||
from eos.saveddata.fit import Fit
|
||||
@@ -40,3 +44,27 @@ modules_table = Table("modules", saveddata_meta,
|
||||
|
||||
mapper(Module, modules_table,
|
||||
properties={"owner": relation(Fit)})
|
||||
|
||||
|
||||
def update_fit_modified(target, value, oldvalue, initiator):
|
||||
if not target.owner:
|
||||
return
|
||||
|
||||
if value != oldvalue:
|
||||
print "{} had a module change".format(target.owner)
|
||||
target.owner.modified = datetime.datetime.now()
|
||||
|
||||
|
||||
def my_load_listener(target, context):
|
||||
# We only want to se these events when the module is first loaded (otherwise events will fire during the initial
|
||||
# population of data). This runs through all columns and sets up "set" events on each column. We do it with each
|
||||
# column because the alternative would be to do a before/after_update for the Mapper itself, however we're only
|
||||
# allowed to change the local attributes during those events as that's inter-flush.
|
||||
# See http://docs.sqlalchemy.org/en/rel_1_0/orm/session_events.html#mapper-level-events
|
||||
for col in inspect(Module).column_attrs:
|
||||
listen(col, 'set', update_fit_modified)
|
||||
|
||||
|
||||
listen(Module, 'load', my_load_listener)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user