From 5749394f4004b3a4201b19f244112ff13cb68575 Mon Sep 17 00:00:00 2001 From: Cameron Higby-Naquin Date: Fri, 6 Mar 2020 16:10:22 -0500 Subject: [PATCH 1/3] Copy affiliation data to incident targets We are phasing out the one-affiliation-per-incident affiliation model in favor of an Institution model, which can either be applied on a journalist by journalist basis, or to the incident as a whole, but as many as desired. For this, we must move data from the legacy affiliation field to the new Institution field. We do this using a data migration, with two pre- and post- migrations to work around this bug in djang-modelcluster: https://github.com/wagtail/django-modelcluster/issues/96 For the migration, we want these steps: 1. For each incident, examine the `affiliation` field 2. If it null or set to the default value of "Independent," discard it. The "independent" value is a placeholder for no definite affiliation and will not be used in the new scheme. 3. Create, if it doesn't already exist, an Institution with the title of the affiliation. 4. If this incident has any targeted journalists who do not already have an associated institution, associate the new institution with them. 5. If this incident does *not* have any targeted journalists, add the new institution as a target of this incident. This will fully bring any affiliation data into the institution model and apply it to the incidents that this data described. --- .../migrations/0043_auto_20200306_1646.py | 18 +++++++++ .../0044_copy_affiliation_to_institutions.py | 38 +++++++++++++++++++ .../migrations/0045_auto_20200306_1648.py | 19 ++++++++++ 3 files changed, 75 insertions(+) create mode 100644 incident/migrations/0043_auto_20200306_1646.py create mode 100644 incident/migrations/0044_copy_affiliation_to_institutions.py create mode 100644 incident/migrations/0045_auto_20200306_1648.py diff --git a/incident/migrations/0043_auto_20200306_1646.py b/incident/migrations/0043_auto_20200306_1646.py new file mode 100644 index 000000000..5f8f342a7 --- /dev/null +++ b/incident/migrations/0043_auto_20200306_1646.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.10 on 2020-03-06 16:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('incident', '0042_copy_uncategorized_targets_to_gov_workers'), + ] + + operations = [ + migrations.AlterField( + model_name='incidentpage', + name='targeted_institutions', + field=models.ManyToManyField(blank=True, related_name='institutions_incidents', to='incident.Institution', verbose_name='Targeted Institutions'), + ), + ] diff --git a/incident/migrations/0044_copy_affiliation_to_institutions.py b/incident/migrations/0044_copy_affiliation_to_institutions.py new file mode 100644 index 000000000..5a6004faf --- /dev/null +++ b/incident/migrations/0044_copy_affiliation_to_institutions.py @@ -0,0 +1,38 @@ +# Generated by Django 2.2.10 on 2020-03-05 21:28 + +from django.db import migrations + + +def copy_affiliation(apps, schema_editor): + """Copy incident affiliations to """ + IncidentPage = apps.get_model('incident', 'IncidentPage') + Institution = apps.get_model('incident', 'Institution') + + for incident in IncidentPage.objects.order_by('pk'): + if incident.affiliation and incident.affiliation != 'Independent': + inst, _ = Institution.objects.get_or_create(title=incident.affiliation) + + targeted_journos = incident.targeted_journalists.all() + if targeted_journos: + for tj in targeted_journos: + if not tj.institution: + tj.institution = inst + tj.save() + else: + incident.targeted_institutions.add(inst) + incident.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('incident', '0043_auto_20200306_1646'), + ] + + operations = [ + migrations.RunPython( + copy_affiliation, + reverse_code=migrations.RunPython.noop, + elidable=True, + ), + ] diff --git a/incident/migrations/0045_auto_20200306_1648.py b/incident/migrations/0045_auto_20200306_1648.py new file mode 100644 index 000000000..8b5a29630 --- /dev/null +++ b/incident/migrations/0045_auto_20200306_1648.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.10 on 2020-03-06 16:48 + +from django.db import migrations +import modelcluster.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('incident', '0044_copy_affiliation_to_institutions'), + ] + + operations = [ + migrations.AlterField( + model_name='incidentpage', + name='targeted_institutions', + field=modelcluster.fields.ParentalManyToManyField(blank=True, related_name='institutions_incidents', to='incident.Institution', verbose_name='Targeted Institutions'), + ), + ] From 36bf7a7de9038c1255d954a8740276755e522c55 Mon Sep 17 00:00:00 2001 From: Cameron Higby-Naquin Date: Fri, 6 Mar 2020 16:28:28 -0500 Subject: [PATCH 2/3] Remove affiliation information from the incident page sidebar Since this information is subsumed by the institution data, I'm feeling like it should be removed from display as well. --- incident/templates/incident/incident_page.html | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/incident/templates/incident/incident_page.html b/incident/templates/incident/incident_page.html index 92a013f97..500187245 100644 --- a/incident/templates/incident/incident_page.html +++ b/incident/templates/incident/incident_page.html @@ -83,19 +83,6 @@

