From cc37287f27f591004b79436cb46d842f852e8da0 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Sun, 18 Feb 2018 15:50:43 +0100 Subject: Importer: INSEE code formater --- ishtar_common/data_importer.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'ishtar_common/data_importer.py') 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() -- cgit v1.2.3