Add basic crepes import-csv command, for CSV -> CORE import

This commit is contained in:
Lance Edgar 2019-07-29 15:33:05 -05:00
parent 83b1f93d99
commit 0b1a8f9617
3 changed files with 86 additions and 0 deletions

View file

@ -147,3 +147,35 @@ class ImportCore(ImportToCore):
if 'args' in kwargs: if 'args' in kwargs:
kwargs['dbkey'] = kwargs['args'].dbkey kwargs['dbkey'] = kwargs['args'].dbkey
return kwargs return kwargs
class ImportCSV(commands.ImportFileSubcommand):
"""
Import data from CSV file(s) to CORE database
"""
name = 'import-csv'
description = __doc__.strip()
default_handler_spec = 'rattail_corepos.corepos.importing.csv:FromCSVToCore'
def add_parser_args(self, parser):
super(ImportCSV, self).add_parser_args(parser)
parser.add_argument('--dbkey', metavar='KEY', default='default',
help="Config key for database engine to be used as the \"target\" "
"CORE DB, i.e. where data will be imported *to*. This key must be "
"defined in the [rattail_corepos.db] section of your config file.")
def get_handler_kwargs(self, **kwargs):
kwargs = super(ImportCSV, self).get_handler_kwargs(**kwargs)
if 'args' in kwargs:
args = kwargs['args']
kwargs['dbkey'] = args.dbkey
return kwargs
def get_handler_factory(self, **kwargs):
if self.config:
spec = self.config.get('rattail_corepos.importing', 'csv.handler',
default=self.default_handler_spec)
else:
# just use default, for sake of cmd line help
spec = self.default_handler_spec
return load_object(spec)

View file

@ -0,0 +1,53 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2019 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/>.
#
################################################################################
"""
CSV -> CORE data import
"""
from __future__ import unicode_literals, absolute_import
from corepos.db import model as corepos, Session as CoreSession
from rattail.importing.handlers import FromFileHandler
from rattail.importing.csv import FromCSVToSQLAlchemyMixin
from rattail_corepos.corepos.importing.model import ToCore
from rattail_corepos.corepos.importing.corepos import ToCoreHandler
class FromCSVToCore(FromCSVToSQLAlchemyMixin, FromFileHandler, ToCoreHandler):
"""
Handler for CSV -> CORE data import
"""
host_title = "CSV"
local_title = "CORE"
ToParent = ToCore
@property
def local_title(self):
return "CORE ({})".format(self.dbkey)
def get_model(self):
return corepos
def make_session(self):
return CoreSession(bind=self.config.corepos_engines[self.dbkey])

View file

@ -107,6 +107,7 @@ setup(
'export-core = rattail_corepos.corepos.commands:ExportCore', 'export-core = rattail_corepos.corepos.commands:ExportCore',
'export-csv = rattail_corepos.corepos.commands:ExportCSV', 'export-csv = rattail_corepos.corepos.commands:ExportCSV',
'import-core = rattail_corepos.corepos.commands:ImportCore', 'import-core = rattail_corepos.corepos.commands:ImportCore',
'import-csv = rattail_corepos.corepos.commands:ImportCSV',
], ],
'rattail.config.extensions': [ 'rattail.config.extensions': [