Add initial Rattail -> CORE-POS export logic

only allows "update" for Vendor model so far.  more to come after testing...
This commit is contained in:
Lance Edgar 2020-03-03 21:45:11 -06:00
parent 6f03461114
commit 0298e63384
5 changed files with 251 additions and 8 deletions

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
# Copyright © 2010-2020 Lance Edgar
#
# This file is part of Rattail.
#
@ -27,7 +27,7 @@ DataSync for CORE POS
from corepos.db.office_op import Session as CoreSession, model as corepos
from rattail.db import model
from rattail.datasync import DataSyncWatcher
from rattail.datasync import DataSyncWatcher, NewDataSyncImportConsumer
class CoreOfficeOpWatcher(DataSyncWatcher):
@ -130,3 +130,66 @@ class COREPOSProductWatcher(DataSyncWatcher):
session.close()
return changes
class FromRattailToCore(NewDataSyncImportConsumer):
"""
Rattail -> CORE POS datasync consumer
"""
handler_spec = 'rattail_corepos.corepos.importing.rattail:FromRattailToCore'
def begin_transaction(self):
self.corepos_session = CoreSession()
def rollback_transaction(self):
self.corepos_session.rollback()
self.corepos_session.close()
def commit_transaction(self):
self.corepos_session.commit()
self.corepos_session.close()
def process_changes(self, session, changes):
"""
Process all the given changes, coming from Rattail.
"""
# TODO: this probably doesn't accomplish anything here?
if self.runas_username:
session.set_continuum_user(self.runas_username)
# update all importers with current Rattail/CORE sessions
for importer in self.importers.values():
importer.host_session = session
importer.session = self.corepos_session
# also establish the API client for each!
importer.establish_api()
# next pass syncs all Vendor changes
types = [
'Vendor',
'VendorPhoneNumber',
'VendorEmailAddress',
]
for change in [c for c in changes if c.payload_type in types]:
vendor = self.get_host_vendor(session, change)
if vendor:
# TODO: what about "deletions" - not sure what happens yet
self.process_change(session, self.importers['Vendor'],
host_object=vendor)
# self.process_change(session, self.importers['VendorContact'],
# host_object=vendor)
def get_host_vendor(self, session, change):
if change.payload_type == 'Vendor':
return session.query(model.Vendor).get(change.payload_key)
if change.payload_type == 'VendorPhoneNumber':
phone = session.query(model.VendorPhoneNumber).get(change.payload_key)
if phone:
return phone.vendor
if change.payload_type == 'VendorEmailAddress':
email = session.query(model.VendorEmailAddress).get(change.payload_key)
if email:
return email.vendor