We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Move to IATI country list by default
We now have our own country list in RSR, and IATI also uses a country list (which we also use for some fields, such as recipient country). I think we should completely switch to the IATI country list, but that also involves some other changes (filters on RSR pages, etc).
- [x] Projects
  - [x] Remove the rsr_projectlocation.country field from the project editor
  - [x] Remove all mentions of rsr_projectlocation.country in the validation sets
  - [x] Do not hide recipient countries and regions in the RSR validation set
  - [x] Convert any country of a project location to a recipient country
  - [x] Remove the country field as a mandatory field for publishing in RSR
  - [x] Update project page
    - [x] Project header
    - [x] Full report tab (remove country field from location)
    - [x] Donation thanks page
  - [x] Update projects list
    - [x] Update mention of country per project
    - [x] Update location filter
  - [x] Update IATI import
  - [x] Add comment to state that location.country is legacy
- [x] Project updates
  - [x] Remove project update location countries (not used)
  - [x] Remove location filter on updates list
- [x] Organisations
  - [x] Add new iati_country field
  - [x] Convert existing countries to IATI countries
  - [x] Replace country field in 'old' organisation admin
  - [x] Add comment to state that location.country is legacy
  - [x] Update location filter on organisations list
  - [x] Update organisations list
- [x] Employments
  - [x] Convert the employment countries to IATI countries
  - [x] Update 'Request to join an organisation' module
- [x] Make all migrations reversable


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `akvo/rsr/migrations/0073_auto_20160525_1541.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3 
4 from django.db import models, migrations
5 
6 
7 def convert_org_locations_to_iati_countries(apps, schema_editor):
8     """
9     Convert the organisation location countries to the new 'iati_country' field.
10     """
11     OrganisationLocation = apps.get_model("rsr", "OrganisationLocation")
12 
13     for location in OrganisationLocation.objects.all():
14         country = location.country
15         if country:
16             location.iati_country = country.iso_code.upper()
17             location.save(update_fields=['iati_country', ])
18 
19 
20 def convert_org_locations_to_iati_countries_reverse(apps, schema_editor):
21     """
22     Revert the organisation location countries to the old 'country' field.
23     """
24     OrganisationLocation = apps.get_model("rsr", "OrganisationLocation")
25     Country = apps.get_model("rsr", "Country")
26 
27     for location in OrganisationLocation.objects.all():
28         country = location.iati_country
29         if country:
30             try:
31                 location.country = Country.objects.get(iso_code=country.lower())
32                 location.save(update_fields=['country', ])
33             except Country.DoesNotExist:
34                 pass
35 
36 
37 def convert_employments_to_iati_countries(apps, schema_editor):
38     """
39     Convert the employment countries to the 'new_country_field' field. This field is renamed in
40     the following migration.
41     """
42     Employment = apps.get_model("rsr", "Employment")
43 
44     for employment in Employment.objects.all():
45         country = employment.country
46         if country:
47             employment.new_country_field = country.iso_code.upper()
48             employment.save(update_fields=['new_country_field', ])
49 
50 
51 def convert_employments_to_iati_countries_reverse(apps, schema_editor):
52     """
53     Revert the employment countries to the old 'country' field.
54     """
55     Employment = apps.get_model("rsr", "Employment")
56     Country = apps.get_model("rsr", "Country")
57 
58     for employment in Employment.objects.all():
59         new_country_field = employment.new_country_field
60         if new_country_field:
61             employment.country = Country.objects.get(iso_code=new_country_field.lower())
62             employment.save(update_fields=['country', ])
63 
64 
65 class Migration(migrations.Migration):
66 
67     dependencies = [
68         ('rsr', '0072_auto_20160525_1528'),
69     ]
70 
71     operations = [
72         migrations.RunPython(convert_org_locations_to_iati_countries,
73                              convert_org_locations_to_iati_countries_reverse),
74         migrations.RunPython(convert_employments_to_iati_countries,
75                              convert_employments_to_iati_countries_reverse),
76     ]
77
```

