diff options
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r-- | ishtar_common/data_importer.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index b2dd0d5a0..ffe6c221d 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -269,6 +269,35 @@ class FloatFormater(Formater): 'value': value}) +class InseeFormater(Formater): + """ + Formater for "code INSEE" (statistic institute ine France) + The syntax "CodeINSEE-Year" is accepted (Ishtar trick) in order to manage + old INSEE (year is the date of creation) + """ + ERROR = _(u"\"{value}\" is not an appropriate INSEE code") + + def format(self, value): + value = value.strip() + exp = value.split('-') + code = exp[0] + try: + int(code) + except ValueError: + raise ValueError(unicode(self.ERROR).format(value)) + while len(code) < 5: + code = "0" + code + if len(exp) > 2: + raise ValueError(unicode(self.ERROR).format(value)) + elif len(exp) == 1: + return code + try: + datetime.datetime.strptime(exp[1], '%Y') + except ValueError: + raise ValueError(unicode(self.ERROR).format(value)) + return code + u"-" + exp[1] + + class YearFormater(Formater): def format(self, value): value = value.strip() |