1
+ import logging
2
+
1
3
from django .db .models import Max
2
4
from import_export import resources
3
5
from import_export .fields import Field
4
- from import_export .widgets import ForeignKeyWidget , ManyToManyWidget
5
6
from import_export .results import RowResult
7
+ from import_export .widgets import ForeignKeyWidget , ManyToManyWidget
6
8
7
9
from base .models import GeographicScope , Geography , Pathogen , SeverityPyramidRung
8
10
from base .resources import GEOGRAPHIC_GRANULARITY_MAPPING
13
15
Indicator ,
14
16
IndicatorGeography ,
15
17
IndicatorType ,
18
+ NonDelphiIndicator ,
19
+ OtherEndpointIndicator ,
16
20
)
17
- from indicatorsets .models import IndicatorSet
21
+ from indicatorsets .models import IndicatorSet , NonDelphiIndicatorSet
22
+
23
+ logger = logging .getLogger (__name__ )
18
24
19
25
20
26
def fix_boolean_fields (row ) -> None :
@@ -29,11 +35,11 @@ def fix_boolean_fields(row) -> None:
29
35
]
30
36
31
37
for field in fields :
32
- if row [ field ] == "TRUE" :
38
+ if row . get ( field , "" ) == "TRUE" :
33
39
row [field ] = True
34
- elif row [ field ] == "FALSE" :
40
+ elif row . get ( field , "" ) == "FALSE" :
35
41
row [field ] = False
36
- elif row [ field ] == "" :
42
+ elif row . get ( field , None ) == "" :
37
43
row [field ] = False
38
44
return row
39
45
@@ -225,18 +231,27 @@ def import_row(self, row, instance_loader, **kwargs):
225
231
return import_result
226
232
227
233
234
+ class PermissiveForeignKeyWidget (ForeignKeyWidget ):
235
+
236
+ def clean (self , value , row = None , ** kwargs ):
237
+ try :
238
+ return super ().clean (value )
239
+ except self .model .DoesNotExist :
240
+ logger .warning (f"instance matching '{ value } ' does not exist" )
241
+
242
+
228
243
class IndicatorBaseResource (ModelResource ):
229
244
name = Field (attribute = "name" , column_name = "Signal" )
230
245
display_name = Field (attribute = "display_name" , column_name = "Name" )
231
246
base = Field (
232
247
attribute = "base" ,
233
248
column_name = "base" ,
234
- widget = ForeignKeyWidget (Indicator , field = "id" ),
249
+ widget = PermissiveForeignKeyWidget (Indicator , field = "id" ),
235
250
)
236
251
source = Field (
237
252
attribute = "source" ,
238
253
column_name = "Source Subdivision" ,
239
- widget = ForeignKeyWidget (SourceSubdivision , field = "name" ),
254
+ widget = PermissiveForeignKeyWidget (SourceSubdivision , field = "name" ),
240
255
)
241
256
242
257
class Meta :
@@ -271,13 +286,13 @@ class IndicatorResource(ModelResource):
271
286
indicator_type = Field (
272
287
attribute = "indicator_type" ,
273
288
column_name = "Indicator Type" ,
274
- widget = ForeignKeyWidget (IndicatorType ),
289
+ widget = PermissiveForeignKeyWidget (IndicatorType ),
275
290
)
276
291
active = Field (attribute = "active" , column_name = "Active" )
277
292
format_type = Field (
278
293
attribute = "format_type" ,
279
294
column_name = "Format" ,
280
- widget = ForeignKeyWidget (FormatType ),
295
+ widget = PermissiveForeignKeyWidget (FormatType ),
281
296
)
282
297
time_type = Field (attribute = "time_type" , column_name = "Time Type" )
283
298
time_label = Field (attribute = "time_label" , column_name = "Time Label" )
@@ -299,12 +314,12 @@ class IndicatorResource(ModelResource):
299
314
category = Field (
300
315
attribute = "category" ,
301
316
column_name = "Category" ,
302
- widget = ForeignKeyWidget (Category ),
317
+ widget = PermissiveForeignKeyWidget (Category ),
303
318
)
304
319
geographic_scope = Field (
305
320
attribute = "geographic_scope" ,
306
321
column_name = "Geographic Coverage" ,
307
- widget = ForeignKeyWidget (GeographicScope ),
322
+ widget = PermissiveForeignKeyWidget (GeographicScope ),
308
323
)
309
324
available_geographies = Field (
310
325
attribute = "available_geographies" ,
@@ -332,7 +347,7 @@ class IndicatorResource(ModelResource):
332
347
source = Field (
333
348
attribute = "source" ,
334
349
column_name = "Source Subdivision" ,
335
- widget = ForeignKeyWidget (SourceSubdivision ),
350
+ widget = PermissiveForeignKeyWidget (SourceSubdivision ),
336
351
)
337
352
data_censoring = Field (attribute = "data_censoring" , column_name = "Data Censoring" )
338
353
missingness = Field (attribute = "missingness" , column_name = "Missingness" )
@@ -349,7 +364,7 @@ class IndicatorResource(ModelResource):
349
364
indicator_set = Field (
350
365
attribute = "indicator_set" ,
351
366
column_name = "Indicator Set" ,
352
- widget = ForeignKeyWidget (IndicatorSet , field = "name" ),
367
+ widget = PermissiveForeignKeyWidget (IndicatorSet , field = "name" ),
353
368
)
354
369
355
370
class Meta :
@@ -425,7 +440,7 @@ class OtherEndpointIndicatorResource(ModelResource):
425
440
attribute = "short_description" , column_name = "Short Description"
426
441
)
427
442
description = Field (attribute = "description" , column_name = "Description" )
428
- member_name = Field (attribute = "member_name" , column_name = "Member API Name" )
443
+ member_name = Field (attribute = "member_name" , column_name = "Member Name" )
429
444
member_short_name = Field (
430
445
attribute = "member_short_name" , column_name = "Member Short Name"
431
446
)
@@ -440,13 +455,13 @@ class OtherEndpointIndicatorResource(ModelResource):
440
455
indicator_type = Field (
441
456
attribute = "indicator_type" ,
442
457
column_name = "Indicator Type" ,
443
- widget = ForeignKeyWidget (IndicatorType ),
458
+ widget = PermissiveForeignKeyWidget (IndicatorType ),
444
459
)
445
460
active = Field (attribute = "active" , column_name = "Active" )
446
461
format_type = Field (
447
462
attribute = "format_type" ,
448
463
column_name = "Format" ,
449
- widget = ForeignKeyWidget (FormatType ),
464
+ widget = PermissiveForeignKeyWidget (FormatType ),
450
465
)
451
466
time_type = Field (attribute = "time_type" , column_name = "Time Type" )
452
467
time_label = Field (attribute = "time_label" , column_name = "Time Label" )
@@ -468,12 +483,12 @@ class OtherEndpointIndicatorResource(ModelResource):
468
483
category = Field (
469
484
attribute = "category" ,
470
485
column_name = "Category" ,
471
- widget = ForeignKeyWidget (Category ),
486
+ widget = PermissiveForeignKeyWidget (Category ),
472
487
)
473
488
geographic_scope = Field (
474
489
attribute = "geographic_scope" ,
475
490
column_name = "Geographic Coverage" ,
476
- widget = ForeignKeyWidget (GeographicScope ),
491
+ widget = PermissiveForeignKeyWidget (GeographicScope ),
477
492
)
478
493
available_geographies = Field (
479
494
attribute = "available_geographies" ,
@@ -501,7 +516,7 @@ class OtherEndpointIndicatorResource(ModelResource):
501
516
source = Field (
502
517
attribute = "source" ,
503
518
column_name = "Source Subdivision" ,
504
- widget = ForeignKeyWidget (SourceSubdivision ),
519
+ widget = PermissiveForeignKeyWidget (SourceSubdivision ),
505
520
)
506
521
data_censoring = Field (attribute = "data_censoring" , column_name = "Data Censoring" )
507
522
missingness = Field (attribute = "missingness" , column_name = "Missingness" )
@@ -518,11 +533,11 @@ class OtherEndpointIndicatorResource(ModelResource):
518
533
indicator_set = Field (
519
534
attribute = "indicator_set" ,
520
535
column_name = "Indicator Set" ,
521
- widget = ForeignKeyWidget (IndicatorSet , field = "name" ),
536
+ widget = PermissiveForeignKeyWidget (IndicatorSet , field = "name" ),
522
537
)
523
538
524
539
class Meta :
525
- model = Indicator
540
+ model = OtherEndpointIndicator
526
541
fields : list [str ] = [
527
542
"name" ,
528
543
"display_name" ,
@@ -585,3 +600,36 @@ def skip_row(self, instance, original, row, import_validation_errors=None):
585
600
586
601
def after_import_row (self , row , row_result , ** kwargs ):
587
602
process_indicator_geography (row )
603
+
604
+
605
+ class NonDelphiIndicatorResource (resources .ModelResource ):
606
+
607
+ name = Field (attribute = "name" , column_name = "Indicator Name" )
608
+ display_name = Field (attribute = "display_name" , column_name = "Indicator Name" )
609
+ member_name = Field (attribute = "member_name" , column_name = "Indicator API Name" )
610
+ description = Field (attribute = "description" , column_name = "Indicator Description" )
611
+ indicator_set = Field (
612
+ attribute = "indicator_set" ,
613
+ column_name = "Indicator Set" ,
614
+ widget = PermissiveForeignKeyWidget (NonDelphiIndicatorSet , field = "name" ),
615
+ )
616
+
617
+ class Meta :
618
+ model = NonDelphiIndicator
619
+ fields : list [str ] = [
620
+ "name" ,
621
+ "display_name" ,
622
+ "member_name" ,
623
+ "description" ,
624
+ "indicator_set" ,
625
+ ]
626
+ import_id_fields : list [str ] = ["name" ]
627
+ skip_unchanged = True
628
+
629
+ def before_import_row (self , row , ** kwargs ) -> None :
630
+ """Post-processes each row after importing."""
631
+ fix_boolean_fields (row )
632
+
633
+ def skip_row (self , instance , original , row , import_validation_errors = None ):
634
+ if not row ["Include in indicator app" ]:
635
+ return True
0 commit comments