From 63a11724086df1b4fe16ca9ac943fb9eb29dfc2f Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 21 Aug 2022 20:00:15 -0500 Subject: [PATCH] Update usage of `short_session()` per upstream changes --- rattail_corepos/corepos/importing/model.py | 2 +- rattail_corepos/corepos/importing/rattail.py | 7 +++---- rattail_corepos/corepos/util.py | 18 ++++++++++-------- rattail_corepos/importing/corepos/api.py | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/rattail_corepos/corepos/importing/model.py b/rattail_corepos/corepos/importing/model.py index 8729c25..17bac9b 100644 --- a/rattail_corepos/corepos/importing/model.py +++ b/rattail_corepos/corepos/importing/model.py @@ -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 diff --git a/rattail_corepos/corepos/importing/rattail.py b/rattail_corepos/corepos/importing/rattail.py index db752ce..272f8d6 100644 --- a/rattail_corepos/corepos/importing/rattail.py +++ b/rattail_corepos/corepos/importing/rattail.py @@ -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): diff --git a/rattail_corepos/corepos/util.py b/rattail_corepos/corepos/util.py index a1e9e89..06b01fa 100644 --- a/rattail_corepos/corepos/util.py +++ b/rattail_corepos/corepos/util.py @@ -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 diff --git a/rattail_corepos/importing/corepos/api.py b/rattail_corepos/importing/corepos/api.py index cc40e7b..cec8493 100644 --- a/rattail_corepos/importing/corepos/api.py +++ b/rattail_corepos/importing/corepos/api.py @@ -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):