summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@peacefrogs.net>2013-05-07 19:22:23 +0200
committerÉtienne Loks <etienne.loks@peacefrogs.net>2013-05-07 19:22:23 +0200
commitc5cff744df1ee138ddf77fc2b683e4f252b77ad5 (patch)
treea5449faaaf5e6b034152fcaea7fcb23165f76947
parent31c3a9a2e17db368a54e570eb1a2982b29849484 (diff)
downloadIshtar-c5cff744df1ee138ddf77fc2b683e4f252b77ad5.tar.bz2
Ishtar-c5cff744df1ee138ddf77fc2b683e4f252b77ad5.zip
First work on parcel imports
-rw-r--r--archaeological_operations/import_from_csv.py40
-rw-r--r--archaeological_operations/tests.py68
-rw-r--r--example_project/local_settings_nantes.py2
-rw-r--r--example_project/settings.py2
4 files changed, 108 insertions, 4 deletions
diff --git a/archaeological_operations/import_from_csv.py b/archaeological_operations/import_from_csv.py
index 9d3a58ffd..e61596b00 100644
--- a/archaeological_operations/import_from_csv.py
+++ b/archaeological_operations/import_from_csv.py
@@ -410,6 +410,43 @@ def parse_rapp_index(value):
if items:
return int(items[-1])
+PARCEL_YEAR_REGEXP = re.compile(r"^([0-9]{4})[ :]+")
+
+PARCEL_SECTION_REGEXP = re.compile(r"([A-Z0-9]{1,3})[ :]+ *(([0-9]+[a-z]?[ ,à]*[et]*)+)")
+PARCEL_NB_REGEXP = re.compile(r'([0-9]+[a-z]?)[ ,à]*[et]*')
+
+def parse_parcels(insee_code, parcel_str, owner):
+ parcels = []
+ town = parse_insee(insee_code)
+ # manage only one town at a time
+ assert len(town) < 2
+ if not town:
+ return parcels
+ town = town[0]
+ parcel_values = {}
+ m = PARCEL_YEAR_REGEXP.match(parcel_str)
+ year = None
+ if m:
+ year = m.groups()[0]
+ parcel_str = parcel_str[m.span()[1]:]
+ parcel_values[year] = []
+ parcels_str = [parcel_str]
+ if ';' in parcel_str:
+ parcels_str = parcel_str.split(';')
+ if '/' in parcel_str:
+ parcels_str = parcel_str.split('/')
+ for parcel_str in parcels_str:
+ m = PARCEL_SECTION_REGEXP.match(parcel_str)
+ while m:
+ gp = m.groups()
+ sector, nums = gp[0], gp[1]
+ for num in PARCEL_NB_REGEXP.findall(nums):
+ parcel_values[year].append((sector, num))
+ parcel_str = parcel_str[m.span()[1]:]
+ m = PARCEL_SECTION_REGEXP.match(parcel_str)
+ print parcel_values
+ return parcels
+
_CACHED_DOC_TYPES = {}
def parse_doc_types(value):
@@ -479,7 +516,8 @@ def ope_postimportfix(ope, dct):
return ope
class RelatedClass:
- def __init__(self, key, cls, default_data={}, reverse_key=False, unique_keys=[]):
+ def __init__(self, key, cls, default_data={}, reverse_key=False,
+ unique_keys=[]):
self.key, self.cls, self.default_data = key, cls, default_data
self.reverse_key, self.unique_keys = reverse_key, unique_keys
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index 61e7695f9..bec490122 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -28,7 +28,7 @@ from django.test import TestCase
from django.contrib.auth.models import User
import models
-from ishtar_common.models import OrganizationType, Organization
+from ishtar_common.models import OrganizationType, Organization, Town
class ImportOperationTest(TestCase):
fixtures = ['../ishtar_common/fixtures/initial_data.json',
@@ -51,3 +51,69 @@ class ImportOperationTest(TestCase):
"""
call_command('import_operations', os.sep.join([os.getcwd(), '..',
'archaeological_operations', 'tests', 'sample.csv']))
+
+ def testParseParcels(self):
+ # the database needs to be initialised before importing
+ from archaeological_operations.import_from_csv import parse_parcels
+ default_town = Town.objects.create(numero_insee="12345",
+ name="default_town")
+ test_values = (
+ ("1996 : XT:53,54,56,57,59,60,61,62",
+ {1996:[
+ ("XT", "53"),
+ ("XT", "54"),
+ ("XT", "56"),
+ ("XT", "57"),
+ ("XT", "59"),
+ ("XT", "60"),
+ ("XT", "61"),
+ ("XT", "62"),
+ ]}),
+ ("AD:23",
+ {None:[
+ ("AD", "23")
+ ]}),
+ ("1961 :B1:227;", {}
+ ),("1982 CV:35;CV:36", {}
+ ),("E:24;E:25", {}
+ ),("B : 375, 376, 386, 387, 645, 646 / C : 412 à 415, 432 à 435, 622 / F : 120, 149, 150, 284, 287, 321 à 323", {}
+ ),("AD : 95, 96, 86, 87, 81, 252, AE : 58, AD : 115 à 132", {}
+ ),("XD:1 à 13, 24 à 28, 33 à 39, 50 à 52, 80, 83, 84 à 86, 259 à 261, 182, 225 ; XH:5 ; P:1640, 1888, 1889, 1890 ; R:1311, 1312, 1314, 1342, 1343, 1559 à 1569", {}
+ ),("BZ:2 à 5, 365 ; CD:88 à 104, 106, 108, 326", {}
+ ),("AV 118 à 125, 127, 132 à 137, 153, 398p, 399, 402; BI 27, 30, 32, 33, 188, 255, 256 à 258, 260, 284p, 294; BL 297", {}
+ ),("A : 904 à 906, 911 ; E:40, 41", {}
+ ),("1991 : BE:8, 12", {}
+ ),("AB 37 et 308", {}
+ ),("1979 : EM:1", {}
+ ),("B:448;B:449;B:450;B:451;B:452;B:455;B:456;B:457;B:458;B:459;B:1486;", {}
+ ),("AC : 72 à 81, 91 à 100, 197 / ZC:180 à 189", {}
+ ),("A : 1195, 1203 à 1208, 1338 ; ZC : 11 à 13, 16, 18, 22, 23, 25 à 31, 45, 82, 103, 107, 109 ; ZA : 2 à 8, 10, 11, 34 à 36, 49 à 41, 57, 58, Z:21", {}
+ ),("1983 D2 n° 458 et 459", {}
+ ),("ZS : 21p, 66", {}
+ ),("VV:166, 167, domaine public", {}
+ ),("Domaine public", {}
+ ),("Tranche 1 : AV:4 à 6, 18, 80, 104 / partiellement : 5 et 18", {}
+ ),(" AS:13 à 15, 17 à 19, 21 à 32, 34 à 45, 47 à 53, 69, 70, 82, 84 / CK:1, 24, 25, 29, 30, 37 à 43", {}
+ ),(" ZE, 16, 17, 83, 10, 18, 82, 9, 73, 76, 77, 79, 80, 84, 78 et 81", {}
+ ),(" ZN:37, 15, 35, 28, 29 / ZM:9, 73", {}
+ ),("A:26a, 26b, 27 / AB:95 / AK:4, 12, 20", {}
+ ),(" Tranche n°1 : YP:243, 12, 14 à 16, 18 à 26, DP / Tranche n°2 : YP:17, 307, 27, 308, 44 à 46, 683, BM:1, 250, 488 à 492", {}
+ ),(" 1987 : ZD: ?", {}
+ ),(" H : 106, 156, 158", {}
+ ))
+ for value, result in test_values:
+ parcels = parse_parcels("12345", value, None)
+ """
+ self.assertTrue(parcels != [])
+ parcels_copy = parcels[:]
+ for year in result.keys():
+ for parcel in parcels_copy:
+ if parcel.year != year:
+ continue
+ self.assertTrue((parcel.section, parcel.parcel_number)
+ in result[year])
+ parcel.pop(parcel.index(parcel))
+ # all parcels have been imported
+ self.assertEqual(parcels, [])
+ """
+
diff --git a/example_project/local_settings_nantes.py b/example_project/local_settings_nantes.py
index 1b9b9a537..b96b1404c 100644
--- a/example_project/local_settings_nantes.py
+++ b/example_project/local_settings_nantes.py
@@ -199,7 +199,7 @@ ISHTAR_OPE_COL_FORMAT = [
None, # pass
(('towns',), 'parse_insee'),
None, # pass
- None, # (('parcels',), 'parse_parcels'),
+ (('parcels',), 'parse_parcels', [77], True),
None, # pass
None, # pass
None, # pass
diff --git a/example_project/settings.py b/example_project/settings.py
index bfb59fb5d..9dc52868c 100644
--- a/example_project/settings.py
+++ b/example_project/settings.py
@@ -102,7 +102,7 @@ INSTALLED_APPS = [
'django.contrib.formtools',
'south',
'registration',
- 'geodjangofla',
+ #'geodjangofla',
'ishtar_common',
'archaeological_operations', # mandatory app to run ishtar
#'django_extensions',