Changeset - 86f851c41745
[Not reviewed]
0 1 0
Lance Edgar (lance) - 3 years ago 2022-02-06 11:23:24
lance@edbob.org
Add `match_on_time_field` for Trainwreck importers

we should match on whichever time field is "most reliable" from the
host transaction data. for instance some POS systems include an
`upload_time` value which is most preferable, but we use `end_time` as
the default since it's more likely to exist in all systems.
1 file changed with 32 insertions and 10 deletions:
0 comments (0 inline, 0 general)
rattail/trainwreck/importing/model.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2021 Lance Edgar
 
#  Copyright © 2010-2022 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -26,12 +26,9 @@ Trainwreck model importers
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import datetime
 

	
 
from sqlalchemy import orm
 

	
 
from rattail import importing
 
from rattail.time import localtime, make_utc
 
from .util import ToOrFromTrainwreck
 

	
 

	
 
@@ -45,7 +42,18 @@ class ToTrainwreck(ToOrFromTrainwreck, importing.ToSQLAlchemy):
 
class TransactionImporter(ToTrainwreck):
 
    """
 
    Transaction data importer
 

	
 
    .. attribute:: match_on_time_field
 

	
 
       This time field will be used for the cache query etc.  In
 
       particular it controls which transactions are deemed to belong
 
       to a given date, which is needed when restricting the import to
 
       a particular date range.  Can set this to ``'upload_time'`` or
 
       ``'start_time'`` if necessary, depending on the nature of
 
       transaction data coming from the host side.
 
    """
 
    match_on_time_field = 'end_time'
 

	
 
    @property
 
    def importing_from_system(self):
 
        raise NotImplementedError("TODO: please define this for your subclass")
 
@@ -64,14 +72,25 @@ class TransactionImporter(ToTrainwreck):
 
            pass
 
        else:
 
            query = query.filter(self.model_class.system == system)
 
        return query.filter(self.model_class.end_time >= make_utc(self.start_time))\
 
                    .filter(self.model_class.end_time < make_utc(self.end_time))
 
        time_field = getattr(self.model_class, self.match_on_time_field)
 
        return query.filter(time_field >= self.app.make_utc(self.start_time))\
 
                    .filter(time_field < self.app.make_utc(self.end_time))
 

	
 

	
 
class TransactionItemImporter(ToTrainwreck):
 
    """
 
    Transaction item data importer
 

	
 
    .. attribute:: match_on_time_field
 

	
 
       This time field will be used for the cache query etc.  In
 
       particular it controls which transactions are deemed to belong
 
       to a given date, which is needed when restricting the import to
 
       a particular date range.  Can set this to ``'upload_time'`` or
 
       ``'start_time'`` if necessary, depending on the nature of
 
       transaction data coming from the host side.
 
    """
 
    match_on_time_field = 'end_time'
 

	
 
    @property
 
    def supported_fields(self):
 
@@ -109,8 +128,10 @@ class TransactionItemImporter(ToTrainwreck):
 
                pass
 
            else:
 
                query = query.filter(trainwreck.Transaction.system == system)
 
            query = query.filter(trainwreck.Transaction.end_time >= make_utc(self.start_time))\
 
                         .filter(trainwreck.Transaction.end_time < make_utc(self.end_time))
 

	
 
            time_field = getattr(trainwreck.Transaction, self.match_on_time_field)
 
            query = query.filter(time_field >= self.app.make_utc(self.start_time))\
 
                         .filter(time_field < self.app.make_utc(self.end_time))
 
            self.transactions_by_system_id = self.cache_model(trainwreck.Transaction,
 
                                                              query=query,
 
                                                              key='system_id')
 
@@ -131,9 +152,10 @@ class TransactionItemImporter(ToTrainwreck):
 
            pass
 
        else:
 
            query = query.filter(trainwreck.Transaction.system == system)
 
        time_field = getattr(trainwreck.Transaction, self.match_on_time_field)
 
        return query.join(trainwreck.Transaction)\
 
                    .filter(trainwreck.Transaction.end_time >= make_utc(self.start_time))\
 
                    .filter(trainwreck.Transaction.end_time < make_utc(self.end_time))
 
                    .filter(time_field >= self.app.make_utc(self.start_time))\
 
                    .filter(time_field < self.app.make_utc(self.end_time))
 

	
 
    def get_transaction_by_system_id(self, system_id):
 
        if hasattr(self, 'transactions_by_system_id'):
0 comments (0 inline, 0 general)