From a1c98cef45e6854bb25f94bc7a21e2a206347af2 Mon Sep 17 00:00:00 2001 From: Jeferson Moura Date: Wed, 9 May 2018 11:29:44 +0200 Subject: [PATCH 1/2] Update some disaggregation fields to be protected --- .../migrations/0009_auto_20180509_0140.py | 26 +++++++++++++++++++ indicators/models.py | 4 +-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 indicators/migrations/0009_auto_20180509_0140.py diff --git a/indicators/migrations/0009_auto_20180509_0140.py b/indicators/migrations/0009_auto_20180509_0140.py new file mode 100644 index 000000000..134b9f0d1 --- /dev/null +++ b/indicators/migrations/0009_auto_20180509_0140.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.3 on 2018-05-09 08:40 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('indicators', '0008_disaggregationvalue_indicator'), + ] + + operations = [ + migrations.AlterField( + model_name='disaggregationlabel', + name='disaggregation_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='indicators.DisaggregationType'), + ), + migrations.AlterField( + model_name='disaggregationvalue', + name='disaggregation_label', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='indicators.DisaggregationLabel'), + ), + ] diff --git a/indicators/models.py b/indicators/models.py index d3125b33f..ff4db3102 100755 --- a/indicators/models.py +++ b/indicators/models.py @@ -192,7 +192,7 @@ class DisaggregationTypeAdmin(admin.ModelAdmin): class DisaggregationLabel(models.Model): - disaggregation_type = models.ForeignKey(DisaggregationType) + disaggregation_type = models.ForeignKey(DisaggregationType, on_delete=models.deletion.PROTECT) label = models.CharField(max_length=765, blank=True) customsort = models.IntegerField(blank=True, null=True) create_date = models.DateTimeField(null=True, blank=True) @@ -210,7 +210,7 @@ def save(self, *args, **kwargs): class DisaggregationValue(models.Model): - disaggregation_label = models.ForeignKey(DisaggregationLabel) + disaggregation_label = models.ForeignKey(DisaggregationLabel, on_delete=models.deletion.PROTECT) indicator = models.ForeignKey('indicators.Indicator', null=True, blank=True) value = models.CharField(max_length=765, blank=True) create_date = models.DateTimeField(null=True, blank=True) From c51ff1c26454452b2060f052be1d40d40cb2e4a2 Mon Sep 17 00:00:00 2001 From: Jeferson Moura Date: Wed, 9 May 2018 14:46:51 +0200 Subject: [PATCH 2/2] Add disaggregation to the indicator factory --- factories/indicators_models.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/factories/indicators_models.py b/factories/indicators_models.py index f291ad4ce..bfd4a1c70 100644 --- a/factories/indicators_models.py +++ b/factories/indicators_models.py @@ -68,6 +68,17 @@ def workflowlevel1(self, create, extracted, **kwargs): for workflowlevel1 in extracted: self.workflowlevel1.add(workflowlevel1) + @post_generation + def disaggregation(self, create, extracted, **kwargs): + if not create: + # Simple build, do nothing. + return + + if extracted: + # A list of disaggregation were passed in, use them + for disaggregation in extracted: + self.disaggregation.add(disaggregation) + class Objective(DjangoModelFactory): class Meta: