# -*- coding: utf-8 -*-

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

from lxml import etree

TYPE_TO_CODE = {
    'funding': '1',
    'support': '2',
    'sponsor': '3',
    'field': '4',
}


def participating_org(project):
    """
    Generate the participating-org element.

    :param project: Project object
    :return: A list of Etree elements
    """
    partnership_elements = []

    for partnership in project.partnerships.all():
        org = partnership.organisation
        element = etree.Element("participating-org")

        if org.iati_org_id:
            element.attrib['ref'] = org.iati_org_id

        if org.new_organisation_type:
            element.attrib['type'] = str(org.new_organisation_type)

        if partnership.partner_type in TYPE_TO_CODE.keys():
            element.attrib['role'] = TYPE_TO_CODE[partnership.partner_type]

        narrative_element = etree.SubElement(element, "narrative")

        if org.long_name:
            narrative_element.text = org.long_name
        elif org.name:
            narrative_element.text = org.name

        partnership_elements.append(element)

    return partnership_elements

@@ -685,16 +685,36 @@
 isation%0A
+        if org:%0A    
         
@@ -763,24 +763,28 @@
 %22)%0A%0A        
+    
 if org.iati_
@@ -795,32 +795,36 @@
 id:%0A            
+  
+  
 element.attrib%5B'
@@ -852,24 +852,28 @@
 id%0A%0A        
+    
 if org.new_o
@@ -894,32 +894,36 @@
 pe:%0A            
+  
+  
 element.attrib%5B'
@@ -971,16 +971,20 @@
         
+    
 if partn
@@ -1031,32 +1031,36 @@
 ():%0A            
+   
+ 
 element.attrib%5B'
@@ -1108,32 +1108,36 @@
 _type%5D%0A%0A        
+    
 narrative_elemen
@@ -1184,24 +1184,28 @@
 %22)%0A%0A        
+   
+ 
 if org.long_
@@ -1206,24 +1206,28 @@
 .long_name:%0A
+    
             
@@ -1265,16 +1265,20 @@
 ng_name%0A
+    
         
@@ -1296,32 +1296,36 @@
 me:%0A            
+    
 narrative_elemen
@@ -1343,16 +1343,20 @@
 g.name%0A%0A
+    
         

Check if partnership has organisation in IATI export