# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


def combine_results(apps, schema_editor):
    """
    If two (or more) similar results are present in a project, then combine the
    indicators of these results.
    """
    Project = apps.get_model("rsr", "Project")
    for project in Project.objects.all():
        if project.results.all() and project.results.all().count() > 1:
            altered_results = []
            for result in project.results.all():
                for other_result in project.results.filter(title=result.title,
                                                           type=result.type).exclude(pk=result.pk):
                    for indicator in other_result.indicators.all():
                        indicator.result = result
                        indicator.save()
                    if other_result not in altered_results:
                        altered_results.append(other_result)

            for result in altered_results:
                if not result.indicators.all():
                    result.delete()



class Migration(migrations.Migration):

    dependencies = [
        ('rsr', '0003_auto_20150402_1040'),
    ]

    operations = [
        migrations.RunPython(combine_results),
    ]

@@ -507,32 +507,80 @@
 .results.all():%0A
+                if result.indicators.all():%0A    
                 
@@ -626,29 +626,9 @@
 ter(
-title=result.title,%0A 
+%0A
     
@@ -655,37 +655,27 @@
     
-                             
+title=result.title,
  typ
@@ -728,24 +728,28 @@
             
+   
+ 
 for indicato
@@ -800,32 +800,36 @@
                 
+    
 indicator.result
@@ -838,16 +838,20 @@
  result%0A
+    
         
@@ -903,16 +903,20 @@
         
+    
 if other
@@ -939,32 +939,36 @@
 ltered_results:%0A
+    
                 

Add additional check to migration