Path: `akvo/rsr/migrations/0071_auto_20160525_1036.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3 
4 from django.db import models, migrations
5 
6 
7 def remove_country_from_validation_sets(apps, schema_editor):
8     """
9     Removes the rsr_projectlocation.country field from all validation sets.
10     """
11     ProjectEditorValidationSet = apps.get_model("rsr", "ProjectEditorValidationSet")
12     validations_to_be_deleted = []
13 
14     for validation_set in ProjectEditorValidationSet.objects.all():
15         for validation in validation_set.validations.all():
16             if 'rsr_projectlocation.country' in validation.validation:
17                 validations_to_be_deleted.append(validation)
18 
19     for delete_validation in validations_to_be_deleted:
20         delete_validation.delete()
21 
22 
23 def add_country_to_rsr_validation_set(apps, schema_editor):
24     """
25     Adds the rsr_projectlocation.country field to the RSR validation set, as mandatory field.
26     """
27     ProjectEditorValidationSet = apps.get_model("rsr", "ProjectEditorValidationSet")
28     ProjectEditorValidation = apps.get_model("rsr", "ProjectEditorValidation")
29     try:
30         rsr_validation_set = ProjectEditorValidationSet.objects.get(pk=1)
31         ProjectEditorValidation.objects.get_or_create(
32             validation_set=rsr_validation_set,
33             validation='rsr_projectlocation.country',
34             action=1
35         )
36     except ProjectEditorValidationSet.DoesNotExist:
37         # The RSR validation set (id = 1) can not be deleted and should always exist
38         pass
39 
40 
41 def do_not_hide_recipient_countries_and_regions(apps, schema_editor):
42     """
43     In the RSR validations set (id is always 1), remove the settings to hide the
44     rsr_recipientcountry and rsr_recipientregion models. Do hide some of the specific fields in
45     these models, such as the free text, percentage, vocabulary and vocabulary_uri.
46     """
47     ProjectEditorValidationSet = apps.get_model("rsr", "ProjectEditorValidationSet")
48     ProjectEditorValidation = apps.get_model("rsr", "ProjectEditorValidation")
49     validations_to_be_deleted = []
50     validations_to_be_added = [
51         ['rsr_recipientcountry', ['percentage', 'text']],
52         ['rsr_recipientregion', ['percentage', 'text', 'region_vocabulary',
53                                  'region_vocabulary_uri']],
54     ]
55 
56     try:
57         rsr_validation_set = ProjectEditorValidationSet.objects.get(id=1)
58         for validation in rsr_validation_set.validations.all():
59             if validation.validation in ["rsr_recipientcountry", "rsr_recipientregion"] and \
60                     validation.action == 2:
61                 validations_to_be_deleted.append(validation)
62 
63         for delete_validation in validations_to_be_deleted:
64             delete_validation.delete()
65 
66         for add_validation in validations_to_be_added:
67             model_name = add_validation[0]
68             field_names = add_validation[1]
69             for field_name in field_names:
70                 ProjectEditorValidation.objects.get_or_create(
71                     validation_set=rsr_validation_set,
72                     validation="{0}.{1}".format(model_name, field_name),
73                     action=2
74                 )
75     except ProjectEditorValidationSet.DoesNotExist:
76         # The RSR validation set (id = 1) can not be deleted and should always exist
77         pass
78 
79 
80 def hide_recipient_countries_and_regions(apps, schema_editor):
81     """
82     In the RSR validations set (id is always 1), re-add the settings to hide the
83     rsr_recipientcountry and rsr_recipientregion models.
84     """
85     ProjectEditorValidationSet = apps.get_model("rsr", "ProjectEditorValidationSet")
86     ProjectEditorValidation = apps.get_model("rsr", "ProjectEditorValidation")
87 
88     try:
89         rsr_validation_set = ProjectEditorValidationSet.objects.get(pk=1)
90         ProjectEditorValidation.objects.get_or_create(
91             validation_set=rsr_validation_set,
92             validation='rsr_recipientcountry',
93             action=2
94         )
95         ProjectEditorValidation.objects.get_or_create(
96             validation_set=rsr_validation_set,
97             validation='rsr_recipientregion',
98             action=2
99         )
100     except ProjectEditorValidationSet.DoesNotExist:
101         # The RSR validation set (id = 1) can not be deleted and should always exist
102         pass
103 
104 
105 def convert_locations_to_recipient_countries(apps, schema_editor):
106     """
107     For each project retrieve the country code from the location and add a new recipient country
108     object if it does not exist yet.
109     """
110     Project = apps.get_model("rsr", "Project")
111     RecipientCountry = apps.get_model("rsr", "RecipientCountry")
112 
113     for project in Project.objects.all():
114         country_codes = []
115         for location in project.locations.all():
116             country = location.country
117             if country:
118                 country_codes.append(country.iso_code.upper())
119 
120         for country_code in list(set(country_codes)):
121             RecipientCountry.objects.get_or_create(project=project, country=country_code)
122 
123 
124 def revert_locations_to_recipient_countries(apps, schema_editor):
125     """
126     Unfortunately we can't be sure which recipient country stems from a location, or whether it
127     already existed originally. Therefore we can only ignore it.
128     """
129     pass
130 
131 
132 class Migration(migrations.Migration):
133 
134     dependencies = [
135         ('rsr', '0070_auto_20160519_1205'),
136     ]
137 
138     operations = [
139         migrations.RunPython(remove_country_from_validation_sets,
140                              add_country_to_rsr_validation_set),
141         migrations.RunPython(do_not_hide_recipient_countries_and_regions,
142                              hide_recipient_countries_and_regions),
143         migrations.RunPython(convert_locations_to_recipient_countries,
144                              revert_locations_to_recipient_countries),
145     ]
146
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/akvo/rsr/migrations/0071_auto_20160525_1036.py b/akvo/rsr/migrations/0071_auto_20160525_1036.py
--- a/akvo/rsr/migrations/0071_auto_20160525_1036.py
+++ b/akvo/rsr/migrations/0071_auto_20160525_1036.py
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 
+from django.core.exceptions import MultipleObjectsReturned
 from django.db import models, migrations
 
 
@@ -33,7 +34,7 @@
             validation='rsr_projectlocation.country',
             action=1
         )
-    except ProjectEditorValidationSet.DoesNotExist:
+    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):
         # The RSR validation set (id = 1) can not be deleted and should always exist
         pass
 
@@ -72,7 +73,7 @@
                     validation="{0}.{1}".format(model_name, field_name),
                     action=2
                 )
-    except ProjectEditorValidationSet.DoesNotExist:
+    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):
         # The RSR validation set (id = 1) can not be deleted and should always exist
         pass
 
@@ -97,7 +98,7 @@
             validation='rsr_recipientregion',
             action=2
         )
-    except ProjectEditorValidationSet.DoesNotExist:
+    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):
         # The RSR validation set (id = 1) can not be deleted and should always exist
         pass
 
@@ -118,7 +119,12 @@
                 country_codes.append(country.iso_code.upper())
 
         for country_code in list(set(country_codes)):
-            RecipientCountry.objects.get_or_create(project=project, country=country_code)
+            try:
+                RecipientCountry.objects.get_or_create(project=project, country=country_code)
+            except MultipleObjectsReturned:
+                # This could happen when a project has the same country specified as a recipient
+                # country multiple times
+                pass
 
 
 def revert_locations_to_recipient_countries(apps, schema_editor):
diff --git a/akvo/rsr/migrations/0073_auto_20160525_1541.py b/akvo/rsr/migrations/0073_auto_20160525_1541.py
--- a/akvo/rsr/migrations/0073_auto_20160525_1541.py
+++ b/akvo/rsr/migrations/0073_auto_20160525_1541.py
@@ -58,8 +58,11 @@
     for employment in Employment.objects.all():
         new_country_field = employment.new_country_field
         if new_country_field:
-            employment.country = Country.objects.get(iso_code=new_country_field.lower())
-            employment.save(update_fields=['country', ])
+            try:
+                employment.country = Country.objects.get(iso_code=new_country_field.lower())
+                employment.save(update_fields=['country', ])
+            except Country.DoesNotExist:
+                pass
 
 
 class Migration(migrations.Migration):

{"golden_diff": "diff --git a/akvo/rsr/migrations/0071_auto_20160525_1036.py b/akvo/rsr/migrations/0071_auto_20160525_1036.py\n--- a/akvo/rsr/migrations/0071_auto_20160525_1036.py\n+++ b/akvo/rsr/migrations/0071_auto_20160525_1036.py\n@@ -1,6 +1,7 @@\n # -*- coding: utf-8 -*-\n from __future__ import unicode_literals\n \n+from django.core.exceptions import MultipleObjectsReturned\n from django.db import models, migrations\n \n \n@@ -33,7 +34,7 @@\n             validation='rsr_projectlocation.country',\n             action=1\n         )\n-    except ProjectEditorValidationSet.DoesNotExist:\n+    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):\n         # The RSR validation set (id = 1) can not be deleted and should always exist\n         pass\n \n@@ -72,7 +73,7 @@\n                     validation=\"{0}.{1}\".format(model_name, field_name),\n                     action=2\n                 )\n-    except ProjectEditorValidationSet.DoesNotExist:\n+    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):\n         # The RSR validation set (id = 1) can not be deleted and should always exist\n         pass\n \n@@ -97,7 +98,7 @@\n             validation='rsr_recipientregion',\n             action=2\n         )\n-    except ProjectEditorValidationSet.DoesNotExist:\n+    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):\n         # The RSR validation set (id = 1) can not be deleted and should always exist\n         pass\n \n@@ -118,7 +119,12 @@\n                 country_codes.append(country.iso_code.upper())\n \n         for country_code in list(set(country_codes)):\n-            RecipientCountry.objects.get_or_create(project=project, country=country_code)\n+            try:\n+                RecipientCountry.objects.get_or_create(project=project, country=country_code)\n+            except MultipleObjectsReturned:\n+                # This could happen when a project has the same country specified as a recipient\n+                # country multiple times\n+                pass\n \n \n def revert_locations_to_recipient_countries(apps, schema_editor):\ndiff --git a/akvo/rsr/migrations/0073_auto_20160525_1541.py b/akvo/rsr/migrations/0073_auto_20160525_1541.py\n--- a/akvo/rsr/migrations/0073_auto_20160525_1541.py\n+++ b/akvo/rsr/migrations/0073_auto_20160525_1541.py\n@@ -58,8 +58,11 @@\n     for employment in Employment.objects.all():\n         new_country_field = employment.new_country_field\n         if new_country_field:\n-            employment.country = Country.objects.get(iso_code=new_country_field.lower())\n-            employment.save(update_fields=['country', ])\n+            try:\n+                employment.country = Country.objects.get(iso_code=new_country_field.lower())\n+                employment.save(update_fields=['country', ])\n+            except Country.DoesNotExist:\n+                pass\n \n \n class Migration(migrations.Migration):\n", "issue": "Move to IATI country list by default\nWe now have our own country list in RSR, and IATI also uses a country list (which we also use for some fields, such as recipient country). I think we should completely switch to the IATI country list, but that also involves some other changes (filters on RSR pages, etc).\n- [x] Projects\n  - [x] Remove the rsr_projectlocation.country field from the project editor\n  - [x] Remove all mentions of rsr_projectlocation.country in the validation sets\n  - [x] Do not hide recipient countries and regions in the RSR validation set\n  - [x] Convert any country of a project location to a recipient country\n  - [x] Remove the country field as a mandatory field for publishing in RSR\n  - [x] Update project page\n    - [x] Project header\n    - [x] Full report tab (remove country field from location)\n    - [x] Donation thanks page\n  - [x] Update projects list\n    - [x] Update mention of country per project\n    - [x] Update location filter\n  - [x] Update IATI import\n  - [x] Add comment to state that location.country is legacy\n- [x] Project updates\n  - [x] Remove project update location countries (not used)\n  - [x] Remove location filter on updates list\n- [x] Organisations\n  - [x] Add new iati_country field\n  - [x] Convert existing countries to IATI countries\n  - [x] Replace country field in 'old' organisation admin\n  - [x] Add comment to state that location.country is legacy\n  - [x] Update location filter on organisations list\n  - [x] Update organisations list\n- [x] Employments\n  - [x] Convert the employment countries to IATI countries\n  - [x] Update 'Request to join an organisation' module\n- [x] Make all migrations reversable\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.db import models, migrations\n\n\ndef convert_org_locations_to_iati_countries(apps, schema_editor):\n    \"\"\"\n    Convert the organisation location countries to the new 'iati_country' field.\n    \"\"\"\n    OrganisationLocation = apps.get_model(\"rsr\", \"OrganisationLocation\")\n\n    for location in OrganisationLocation.objects.all():\n        country = location.country\n        if country:\n            location.iati_country = country.iso_code.upper()\n            location.save(update_fields=['iati_country', ])\n\n\ndef convert_org_locations_to_iati_countries_reverse(apps, schema_editor):\n    \"\"\"\n    Revert the organisation location countries to the old 'country' field.\n    \"\"\"\n    OrganisationLocation = apps.get_model(\"rsr\", \"OrganisationLocation\")\n    Country = apps.get_model(\"rsr\", \"Country\")\n\n    for location in OrganisationLocation.objects.all():\n        country = location.iati_country\n        if country:\n            try:\n                location.country = Country.objects.get(iso_code=country.lower())\n                location.save(update_fields=['country', ])\n            except Country.DoesNotExist:\n                pass\n\n\ndef convert_employments_to_iati_countries(apps, schema_editor):\n    \"\"\"\n    Convert the employment countries to the 'new_country_field' field. This field is renamed in\n    the following migration.\n    \"\"\"\n    Employment = apps.get_model(\"rsr\", \"Employment\")\n\n    for employment in Employment.objects.all():\n        country = employment.country\n        if country:\n            employment.new_country_field = country.iso_code.upper()\n            employment.save(update_fields=['new_country_field', ])\n\n\ndef convert_employments_to_iati_countries_reverse(apps, schema_editor):\n    \"\"\"\n    Revert the employment countries to the old 'country' field.\n    \"\"\"\n    Employment = apps.get_model(\"rsr\", \"Employment\")\n    Country = apps.get_model(\"rsr\", \"Country\")\n\n    for employment in Employment.objects.all():\n        new_country_field = employment.new_country_field\n        if new_country_field:\n            employment.country = Country.objects.get(iso_code=new_country_field.lower())\n            employment.save(update_fields=['country', ])\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('rsr', '0072_auto_20160525_1528'),\n    ]\n\n    operations = [\n        migrations.RunPython(convert_org_locations_to_iati_countries,\n                             convert_org_locations_to_iati_countries_reverse),\n        migrations.RunPython(convert_employments_to_iati_countries,\n                             convert_employments_to_iati_countries_reverse),\n    ]\n", "path": "akvo/rsr/migrations/0073_auto_20160525_1541.py"}, {"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.db import models, migrations\n\n\ndef remove_country_from_validation_sets(apps, schema_editor):\n    \"\"\"\n    Removes the rsr_projectlocation.country field from all validation sets.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    validations_to_be_deleted = []\n\n    for validation_set in ProjectEditorValidationSet.objects.all():\n        for validation in validation_set.validations.all():\n            if 'rsr_projectlocation.country' in validation.validation:\n                validations_to_be_deleted.append(validation)\n\n    for delete_validation in validations_to_be_deleted:\n        delete_validation.delete()\n\n\ndef add_country_to_rsr_validation_set(apps, schema_editor):\n    \"\"\"\n    Adds the rsr_projectlocation.country field to the RSR validation set, as mandatory field.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    ProjectEditorValidation = apps.get_model(\"rsr\", \"ProjectEditorValidation\")\n    try:\n        rsr_validation_set = ProjectEditorValidationSet.objects.get(pk=1)\n        ProjectEditorValidation.objects.get_or_create(\n            validation_set=rsr_validation_set,\n            validation='rsr_projectlocation.country',\n            action=1\n        )\n    except ProjectEditorValidationSet.DoesNotExist:\n        # The RSR validation set (id = 1) can not be deleted and should always exist\n        pass\n\n\ndef do_not_hide_recipient_countries_and_regions(apps, schema_editor):\n    \"\"\"\n    In the RSR validations set (id is always 1), remove the settings to hide the\n    rsr_recipientcountry and rsr_recipientregion models. Do hide some of the specific fields in\n    these models, such as the free text, percentage, vocabulary and vocabulary_uri.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    ProjectEditorValidation = apps.get_model(\"rsr\", \"ProjectEditorValidation\")\n    validations_to_be_deleted = []\n    validations_to_be_added = [\n        ['rsr_recipientcountry', ['percentage', 'text']],\n        ['rsr_recipientregion', ['percentage', 'text', 'region_vocabulary',\n                                 'region_vocabulary_uri']],\n    ]\n\n    try:\n        rsr_validation_set = ProjectEditorValidationSet.objects.get(id=1)\n        for validation in rsr_validation_set.validations.all():\n            if validation.validation in [\"rsr_recipientcountry\", \"rsr_recipientregion\"] and \\\n                    validation.action == 2:\n                validations_to_be_deleted.append(validation)\n\n        for delete_validation in validations_to_be_deleted:\n            delete_validation.delete()\n\n        for add_validation in validations_to_be_added:\n            model_name = add_validation[0]\n            field_names = add_validation[1]\n            for field_name in field_names:\n                ProjectEditorValidation.objects.get_or_create(\n                    validation_set=rsr_validation_set,\n                    validation=\"{0}.{1}\".format(model_name, field_name),\n                    action=2\n                )\n    except ProjectEditorValidationSet.DoesNotExist:\n        # The RSR validation set (id = 1) can not be deleted and should always exist\n        pass\n\n\ndef hide_recipient_countries_and_regions(apps, schema_editor):\n    \"\"\"\n    In the RSR validations set (id is always 1), re-add the settings to hide the\n    rsr_recipientcountry and rsr_recipientregion models.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    ProjectEditorValidation = apps.get_model(\"rsr\", \"ProjectEditorValidation\")\n\n    try:\n        rsr_validation_set = ProjectEditorValidationSet.objects.get(pk=1)\n        ProjectEditorValidation.objects.get_or_create(\n            validation_set=rsr_validation_set,\n            validation='rsr_recipientcountry',\n            action=2\n        )\n        ProjectEditorValidation.objects.get_or_create(\n            validation_set=rsr_validation_set,\n            validation='rsr_recipientregion',\n            action=2\n        )\n    except ProjectEditorValidationSet.DoesNotExist:\n        # The RSR validation set (id = 1) can not be deleted and should always exist\n        pass\n\n\ndef convert_locations_to_recipient_countries(apps, schema_editor):\n    \"\"\"\n    For each project retrieve the country code from the location and add a new recipient country\n    object if it does not exist yet.\n    \"\"\"\n    Project = apps.get_model(\"rsr\", \"Project\")\n    RecipientCountry = apps.get_model(\"rsr\", \"RecipientCountry\")\n\n    for project in Project.objects.all():\n        country_codes = []\n        for location in project.locations.all():\n            country = location.country\n            if country:\n                country_codes.append(country.iso_code.upper())\n\n        for country_code in list(set(country_codes)):\n            RecipientCountry.objects.get_or_create(project=project, country=country_code)\n\n\ndef revert_locations_to_recipient_countries(apps, schema_editor):\n    \"\"\"\n    Unfortunately we can't be sure which recipient country stems from a location, or whether it\n    already existed originally. Therefore we can only ignore it.\n    \"\"\"\n    pass\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('rsr', '0070_auto_20160519_1205'),\n    ]\n\n    operations = [\n        migrations.RunPython(remove_country_from_validation_sets,\n                             add_country_to_rsr_validation_set),\n        migrations.RunPython(do_not_hide_recipient_countries_and_regions,\n                             hide_recipient_countries_and_regions),\n        migrations.RunPython(convert_locations_to_recipient_countries,\n                             revert_locations_to_recipient_countries),\n    ]\n", "path": "akvo/rsr/migrations/0071_auto_20160525_1036.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.db import models, migrations\n\n\ndef convert_org_locations_to_iati_countries(apps, schema_editor):\n    \"\"\"\n    Convert the organisation location countries to the new 'iati_country' field.\n    \"\"\"\n    OrganisationLocation = apps.get_model(\"rsr\", \"OrganisationLocation\")\n\n    for location in OrganisationLocation.objects.all():\n        country = location.country\n        if country:\n            location.iati_country = country.iso_code.upper()\n            location.save(update_fields=['iati_country', ])\n\n\ndef convert_org_locations_to_iati_countries_reverse(apps, schema_editor):\n    \"\"\"\n    Revert the organisation location countries to the old 'country' field.\n    \"\"\"\n    OrganisationLocation = apps.get_model(\"rsr\", \"OrganisationLocation\")\n    Country = apps.get_model(\"rsr\", \"Country\")\n\n    for location in OrganisationLocation.objects.all():\n        country = location.iati_country\n        if country:\n            try:\n                location.country = Country.objects.get(iso_code=country.lower())\n                location.save(update_fields=['country', ])\n            except Country.DoesNotExist:\n                pass\n\n\ndef convert_employments_to_iati_countries(apps, schema_editor):\n    \"\"\"\n    Convert the employment countries to the 'new_country_field' field. This field is renamed in\n    the following migration.\n    \"\"\"\n    Employment = apps.get_model(\"rsr\", \"Employment\")\n\n    for employment in Employment.objects.all():\n        country = employment.country\n        if country:\n            employment.new_country_field = country.iso_code.upper()\n            employment.save(update_fields=['new_country_field', ])\n\n\ndef convert_employments_to_iati_countries_reverse(apps, schema_editor):\n    \"\"\"\n    Revert the employment countries to the old 'country' field.\n    \"\"\"\n    Employment = apps.get_model(\"rsr\", \"Employment\")\n    Country = apps.get_model(\"rsr\", \"Country\")\n\n    for employment in Employment.objects.all():\n        new_country_field = employment.new_country_field\n        if new_country_field:\n            try:\n                employment.country = Country.objects.get(iso_code=new_country_field.lower())\n                employment.save(update_fields=['country', ])\n            except Country.DoesNotExist:\n                pass\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('rsr', '0072_auto_20160525_1528'),\n    ]\n\n    operations = [\n        migrations.RunPython(convert_org_locations_to_iati_countries,\n                             convert_org_locations_to_iati_countries_reverse),\n        migrations.RunPython(convert_employments_to_iati_countries,\n                             convert_employments_to_iati_countries_reverse),\n    ]\n", "path": "akvo/rsr/migrations/0073_auto_20160525_1541.py"}, {"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.core.exceptions import MultipleObjectsReturned\nfrom django.db import models, migrations\n\n\ndef remove_country_from_validation_sets(apps, schema_editor):\n    \"\"\"\n    Removes the rsr_projectlocation.country field from all validation sets.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    validations_to_be_deleted = []\n\n    for validation_set in ProjectEditorValidationSet.objects.all():\n        for validation in validation_set.validations.all():\n            if 'rsr_projectlocation.country' in validation.validation:\n                validations_to_be_deleted.append(validation)\n\n    for delete_validation in validations_to_be_deleted:\n        delete_validation.delete()\n\n\ndef add_country_to_rsr_validation_set(apps, schema_editor):\n    \"\"\"\n    Adds the rsr_projectlocation.country field to the RSR validation set, as mandatory field.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    ProjectEditorValidation = apps.get_model(\"rsr\", \"ProjectEditorValidation\")\n    try:\n        rsr_validation_set = ProjectEditorValidationSet.objects.get(pk=1)\n        ProjectEditorValidation.objects.get_or_create(\n            validation_set=rsr_validation_set,\n            validation='rsr_projectlocation.country',\n            action=1\n        )\n    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):\n        # The RSR validation set (id = 1) can not be deleted and should always exist\n        pass\n\n\ndef do_not_hide_recipient_countries_and_regions(apps, schema_editor):\n    \"\"\"\n    In the RSR validations set (id is always 1), remove the settings to hide the\n    rsr_recipientcountry and rsr_recipientregion models. Do hide some of the specific fields in\n    these models, such as the free text, percentage, vocabulary and vocabulary_uri.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    ProjectEditorValidation = apps.get_model(\"rsr\", \"ProjectEditorValidation\")\n    validations_to_be_deleted = []\n    validations_to_be_added = [\n        ['rsr_recipientcountry', ['percentage', 'text']],\n        ['rsr_recipientregion', ['percentage', 'text', 'region_vocabulary',\n                                 'region_vocabulary_uri']],\n    ]\n\n    try:\n        rsr_validation_set = ProjectEditorValidationSet.objects.get(id=1)\n        for validation in rsr_validation_set.validations.all():\n            if validation.validation in [\"rsr_recipientcountry\", \"rsr_recipientregion\"] and \\\n                    validation.action == 2:\n                validations_to_be_deleted.append(validation)\n\n        for delete_validation in validations_to_be_deleted:\n            delete_validation.delete()\n\n        for add_validation in validations_to_be_added:\n            model_name = add_validation[0]\n            field_names = add_validation[1]\n            for field_name in field_names:\n                ProjectEditorValidation.objects.get_or_create(\n                    validation_set=rsr_validation_set,\n                    validation=\"{0}.{1}\".format(model_name, field_name),\n                    action=2\n                )\n    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):\n        # The RSR validation set (id = 1) can not be deleted and should always exist\n        pass\n\n\ndef hide_recipient_countries_and_regions(apps, schema_editor):\n    \"\"\"\n    In the RSR validations set (id is always 1), re-add the settings to hide the\n    rsr_recipientcountry and rsr_recipientregion models.\n    \"\"\"\n    ProjectEditorValidationSet = apps.get_model(\"rsr\", \"ProjectEditorValidationSet\")\n    ProjectEditorValidation = apps.get_model(\"rsr\", \"ProjectEditorValidation\")\n\n    try:\n        rsr_validation_set = ProjectEditorValidationSet.objects.get(pk=1)\n        ProjectEditorValidation.objects.get_or_create(\n            validation_set=rsr_validation_set,\n            validation='rsr_recipientcountry',\n            action=2\n        )\n        ProjectEditorValidation.objects.get_or_create(\n            validation_set=rsr_validation_set,\n            validation='rsr_recipientregion',\n            action=2\n        )\n    except (ProjectEditorValidationSet.DoesNotExist, MultipleObjectsReturned):\n        # The RSR validation set (id = 1) can not be deleted and should always exist\n        pass\n\n\ndef convert_locations_to_recipient_countries(apps, schema_editor):\n    \"\"\"\n    For each project retrieve the country code from the location and add a new recipient country\n    object if it does not exist yet.\n    \"\"\"\n    Project = apps.get_model(\"rsr\", \"Project\")\n    RecipientCountry = apps.get_model(\"rsr\", \"RecipientCountry\")\n\n    for project in Project.objects.all():\n        country_codes = []\n        for location in project.locations.all():\n            country = location.country\n            if country:\n                country_codes.append(country.iso_code.upper())\n\n        for country_code in list(set(country_codes)):\n            try:\n                RecipientCountry.objects.get_or_create(project=project, country=country_code)\n            except MultipleObjectsReturned:\n                # This could happen when a project has the same country specified as a recipient\n                # country multiple times\n                pass\n\n\ndef revert_locations_to_recipient_countries(apps, schema_editor):\n    \"\"\"\n    Unfortunately we can't be sure which recipient country stems from a location, or whether it\n    already existed originally. Therefore we can only ignore it.\n    \"\"\"\n    pass\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('rsr', '0070_auto_20160519_1205'),\n    ]\n\n    operations = [\n        migrations.RunPython(remove_country_from_validation_sets,\n                             add_country_to_rsr_validation_set),\n        migrations.RunPython(do_not_hide_recipient_countries_and_regions,\n                             hide_recipient_countries_and_regions),\n        migrations.RunPython(convert_locations_to_recipient_countries,\n                             revert_locations_to_recipient_countries),\n    ]\n", "path": "akvo/rsr/migrations/0071_auto_20160525_1036.py"}]}