Update usage of short_session() per upstream changes

This commit is contained in:
Lance Edgar 2022-08-21 20:00:15 -05:00
parent 7eb7c25172
commit 63a1172408
4 changed files with 15 additions and 14 deletions

View file

@ -124,7 +124,7 @@ class MemberImporter(ToCoreAPI):
empty_date_value = '0000-00-00 00:00:00'
def get_local_objects(self, host_data=None):
return get_core_members(self.api, progress=self.progress)
return get_core_members(self.config, self.api, progress=self.progress)
def get_single_local_object(self, key):
assert len(self.key) == 1

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2021 Lance Edgar
# Copyright © 2010-2022 Lance Edgar
#
# This file is part of Rattail.
#
@ -30,7 +30,6 @@ from sqlalchemy import orm
from rattail import importing
from rattail.db import model
from rattail.db.util import short_session
from rattail.util import OrderedDict, pretty_quantity
from rattail_corepos.corepos import importing as corepos_importing
from rattail_corepos.corepos.util import get_max_existing_vendor_id
@ -220,7 +219,7 @@ class VendorImporter(FromRattail, corepos_importing.model.VendorImporter):
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.max_existing_vendor_id = get_max_existing_vendor_id(self.config)
self.last_vendor_id = self.max_existing_vendor_id
def get_next_vendor_id(self):
@ -228,7 +227,7 @@ class VendorImporter(FromRattail, corepos_importing.model.VendorImporter):
self.last_vendor_id += 1
return self.last_vendor_id
last_vendor_id = get_max_existing_vendor_id()
last_vendor_id = get_max_existing_vendor_id(self.config)
return last_vendor_id + 1
def normalize_host_object(self, vendor):

View file

@ -30,22 +30,23 @@ import sqlalchemy as sa
from corepos.db.office_op import Session as CoreSession, model as corepos
from rattail.db.util import short_session
from rattail.util import OrderedDict, progress_loop
from rattail.util import OrderedDict
log = logging.getLogger(__name__)
def get_core_members(api, progress=None):
def get_core_members(config, api, progress=None):
"""
Shared logic for fetching *all* customer accounts from CORE-POS API.
"""
app = config.get_app()
# TODO: ideally could do this, but API doesn't let us fetch "all"
# return api.get_members()
# first we fetch all customer records from CORE DB
with short_session(Session=CoreSession) as s:
with app.short_session(factory=CoreSession) as s:
db_customers = s.query(corepos.CustData).all()
s.expunge_all()
@ -63,16 +64,17 @@ def get_core_members(api, progress=None):
logger("could not fetch member from CORE API: %s",
dbcust.card_number)
progress_loop(fetch, db_customers, progress,
message="Fetching Member data from CORE API")
app.progress_loop(fetch, db_customers, progress,
message="Fetching Member data from CORE API")
return list(members.values())
def get_max_existing_vendor_id(session=None):
def get_max_existing_vendor_id(config, 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:
app = config.get_app()
with app.short_session(factory=CoreSession, session=session) as s:
return s.query(sa.func.max(corepos.Vendor.id))\
.scalar() or 0

View file

@ -92,7 +92,7 @@ class FromCOREPOSAPI(importing.Importer):
self.api = make_corepos_api(self.config)
def get_core_members(self):
return get_core_members(self.api, progress=self.progress)
return get_core_members(self.config, self.api, progress=self.progress)
class CustomerImporter(FromCOREPOSAPI, corepos_importing.model.CustomerImporter):