diff options
| author | Étienne Loks <etienne.loks@proxience.com> | 2015-03-06 11:15:13 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@proxience.com> | 2015-05-06 16:06:35 +0200 | 
| commit | 663a93f51a3119c6f0dcf00b3f94a645b6c5f62b (patch) | |
| tree | 9408423a8a8036027df54d4c52d8ee8635fb3d3c | |
| parent | 6616c6af04a6fd77d1334eda8c87a84954624e22 (diff) | |
| download | Ishtar-663a93f51a3119c6f0dcf00b3f94a645b6c5f62b.tar.bz2 Ishtar-663a93f51a3119c6f0dcf00b3f94a645b6c5f62b.zip | |
Data importation: add an option to select the default choice (for testing)
| -rw-r--r-- | archaeological_operations/management/commands/ishtar_imports.py | 13 | ||||
| -rw-r--r-- | ishtar_common/data_importer.py | 31 | 
2 files changed, 30 insertions, 14 deletions
| diff --git a/archaeological_operations/management/commands/ishtar_imports.py b/archaeological_operations/management/commands/ishtar_imports.py index 23397204b..3f4d9e2e8 100644 --- a/archaeological_operations/management/commands/ishtar_imports.py +++ b/archaeological_operations/management/commands/ishtar_imports.py @@ -18,6 +18,7 @@  # See the file COPYING for details.  import datetime, unicodecsv +from optparse import make_option  from django.conf import settings  from django.core.management.base import BaseCommand, CommandError @@ -54,6 +55,14 @@ except ImportError:  class Command(BaseCommand):      args = '<filename> <importer_name> [<nb lines skipped>]'      help = "Import archaeological operations" +    option_list = BaseCommand.option_list + ( +        make_option('--choose-default', +            action='store_true', +            dest='choose_default', +            default=False, +            help='When a choice is requested choose the first one available. '\ +                 'For testing purpose'), +        )      def handle(self, *args, **options):          if not args or not args[0]: @@ -68,6 +77,7 @@ class Command(BaseCommand):              skip_lines = int(args[2])          except:              skip_lines = 0 +        choose_default = options.get('choose_default')          filename = args[0]          importer = IMPORTERS[args[1]](skip_lines=skip_lines, output='cli')          sys.stdout.write("*" * 72 + "\n") @@ -82,7 +92,8 @@ class Command(BaseCommand):              for encoding in encodings:                  try:                      importer.importation([line for line in -                            unicodecsv.reader(csv_file, encoding='utf-8')]) +                            unicodecsv.reader(csv_file, encoding='utf-8')], +                            choose_default=choose_default)                      errors = importer.get_csv_errors()                      sys.stdout.write("\n")                      if errors: diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 09c511a25..b70e76ffc 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -63,14 +63,15 @@ class ImportFormater(object):      def report_error(self, *args):          return -    def init(self, vals, output=None): +    def init(self, vals, output=None, choose_default=False):          try:              lst = iter(self.formater)          except TypeError:              lst = [self.formater]          for formater in lst:              if formater: -                formater.check(vals, output, self.comment) +                formater.check(vals, output, self.comment, +                               choose_default=choose_default)      def post_process(self, obj, context, value, owner=None):          raise NotImplemented() @@ -92,7 +93,7 @@ class Formater(object):      def format(self, value):          return value -    def check(self, values, output=None, comment=''): +    def check(self, values, output=None, comment='', choose_default=False):          return  class ChoiceChecker(object): @@ -245,8 +246,8 @@ class StrChoiceFormater(Formater, ChoiceChecker):          msgstr += unicode(_(u"%d. None of the above - skip")) % idx + u"\n"          return msgstr, idx -    def check(self, values, output=None, comment=''): -        if not output or output == 'silent': +    def check(self, values, output=None, comment='', choose_default=False): +        if (not output or output == 'silent') and not choose_default:              return          if self.many_split:              new_values = [] @@ -259,11 +260,13 @@ class StrChoiceFormater(Formater, ChoiceChecker):              value = self.prepare(value)              if value in self.equiv_dict:                  continue -            if output != 'cli': +            if output != 'cli' and not choose_default:                  self.missings.add(value)                  continue              msgstr, idx = self._get_choices(comment)              res = None +            if choose_default: +                res = 1              while res not in range(1, idx+1):                  msg = msgstr % value                  sys.stdout.write(msg.encode('utf-8')) @@ -398,8 +401,8 @@ class StrToBoolean(Formater, ChoiceChecker):              value = slugify(value)          return value -    def check(self, values, output=None, comment=''): -        if not output or output == 'silent': +    def check(self, values, output=None, comment='', choose_default=False): +        if (not output or output == 'silent') and not choose_default:              return          msgstr = comment + u" - "          msgstr += unicode(_(u"Choice for \"%s\" is not available. "\ @@ -411,10 +414,12 @@ class StrToBoolean(Formater, ChoiceChecker):              value = self.prepare(value)              if value in self.dct:                  continue -            if not self.cli: +            if output != 'cli' and not choose_default:                  self.missings.add(value)                  continue              res = None +            if choose_default: +                res = 1              while res not in range(1, 4):                  msg = msgstr % value                  sys.stdout.write(msg.encode('utf-8')) @@ -519,7 +524,7 @@ class Importer(object):      def post_processing(self, item, data):          return item -    def initialize(self, table, output='silent'): +    def initialize(self, table, output='silent', choose_default=False):          """          copy vals in columns and initialize formaters          * output: @@ -541,11 +546,11 @@ class Importer(object):                  vals[idx_col].append(val)          for idx, formater in enumerate(self.line_format):              if formater: -                formater.init(vals[idx], output) +                formater.init(vals[idx], output, choose_default=choose_default) -    def importation(self, table, initialize=True): +    def importation(self, table, initialize=True, choose_default=False):          if initialize: -            self.initialize(table, self.output) +            self.initialize(table, self.output, choose_default=choose_default)          self._importation(table)      @classmethod | 