Incident Data

{% endif %} - {% if page.affiliation %} - - - Affiliation - - - - {{ page.affiliation }} - - - - {% endif %} - {% if page.city or page.state %} From fe9565332a181e552746ba84f43a6b4c7687339e Mon Sep 17 00:00:00 2001 From: Harris Lapiroff Date: Tue, 7 Apr 2020 17:35:12 -0400 Subject: [PATCH 3/3] Renumber migrations --- .../{0043_auto_20200306_1646.py => 0044_auto_20200306_1646.py} | 2 +- ...institutions.py => 0045_copy_affiliation_to_institutions.py} | 2 +- .../{0045_auto_20200306_1648.py => 0046_auto_20200306_1648.py} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename incident/migrations/{0043_auto_20200306_1646.py => 0044_auto_20200306_1646.py} (86%) rename incident/migrations/{0044_copy_affiliation_to_institutions.py => 0045_copy_affiliation_to_institutions.py} (95%) rename incident/migrations/{0045_auto_20200306_1648.py => 0046_auto_20200306_1648.py} (88%) diff --git a/incident/migrations/0043_auto_20200306_1646.py b/incident/migrations/0044_auto_20200306_1646.py similarity index 86% rename from incident/migrations/0043_auto_20200306_1646.py rename to incident/migrations/0044_auto_20200306_1646.py index 5f8f342a7..5adf21284 100644 --- a/incident/migrations/0043_auto_20200306_1646.py +++ b/incident/migrations/0044_auto_20200306_1646.py @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('incident', '0042_copy_uncategorized_targets_to_gov_workers'), + ('incident', '0043_auto_20200226_1558'), ] operations = [ diff --git a/incident/migrations/0044_copy_affiliation_to_institutions.py b/incident/migrations/0045_copy_affiliation_to_institutions.py similarity index 95% rename from incident/migrations/0044_copy_affiliation_to_institutions.py rename to incident/migrations/0045_copy_affiliation_to_institutions.py index 5a6004faf..57871b7b1 100644 --- a/incident/migrations/0044_copy_affiliation_to_institutions.py +++ b/incident/migrations/0045_copy_affiliation_to_institutions.py @@ -26,7 +26,7 @@ def copy_affiliation(apps, schema_editor): class Migration(migrations.Migration): dependencies = [ - ('incident', '0043_auto_20200306_1646'), + ('incident', '0044_auto_20200306_1646'), ] operations = [ diff --git a/incident/migrations/0045_auto_20200306_1648.py b/incident/migrations/0046_auto_20200306_1648.py similarity index 88% rename from incident/migrations/0045_auto_20200306_1648.py rename to incident/migrations/0046_auto_20200306_1648.py index 8b5a29630..6ddfd553c 100644 --- a/incident/migrations/0045_auto_20200306_1648.py +++ b/incident/migrations/0046_auto_20200306_1648.py @@ -7,7 +7,7 @@ class Migration(migrations.Migration): dependencies = [ - ('incident', '0044_copy_affiliation_to_institutions'), + ('incident', '0045_copy_affiliation_to_institutions'), ] operations = [