Jelajahi Sumber

Amélioration des modèles

olivier.massot 7 tahun lalu
induk
melakukan
262ed5fa23
2 mengubah file dengan 52 tambahan dan 29 penghapusan
  1. 22 2
      core/cerberus_extend.py
  2. 30 27
      schemas/netgeo_2_2_doe/models.py

+ 22 - 2
core/cerberus_extend.py

@@ -3,6 +3,7 @@
 @author: olivier.massot, 2018
 '''
 from datetime import datetime
+import re
 
 import cerberus
 
@@ -18,6 +19,25 @@ def is_french_date(field, value, error):
 
 class GeoValidator(cerberus.validator.Validator):
     
+    def __init__(self, *args, **kwargs):
+        super(GeoValidator, self).__init__(*args, **kwargs)
+        
+        # Rends tous les champs requis par défaut, à moins que 'required' ait été défini dans le schéma
+        for field in self.schema:
+            if not 'required' in self.schema[field]:
+                self.schema[field]['required'] = True
+        
+    def _validate_multiallowed(self, allowed, field, value):
+        """ Comme 'allowed', mais autorise plusieurs valeurs séparées par un '-'
+
+        The rule's arguments are validated against this schema:
+        {'type': 'list'}
+        """
+        for item in re.split("\s?-\s?", value):
+            if not item in allowed:
+                self._error(field, "Valeur non-autorisée: {}".format(item))
+        
+    
     def _validate_geometry(self, geom, field, value):
         """ Contrôle la géométrie d'un objet
 
@@ -57,8 +77,8 @@ class CerberusErrorHandler(cerberus.errors.BasicErrorHandler):
                 0x24: "Doit être du type: {constraint}",
                 0x25: "Doit être de type 'dictionnaire'",
                 0x26: "La longueur de la liste doit être de {constraint}, elle est de {0}",
-                0x27: "La longueur minimum est de {constraint}",
-                0x28: "La longueur maximum est de {constraint}",
+                0x27: "La longueur minimum du champs est de {constraint}",
+                0x28: "Trop long, la longueur max. du champs est de {constraint}",
 
                 0x41: "La valeur n'est pas au bon format ('{constraint}')",
                 0x42: "La valeur minimum autorisée est {constraint}",

+ 30 - 27
schemas/netgeo_2_2_doe/models.py

@@ -12,7 +12,7 @@ class Artere(BaseGeoModel):
     filename = "artere_geo.shp"
     pk = "AR_CODE"
     schema = {'geom': {'geometry': (gis.POLYLINE, (XMIN, YMIN, XMAX, YMAX))}, 
-              'AR_CODE': {'type': 'string', 'maxlength': 26}, 
+              'AR_CODE': {'required': False, 'type': 'string', 'maxlength': 26}, 
               'AR_NOM': {'type': 'string', 'maxlength': 26}, 
               'AR_ID_INSE': {'type': 'string', 'empty': False, 'allowed': INSEE_VALIDES}, 
               'AR_LONG': {'type': 'float'}, 
@@ -21,19 +21,21 @@ class Artere(BaseGeoModel):
               'AR_NOEUD_A': {'type': 'string', 'maxlength': 20}, 
               'AR_NOEUD_B': {'type': 'string', 'maxlength': 20}, 
               'AR_NB_FOUR': {'type': 'string', 'maxlength': 20}, 
-              'AR_FOU_DIS': {'type': 'integer'}, 
-              'AR_TYPE_FO': {'type': 'string', 'empty': False, 'allowed': ['PVC', 'PEHD', 'SOUS-TUBAGE PEHD', 'SOUS-TUBAGE  SOUPLE', 'FACADE', 'AERIEN', 'ENCORBELLEMENT', 'AUTRE']}, 
-              'AR_TYFO_AI': {'type': 'string', 'empty': False, 'allowed': ['PVC', 'PEH', 'TUB', 'FAC', 'ENC', 'APP']}, 
-              'AR_DIAM_FO': {'type': 'string', 'empty': False, 'allowed': ['10', '14', '18', '25', '28', '32', '40', '45', '60', 'NUL']}, 
-              'AR_PRO_FOU': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'AUTRE (à préciser)']}, 
-              'AR_FAB': {'type': 'string', 'maxlength': 100}, 'AR_REFFAB': {'type': 'string', 'maxlength': 100}, 
-              'AR_COULEUR': {'type': 'string', 'maxlength': 20}, 'AR_AIGUIL': {'type': 'string', 'empty': False, 'allowed': ['OUI', 'NON']}, 
+              'AR_FOU_DIS': {'coerce': int, 'type': 'integer'}, 
+              'AR_TYPE_FO': {'type': 'string', 'empty': False, 'multiallowed': ['PVC', 'PEHD', 'SOUS-TUBAGE PEHD', 'SOUS-TUBAGE  SOUPLE', 'FACADE', 'AERIEN', 'ENCORBELLEMENT', 'AUTRE']}, 
+              'AR_TYFO_AI': {'type': 'string', 'empty': False, 'multiallowed': ['PVC', 'PEH', 'TUB', 'FAC', 'ENC', 'APP']}, 
+              'AR_DIAM_FO': {'type': 'string', 'empty': False, 'multiallowed': ['10', '14', '18', '25', '28', '32', '40', '45', '60', 'NUL']}, 
+              'AR_PRO_FOU': {'type': 'string', 'empty': False, 'multiallowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'AUTRE (à préciser)']}, 
+              'AR_FAB': {'type': 'string', 'maxlength': 100}, 
+              'AR_REFFAB': {'type': 'string', 'maxlength': 100}, 
+              'AR_COULEUR': {'type': 'string', 'maxlength': 20}, 
+              'AR_AIGUIL': {'type': 'string', 'empty': False, 'allowed': ['OUI', 'NON']}, 
               'AR_NBCABL': {'type': 'integer'}, 
               'AR_PRO_CAB': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE']}, 
-              'AR_GEST_FO': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'AUTRE (à préciser)', 'NUL']}, 
-              'AR_UTIL_FO': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'AUTRE (à préciser)', 'NUL']}, 
-              'AR_DATE_IN': {'validator': is_french_date}, 
-              'AR_DATE_RE': {'validator': is_french_date}, 
+              'AR_GEST_FO': {'type': 'string', 'empty': False, 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'AUTRE (à préciser)', 'NUL']}, 
+              'AR_UTIL_FO': {'type': 'string', 'empty': False, 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'AUTRE (à préciser)', 'NUL']}, 
+              'AR_DATE_IN': {'empty': False, 'validator': is_french_date}, 
+              'AR_DATE_RE': {'empty': False, 'validator': is_french_date}, 
               'AR_REF_PLA': {'type': 'string', 'maxlength': 100}, 
               'AR_SRC_GEO': {'type': 'string', 'maxlength': 50}, 
               'AR_QLT_GEO': {'type': 'string', 'empty': False, 'allowed': ['A', 'B', 'C']}, 
@@ -57,13 +59,14 @@ class Cable(BaseGeoModel):
               'CA_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
               'CA_LONG': {'type': 'float'}, 'CA_EQ_A': {'type': 'string', 'maxlength': 18}, 
               'CA_EQ_B': {'type': 'string', 'maxlength': 18}, 
-              'CA_DIAMETR': {'type': 'integer'}, 
+              'CA_DIAMETR': {'coerce': int, 'type': 'integer'}, 
               'CA_COULEUR': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['NOIR', 'BLEU', 'BLANC']}, 
               'CA_TECHNOL': {'type': 'string', 'maxlength': 17, 'empty': False, 'allowed': ['G657A2_M6', 'G657A2_M12']}, 
-              'A_NB_FO_U': {'type': 'integer'}, 'CA_NB_FO_D': {'type': 'integer'}, 
+              'CA_NB_FO_U': {'coerce': int, 'type': 'integer'}, 
+              'CA_NB_FO_D': {'coerce': int, 'type': 'integer'}, 
               'CA_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE']}, 
               'CA_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['NUL']}, 
-              'CA_DATE_IN': {'validator': is_french_date}, 
+              'CA_DATE_IN': {'empty': False, 'validator': is_french_date}, 
               'CA_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True}, 
               'CA_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
 
@@ -78,8 +81,8 @@ class Equipement(BaseGeoModel):
               'EQ_EMPRISE': {'type': 'string', 'maxlength': 7}, 
               'EQ_FABR': {'type': 'string', 'maxlength': 100}, 
               'EQ_CAPFO': {'type': 'integer'}, 
-              'EQ_NBMXEQ': {'type': 'integer'}, 
-              'EQ_NBCAB': {'type': 'integer'}, 
+              'EQ_NBMXEQ': {'coerce': int, 'type': 'integer'}, 
+              'EQ_NBCAB': {'coerce': int, 'type': 'integer'}, 
               'EQ_DIMENS': {'type': 'string', 'maxlength': 50}, 
               'EQ_TYPEQ': {'type': 'string', 'maxlength': 100}, 
               'EQ_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
@@ -91,7 +94,7 @@ class Equipement(BaseGeoModel):
               'EQ_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'AUTRE (à préciser)', 'NUL']}, 
               'EQ_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'AUTRE (à préciser)', 'NUL']}, 
               'EQ_HAUT': {'type': 'float'}, 
-              'EQ_DATE_IN': {'validator': is_french_date}, 
+              'EQ_DATE_IN': {'empty': False, 'validator': is_french_date}, 
               'EQ_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True}, 
               'EQ_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
 
@@ -111,15 +114,15 @@ class Noeud(BaseGeoModel):
               'NO_TYPE_LQ': {'type': 'string', 'maxlength': 10, 'empty': False, 'allowed': ['CHTIR', 'CHRACC', 'POT', 'NRO', 'PM', 'MIMO', 'FAC', 'OUV', 'IMM']}, 
               'NO_TYPE_PH': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['CHAMBRE', 'POTEAU', 'ARMOIRE', 'SHELTER', 'BATIMENT', 'SITE MIMO', 'FACADE', 'OUVRAGE', 'IMMEUBLE']}, 
               'NO_CODE_PH': {'type': 'string', 'maxlength': 20}, 
-              'NO_TECH_PS': {'type': 'string', 'maxlength': 4, 'empty': False, 'allowed': ['COAX', 'CUT', 'ECL', 'ELEC', 'VP', 'OPT', 'NC']}, 
+              'NO_TECH_PS': {'type': 'string', 'maxlength': 4, 'empty': False, 'multiallowed': ['COAX', 'CUT', 'ECL', 'ELEC', 'VP', 'OPT', 'NC']}, 
               'NO_AMO': {'type': 'string', 'maxlength': 20}, 
               'NO_PLINOX': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['OUI', 'NON']}, 
-              'NO_X': {'type': 'float'}, 
-              'NO_Y': {'type': 'float'}, 
+              'NO_X': {'coerce': float, 'type': 'float'}, 
+              'NO_Y': {'coerce': float, 'type': 'float'}, 
               'NO_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'ERDF', 'AUTRE (à préciser)', 'NUL']}, 
               'NO_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'ERDF', 'MANCHE FIBRE', 'AUTRE (à préciser)', 'NUL']}, 
               'NO_HAUT': {'type': 'float'}, 
-              'NO_DATE_IN': {'validator': is_french_date}, 
+              'NO_DATE_IN': {'empty': False, 'validator': is_french_date}, 
               'NO_REF_PLA': {'type': 'string', 'maxlength': 100}, 
               'NO_SRC_GEO': {'type': 'string', 'maxlength': 50}, 
               'NO_QLT_GEO': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['A', 'B', 'C']}, 
@@ -138,17 +141,17 @@ class Tranchee(BaseGeoModel):
               'TR_TYP_IMP': {'type': 'string', 'empty': False, 'allowed': ['ACCOTEMENT STABILISE', 'ACCOTEMENT NON STABILISE', 'CHAUSSEE LOURDE', 'CHAUSSEE LEGERE', 'FOSSE', 'TROTTOIR', 'ESPACE VERT', 'ENCORBELLEMENT']}, 
               'TR_MOD_POS': {'type': 'string', 'empty': False, 'allowed': ['TRADITIONNEL', 'MICRO TRANCHEE', 'FONCAGE 60', 'FONCAGE 90', 'FONCAGE 120', 'TRANCHEUSE', 'FORAGE URBAIN', 'FORAGE RURAL', 'ENCORBELLEMENT']}, 
               'TR_MPOS_AI': {'type': 'string', 'empty': False, 'allowed': ['TRA', 'ALL', 'FONCAGE 60', 'FON', 'FOR', 'ENC']}, 
-              'TR_LONG': {'type': 'float'}, 
-              'TR_LARG': {'type': 'float'}, 
+              'TR_LONG': {'coerce': float, 'type': 'float'}, 
+              'TR_LARG': {'coerce': float, 'type': 'float'}, 
               'TR_REVET': {'type': 'string', 'empty': False, 'allowed': ['SABLE', 'BICOUCHE', 'ENROBE', 'BETON', 'PAVE', 'TERRAIN NATUREL']}, 
-              'TR_CHARGE': {'type': 'float'}, 
-              'TR_GRILLAG': {'type': 'float'}, 
+              'TR_CHARGE': {'coerce': float, 'type': 'float'}, 
+              'TR_GRILLAG': {'coerce': float, 'type': 'float'}, 
               'TR_REMBLAI': {'type': 'string'}, 
               'TR_PLYNOX': {'type': 'string', 'empty': False, 'allowed': ['OUI', 'NON']}, 
               'TR_PRO_VOI': {'type': 'string', 'empty': False, 'allowed': ['COMMUNE', 'COMMUNAUTE DE COMMUNES', 'DEPARTEMENT', 'ETAT', 'PRIVE']}, 
               'TR_GEST_VO': {'type': 'string', 'empty': False, 'allowed': ['COMMUNE', 'COMMUNAUTE DE COMMUNES', 'DEPARTEMENT', 'ETAT', 'PRIVE']}, 
               'TR_SCHEMA': {'maxlength': 100, 'type': 'string'}, 
-              'TR_DATE_IN': {'validator': is_french_date}, 
+              'TR_DATE_IN': {'empty': False, 'validator': is_french_date}, 
               'TR_SRC_GEO': {'type': 'string', 'maxlength': 50}, 
               'TR_QLT_GEO': {'type': 'string', 'empty': False, 'allowed': ['A', 'B', 'C']}, 
               'TR_PRO_MD': {'type': 'string', 'maxlength': 20},