summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ishtar_common/data_importer.py29
-rw-r--r--ishtar_common/models_imports.py4
2 files changed, 32 insertions, 1 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()
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py
index cd868cc4a..4a5654781 100644
--- a/ishtar_common/models_imports.py
+++ b/ishtar_common/models_imports.py
@@ -42,7 +42,7 @@ from ishtar_common.utils import create_slug, \
get_all_related_m2m_objects_with_model
from ishtar_common.data_importer import Importer, ImportFormater, \
IntegerFormater, FloatFormater, UnicodeFormater, DateFormater, \
- TypeFormater, YearFormater, StrToBoolean, FileFormater
+ TypeFormater, YearFormater, StrToBoolean, FileFormater, InseeFormater
logger = logging.getLogger(__name__)
@@ -641,6 +641,7 @@ IMPORTER_TYPES = (
('DateFormater', _(u"Date")),
('TypeFormater', _(u"Type")),
('YearFormater', _(u"Year")),
+ ('InseeFormater', _(u"INSEE code")),
('StrToBoolean', _(u"String to boolean")),
('FileFormater', pgettext_lazy("filesystem", u"File")),
('UnknowType', _(u"Unknow type"))
@@ -655,6 +656,7 @@ IMPORTER_TYPES_DCT = {
'YearFormater': YearFormater,
'StrToBoolean': StrToBoolean,
'FileFormater': FileFormater,
+ 'InseeFormater': InseeFormater,
'UnknowType': None,
}