Always establish start/end date+time range for all importers

some need it, some don't, but helps to standardize, esp. since the
importer commands all accept --start-date and --end-date params
This commit is contained in:
Lance Edgar 2022-01-27 21:07:48 -06:00
parent b0c1b2f19d
commit f8c499a97d
2 changed files with 35 additions and 33 deletions

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.
#
@ -26,6 +26,7 @@ Data Importers
from __future__ import unicode_literals, absolute_import
import datetime
import logging
import six
@ -191,6 +192,37 @@ class Importer(object):
self.delete = kwargs.pop('delete', False) and self.allow_delete
for key, value in kwargs.items():
setattr(self, key, value)
self.establish_date_range()
def establish_date_range(self):
now = self.app.localtime()
today = now.date()
# start time defaults to midnight this morning, unless specified
if not hasattr(self, 'start_time') or not self.start_time:
if not hasattr(self, 'start_date') or not self.start_date:
if (hasattr(self, 'args')
and hasattr(self.args, 'start_date')
and self.args.start_date):
self.start_date = self.args.start_date
else:
self.start_date = today
start_time = datetime.datetime.combine(self.start_date,
datetime.time(0))
self.start_time = self.app.localtime(start_time)
# end_time defaults to midnight tonight, unless specified
if not hasattr(self, 'end_time') or not self.end_time:
if not hasattr(self, 'end_date') or not self.end_date:
if (hasattr(self, 'args')
and hasattr(self.args, 'end_date')
and self.args.end_date):
self.end_date = self.args.end_date
else:
self.end_date = today
end_date = self.end_date + datetime.timedelta(days=1)
end_time = datetime.datetime.combine(end_date, datetime.time(0))
self.end_time = self.app.localtime(end_time)
def setup(self):
"""

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.
#
@ -26,39 +26,9 @@ Trainwreck importing - shared utilities
from __future__ import unicode_literals, absolute_import
import datetime
from rattail.time import localtime, make_utc
# TODO: deprecate / remove this
class ToOrFromTrainwreck(object):
"""
Common base class for ToTrainwreck and FromTrainwreck base clases.
"""
def setup(self):
super(ToOrFromTrainwreck, self).setup()
now = localtime(self.config)
today = now.date()
# start time defaults to midnight this morning, unless specified
if not hasattr(self, 'start_time'):
if not hasattr(self, 'start_date'):
if self.args.start_date:
self.start_date = self.args.start_date
else:
self.start_date = today
start_time = datetime.datetime.combine(self.start_date, datetime.time(0))
self.start_time = localtime(self.config, start_time)
# end_time defaults to midnight tonight, unless specified
if not hasattr(self, 'end_time'):
if not hasattr(self, 'end_date'):
if self.args.end_date:
self.end_date = self.args.end_date
else:
self.end_date = today
end_date = self.end_date + datetime.timedelta(days=1)
end_time = datetime.datetime.combine(end_date, datetime.time(0))
self.end_time = localtime(self.config, end_time)