summaryrefslogtreecommitdiff
path: root/ishtar_common/data_importer.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-02-18 15:50:43 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-02-18 15:50:43 +0100
commitcc37287f27f591004b79436cb46d842f852e8da0 (patch)
tree90ba61f83d0062c4b5487827526f77f71d582dde /ishtar_common/data_importer.py
parentfcb00f3d1f09f86f9819a4128f43594c5712bd4b (diff)
downloadIshtar-cc37287f27f591004b79436cb46d842f852e8da0.tar.bz2
Ishtar-cc37287f27f591004b79436cb46d842f852e8da0.zip
Importer: INSEE code formater
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r--ishtar_common/data_importer.py29
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()