diff options
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r-- | ishtar_common/data_importer.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 427b9f974..3009ba17d 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -29,6 +29,8 @@ from ishtar_common.unicode_csv import UnicodeWriter NEW_LINE_BREAK = '#####@@@#####' +RE_FILTER_CEDEX = re.compile("(.*) *(?: *CEDEX|cedex|Cedex|Cédex|cédex *\d*)") + class ImportFormater(object): def __init__(self, field_name, formater=None, required=True, through=None, through_key=None, through_dict=None, through_unicity_keys=None, @@ -236,6 +238,17 @@ class StrChoiceFormater(Formater): if value in self.equiv_dict: return self.equiv_dict[value] +class TypeFormater(StrChoiceFormater): + def __init__(self, model, cli=False): + self.equiv_dict, self.choices = {}, [] + for item in model.objects.all(): + self.choices.append((item.pk, unicode(item))) + for key in item.get_keys(): + self.equiv_dict[key] = item + + def prepare(self, value): + return slugify(unicode(value).strip()) + class DateFormater(Formater): def __init__(self, date_format="%d/%m/%Y"): self.date_format = date_format @@ -248,6 +261,54 @@ class DateFormater(Formater): raise ValueError(_(u"\"%(value)s\" is not a valid date") % { 'value':value}) +class StrToBoolean(Formater): + def __init__(self, choices={}, cli=False, strict=False): + self.dct = copy.copy(choices) + self.cli = cli + self.strict= strict + self.missings = set() + + def prepare(self, value): + value = unicode(value).strip() + if not self.strict: + value = slugify(value) + return value + + def check(self, values, output=None): + if not output or output == 'silent': + return + msgstr = unicode(_(u"Choice for \"%s\" is not available. "\ + u"Which one is relevant?\n")) + msgstr += u"1. True\n" + msgstr += u"2. False\n" + msgstr += u"3. Empty\n" + for value in values: + value = self.prepare(value) + if value in self.dct: + continue + if not self.cli: + self.missings.add(value) + continue + res = None + while res not in range(1, 4): + sys.stdout.write(msgstr % value) + res = raw_input(">>> ") + try: + res = int(res) + except ValueError: + pass + if res == 1: + self.dct[value] = True + elif res == 2: + self.dct[value] = False + else: + self.dct[value] = None + + def format(self, value): + value = self.prepare(value) + if value in self.dct: + return self.dct[value] + logger = logging.getLogger(__name__) class Importer(object): |