# -*- encoding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: Coninckx David <david@coninckx.com>
#
#    The licence is in the file __openerp__.py
#
##############################################################################


def migrate(cr, version):

    if not version:
        return
    # Add translated value for new property distance_from_closest_city
    # based on old string distance_from_closest_city

    cr.execute(
        """
        CREATE RULE "my_table_on_duplicate_ignore"
        AS ON INSERT TO "compassion_translated_value"
        WHERE EXISTS(SELECT 1 FROM compassion_translated_value
                    WHERE value_en=NEW.value_en)
        DO INSTEAD NOTHING;
        INSERT INTO compassion_translated_value
        (is_tag,value_en, property_name)
        SELECT False, distance_from_closest_city, 'distance_from_closest_city'
        FROM compassion_project
        WHERE distance_from_closest_city IS NOT NULL;
        DROP RULE "my_table_on_duplicate_ignore"
        ON "compassion_translated_value";
        """)

    cr.execute(
        'SELECT id, distance_from_closest_city '
        'FROM compassion_project '
        'WHERE distance_from_closest_city IS NOT NULL'
    )
    projects = cr.fetchall()
    
    cr.execute(
        '''
        CREATE RULE "my_table_on_duplicate_ignore"
        AS ON INSERT TO "project_property_to_value"
        WHERE EXISTS(SELECT 1 FROM project_property_to_value
                    WHERE value_id=NEW.value_id 
                    AND project_id = NEW.project_id)
        DO INSTEAD NOTHING;
        ''')
        
    for project in projects:
        cr.execute(
        "SELECT id FROM compassion_translated_value "
        "WHERE value_en = '{}'".format(project[1]))
        translated_value_id = cr.fetchall()[0][0]
        
        cr.execute(
        "INSERT INTO project_property_to_value (project_id, value_id) "
        "VALUES ({0},{1})".format(
            project[0], translated_value_id))

    cr.execute(
    """
        DROP RULE "my_table_on_duplicate_ignore"
        ON "project_property_to_value";
    """)
@@ -1449,22 +1449,18 @@
 hall()%0D%0A
-    
 %0D%0A
+
     cr.e
@@ -1695,17 +1695,16 @@
 value_id
- 
 %0D%0A      
@@ -1794,26 +1794,18 @@
   ''')%0D%0A
-        
 %0D%0A
+
     for 
@@ -1855,16 +1855,20 @@
         
+    
 %22SELECT 
@@ -1902,24 +1902,28 @@
 ed_value %22%0D%0A
+    
         %22WHE
@@ -2018,18 +2018,10 @@
 0%5D%0D%0A
-        
 %0D%0A
+
     
@@ -2045,16 +2045,20 @@
         
+    
 %22INSERT 
@@ -2122,16 +2122,20 @@
         
+    
 %22VALUES 
@@ -2154,16 +2154,20 @@
 ormat(%0D%0A
+    
         
@@ -2220,24 +2220,28 @@
 r.execute(%0D%0A
+    
     %22%22%22%0D%0A   
@@ -2229,32 +2229,32 @@
 (%0D%0A        %22%22%22%0D%0A
-
         DROP RUL
@@ -2328,16 +2328,20 @@
 alue%22;%0D%0A
+    
     %22%22%22)
@@ -2320,28 +2320,30 @@
 rty_to_value%22;%0D%0A        %22%22%22)
+%0D%0A

Fix PEP8