Changeset - 0b1a8f9617ed
[Not reviewed]
0 2 1
Lance Edgar - 5 years ago 2019-07-29 15:33:05
ledgar@techsupport.coop
Add basic `crepes import-csv` command, for CSV -> CORE import
3 files changed with 86 insertions and 0 deletions:
0 comments (0 inline, 0 general) First comment
rattail_corepos/corepos/commands.py
Show inline comments
 
@@ -147,3 +147,35 @@ class ImportCore(ImportToCore):
 
            if 'args' in kwargs:
 
                kwargs['dbkey'] = kwargs['args'].dbkey
 
        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)
rattail_corepos/corepos/importing/csv.py
Show inline comments
 
new file 100644
 
# -*- 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])
setup.py
Show inline comments
 
@@ -107,6 +107,7 @@ setup(
 
            'export-core = rattail_corepos.corepos.commands:ExportCore',
 
            'export-csv = rattail_corepos.corepos.commands:ExportCSV',
 
            'import-core = rattail_corepos.corepos.commands:ImportCore',
 
            'import-csv = rattail_corepos.corepos.commands:ImportCSV',
 
        ],
 

	
 
        'rattail.config.extensions': [
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now