# Path: promises_instances/csv_loader.py
# class DDAHCSVProcessor(CsvProcessor):
#     def __init__(self, file_, instance, **kwargs):
#         self.instance = instance
#         cat_qs = DDAHCategory.objects.filter(instance=self.instance)
#         promise_qs = DDAHPromise.objects.filter(instance=self.instance)
#         kwargs['category_qs'] = cat_qs
#         kwargs['promise_qs'] = promise_qs
#         kwargs['instance'] = self.instance
#         super(DDAHCSVProcessor, self).__init__(file_, **kwargs)
# 
# Path: promises_instances/models.py
# class DDAHInstance(Instance):
#     pass


from django.core.management.base import BaseCommand
from distutils.version import StrictVersion
from promises_instances.csv_loader import DDAHCSVProcessor
from promises_instances.models import DDAHInstance
import codecs
import django


class Command(BaseCommand):
    help = 'Imports a csv file into promises'

    def add_arguments(self, parser):
        parser.add_argument('csv_file', nargs='+', type=str)
        parser.add_argument('instance_label', nargs='+', type=str)

    def handle(self, *args, **options):
        if not StrictVersion(django.get_version()) < StrictVersion("1.8"):
            file_name = options['csv_file'][0]
            label = options['instance_label'][0]
        else:
            file_name = args[0]
            label = args[1]
        file_ = codecs.open(file_name)
        instance = DDAHInstance.objects.get(label=label)

# Path: promises_instances/csv_loader.py
# class DDAHCSVProcessor(CsvProcessor):
#     def __init__(self, file_, instance, **kwargs):
#         self.instance = instance
#         cat_qs = DDAHCategory.objects.filter(instance=self.instance)
#         promise_qs = DDAHPromise.objects.filter(instance=self.instance)
#         kwargs['category_qs'] = cat_qs
#         kwargs['promise_qs'] = promise_qs
#         kwargs['instance'] = self.instance
#         super(DDAHCSVProcessor, self).__init__(file_, **kwargs)
# 
# Path: promises_instances/models.py
# class DDAHInstance(Instance):
#     pass


# Path: promises_instances/management/commands/ddah_importer.py
from django.core.management.base import BaseCommand
from distutils.version import StrictVersion
from promises_instances.csv_loader import DDAHCSVProcessor
from promises_instances.models import DDAHInstance
import codecs
import django



class Command(BaseCommand):
    help = 'Imports a csv file into promises'

    def add_arguments(self, parser):
        parser.add_argument('csv_file', nargs='+', type=str)
        parser.add_argument('instance_label', nargs='+', type=str)

    def handle(self, *args, **options):
        if not StrictVersion(django.get_version()) < StrictVersion("1.8"):
            file_name = options['csv_file'][0]
            label = options['instance_label'][0]
        else:
            file_name = args[0]
            label = args[1]
        file_ = codecs.open(file_name)
        instance = DDAHInstance.objects.get(label=label)

        processor = DDAHCSVProcessor(file_, instance)