diff --git a/rattail_corepos/datasync/corepos.py b/rattail_corepos/datasync/corepos.py index a2adc58..17a0aa9 100644 --- a/rattail_corepos/datasync/corepos.py +++ b/rattail_corepos/datasync/corepos.py @@ -24,16 +24,46 @@ DataSync for CORE POS """ -from __future__ import unicode_literals, absolute_import - -import six - -from corepos.db import Session as CoreSession, model as corepos +from corepos.db.office_op import Session as CoreSession, model as corepos from rattail.db import model from rattail.datasync import DataSyncWatcher +class CoreOfficeOpWatcher(DataSyncWatcher): + """ + DataSync watcher for the CORE ``office_op`` database. + """ + prunes_changes = True + + def get_changes(self, lastrun): + session = CoreSession() + changes = session.query(corepos.Change).all() + session.expunge_all() + session.close() + if changes: + return [ + (c.id, + model.DataSyncChange( + payload_type=c.object_type, + payload_key=c.object_key, + deletion=c.deleted)) + for c in changes] + + def prune_changes(self, keys): + deleted = 0 + session = CoreSession() + for key in keys: + change = session.query(corepos.Change).get(key) + if change: + session.delete(change) + session.flush() + deleted += 1 + session.commit() + session.close() + return deleted + + class COREPOSProductWatcher(DataSyncWatcher): """ DataSync watcher for the CORE POS database. @@ -56,7 +86,7 @@ class COREPOSProductWatcher(DataSyncWatcher): (None, model.DataSyncChange( payload_type='Department', - payload_key=six.text_type(dept.number))) + payload_key=str(dept.number))) for dept in departments]) # TODO: subdepartment table doesn't have a modified flag?