@@ -63,6 +63,9 @@ class OpenDataSoftQuery(ExternalApiQuery):
6363 identify_metropoles = models .BooleanField (
6464 verbose_name = "Identifier les noms de métropoles" , default = False
6565 )
66+ identify_years = models .BooleanField (
67+ verbose_name = "Identifier les années de publication" , default = False
68+ )
6669
6770 class Meta :
6871 verbose_name = "OpenDataSoft — requête"
@@ -151,6 +154,9 @@ def create_or_update_document(self, result: dict) -> None:
151154 if self .identify_metropoles :
152155 new_doc .identify_metropoles (full_text )
153156
157+ if self .identify_years :
158+ self .get_years_from_keywords (new_doc , metadata ["keyword" ])
159+
154160 def apply_mappings (self , document : Document , metadata : dict ) -> None :
155161 """
156162 Applies the selected mappings to the document
@@ -171,27 +177,37 @@ def add_metadata_with_mapping(
171177 metadata_values = metadata [field ]
172178 for mapping_value , properties in mapping_values .items ():
173179 if mapping_value in metadata_values :
174- if isinstance (properties , list ):
175- for property in properties :
176- self .add_metadata_properties (document , property )
177- else :
178- self .add_metadata_properties (document , properties )
180+ self .add_metadata_properties (document , properties )
179181
180182 def add_metadata_properties (self , document : Document , properties : dict ) -> None :
181183 """
182184 Add metadata from a properties dict
183185 """
184- if properties ["type" ] == "topic" :
185- document .topics .add (properties ["value" ])
186- elif properties ["type" ] == "scope" :
187- document .scope .add (properties ["value" ])
188- elif properties ["type" ] == "publication_pages" :
189- document .publication_pages .add (properties ["value" ])
190- elif properties ["type" ] == "region" :
191- document .regions .add (properties ["value" ])
192- elif properties ["type" ] == "departement" :
193- document .departements .add (properties ["value" ])
186+ for key , values in properties .items ():
187+ if not isinstance (values , list ):
188+ values = [values ]
189+
190+ if key == "topic" :
191+ document .topics .add (* values )
192+ elif key == "scope" :
193+ document .scope .add (* values )
194+ elif key == "publication_pages" :
195+ document .publication_pages .add (* values )
196+ elif key == "region" :
197+ document .regions .add (* values )
198+ elif key == "departement" :
199+ document .departements .add (* values )
200+
201+ document .save ()
194202
203+ def get_years_from_keywords (self , document : Document , keywords : dict ) -> None :
204+ """
205+ Tries to identify publications years from the keywords dict
206+ """
207+ for keyword in keywords :
208+ if keyword .isnumeric () and int (keyword ) >= 2000 and int (keyword ) <= 2100 :
209+ year , year_is_new = DataYear .objects .get_or_create (year = keyword )
210+ document .years .add (year )
195211 document .save ()
196212
197213
0 commit comments