# coding: utf-8 import os from django.core.management.base import BaseCommand from comptabilite.forms import import_csv_transaction # CLASS_MAPPER = { # 'crelan' : forms.Crelan, # 'keytrade': forms.KeyTrade # } class Command(BaseCommand): help = "Import transaction's csv file." def add_arguments(self, parser): parser.add_argument( "-b", "--bank", type=str, help="Indicates the file's bank origin." ) parser.add_argument("-f", "--file", type=str, help="CSV file's path.") def handle(self, *args, **options): # if not os.path.exists(filepath) # raise IOError if not os.path.isfile(options["file"]): print("File seems not exist. Please check the file (name, extension, …)") return try: f = open(options["file"]) f.close() except IOError: print("File not accessible") return # form_class = CLASS_MAPPER.get(options['bank'], None) # if not form_class: # print("Bah on n'a pas trouvé la classe du formulaire.") # return # with open(file, 'r') as csvfile: # rows = csv.reader(csvfile, MyDialect()) # for row in rows: # current_form = form_class(row) # if form.is_valid(): # form.save() # records_added += 1 if options["bank"] == "crelan" or options["bank"] == "keytrade": records_added, error_list = import_csv_transaction( options["file"], options["bank"] ) print("%s transactions ont été importées" % records_added) for error in error_list: print(error) else: print("Format non supporté…")