Oerhaul the Vendor import/export between Rattail and CORE

also, add new DB schema specific to this integration, to hold PKs etc.
This commit is contained in:
Lance Edgar 2020-03-04 19:05:55 -06:00
parent f9071ac6e9
commit af1e38aa18
10 changed files with 311 additions and 30 deletions
rattail_corepos/corepos

View file

@ -96,8 +96,7 @@ class VendorImporter(ToCoreAPI):
'halfCases',
]
# TODO: this importer is in a bit of an experimental state at the moment.
# we only allow "update" b/c it will use the API instead of direct DB
allow_create = False
# we only allow create/update b/c it will use the API instead of direct DB
allow_delete = False
def get_local_objects(self, host_data=None):
@ -122,6 +121,10 @@ class VendorImporter(ToCoreAPI):
return data
def create_object(self, key, data):
# we can get away with using the same logic for both here
return self.update_object(None, data)
def update_object(self, vendor, data, local_data=None):
"""
Push an update for the vendor, via the CORE API.
@ -130,5 +133,5 @@ class VendorImporter(ToCoreAPI):
return data
vendorID = data.pop('vendorID')
self.api.set_vendor(vendorID, **data)
return data
vendor = self.api.set_vendor(vendorID, **data)
return vendor

View file

@ -28,8 +28,10 @@ import logging
from rattail import importing
from rattail.db import model
from rattail.db.util import short_session
from rattail.util import OrderedDict
from rattail_corepos.corepos import importing as corepos_importing
from rattail_corepos.corepos.util import get_max_existing_vendor_id
log = logging.getLogger(__name__)
@ -71,17 +73,31 @@ class VendorImporter(FromRattail, corepos_importing.model.VendorImporter):
'email',
]
def setup(self):
super(VendorImporter, self).setup()
# self.max_existing_vendor_id = self.get_max_existing_vendor_id()
self.max_existing_vendor_id = get_max_existing_vendor_id()
self.last_vendor_id = self.max_existing_vendor_id
def get_next_vendor_id(self):
if hasattr(self, 'last_vendor_id'):
self.last_vendor_id += 1
return self.last_vendor_id
last_vendor_id = get_max_existing_vendor_id()
return last_vendor_id + 1
def normalize_host_object(self, vendor):
if not vendor.id or not vendor.id.isdigit():
log.warning("vendor %s has incompatible ID value: %s",
vendor.uuid, vendor.id)
return
vendor_id = vendor.corepos_id
if not vendor_id:
vendor_id = self.get_next_vendor_id()
data = {
'vendorID': vendor.id,
'vendorID': str(vendor_id),
'vendorName': vendor.name,
'vendorAbbreviation': vendor.abbreviation,
'discountRate': float(vendor.special_discount),
'vendorAbbreviation': vendor.abbreviation or '',
'discountRate': float(vendor.special_discount or 0),
}
if 'phone' in self.fields:
@ -98,4 +114,24 @@ class VendorImporter(FromRattail, corepos_importing.model.VendorImporter):
email = vendor.email
data['email'] = email.address if email else ''
# also embed original Rattail vendor object, if we'll be needing to
# update it later with a new CORE ID
if not vendor.corepos_id:
data['_rattail_vendor'] = vendor
return data
def create_object(self, key, data):
# grab vendor object we (maybe) stashed when normalizing
rattail_vendor = data.pop('_rattail_vendor', None)
# do normal create logic
vendor = super(VendorImporter, self).create_object(key, data)
if vendor:
# maybe set the CORE ID for vendor in Rattail
if rattail_vendor:
rattail_vendor.corepos_id = int(vendor['vendorID'])
return vendor

View file

@ -0,0 +1,41 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2020 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
CORE-POS misc. utilities
"""
import sqlalchemy as sa
from corepos.db.office_op import Session as CoreSession, model as corepos
from rattail.db.util import short_session
def get_max_existing_vendor_id(session=None):
"""
Returns the "last" (max) existing value for the ``vendors.vendorID``
column, for use when creating new records, since it is not auto-increment.
"""
with short_session(Session=CoreSession, session=session) as s:
return s.query(sa.func.max(corepos.Vendor.id))\
.scalar() or 0