Repository: scieloorg/xylose

Issue:
Asignación incorrecta de country en afiliaciones no normalizadas (v70)
En versiones de PC-Programs anteriores a: https://github.com/scieloorg/PC-Programs/commit/5e494a031cabb9d718970a6201f3ee6c9847b942 se realizaba la asignación del campo ```p```de la siguiente forma:
```
a['p'] = item.country if item.i_country is None else item.i_country
a['q'] = item.country if item.i_country is not None else None
```
Por lo que aunque el campo no este normalizado se asignaba el valor correspondiente al código ISO_3166, lo cual trae problemas en aplicaciones como articles_meta en donde encontramos combinados datos de códigos ISO_3166 con nombres del país:
http://articlemeta.scielo.org/api/v1/article/?code=S1665-70632015000300102&format=xmlwos
![screen shot 2016-02-25 at 12 02 39](https://cloud.githubusercontent.com/assets/197827/13329058/906141b6-dbb7-11e5-8428-225d4bd89945.png)


Discussion:
fabiobatalha: Puesdes escribir un testcase para eso.
swarzesherz: Actualizado con testcase
fabiobatalha: Desculpe não ter comentado antes.

Acho que seria legal também incluir no retorno do Xylose a version ISO do país, quando existir e for válida.


swarzesherz: Actualizado, agregue affdict['country_iso_3166'] = aff['p']

Patch:
diff --git a/xylose/scielodocument.py b/xylose/scielodocument.py
index 0b42e3e..c71e5b2 100644
--- a/xylose/scielodocument.py
+++ b/xylose/scielodocument.py
@@ -1309,6 +1309,7 @@ class Article(object):
                     continue
                 normalized[aff['index']]['normalized'] = True
                 normalized[aff['index']]['country'] = aff.get('country', '')
+                normalized[aff['index']]['country_iso_3166'] = aff.get('country_iso_3166', '')
                 normalized[aff['index']]['institution'] = aff.get('institution', '')
                 normalized[aff['index']]['state'] = aff.get('state', '')
 
@@ -1335,8 +1336,7 @@ class Article(object):
 
                         if 'p' in aff and aff['p'] in choices.ISO_3166:
                             affdict['country'] = choices.ISO_3166[aff['p']]
-                            if aff['p'] in choices.ISO_3166:
-                                affdict['country_iso_3166'] = aff['p']
+                            affdict['country_iso_3166'] = aff['p']
 
                         if 's' in aff:
                             affdict['state'] = aff['s']
@@ -1369,6 +1369,9 @@ class Article(object):
                     affdict['state'] = html_decode(aff['s'])
                 if 'p' in aff:
                     affdict['country'] = html_decode(aff['p'])
+                if 'p' in aff and 'q' in aff and aff['p'] in choices.ISO_3166:
+                    affdict['country'] = choices.ISO_3166[aff['p']]
+                    affdict['country_iso_3166'] = aff['p']
                 if 'e' in aff:
                     affdict['email'] = html_decode(aff['e'])
                 if 'd' in aff:


Test patch:
diff --git a/tests/test_document.py b/tests/test_document.py
index 758d7b8..6e6b1d6 100644
--- a/tests/test_document.py
+++ b/tests/test_document.py
@@ -1751,6 +1751,13 @@ class ArticleTests(unittest.TestCase):
                 u"p": u"US",
                 u"s": u"São Paulo",
                 u"_": u"University of Florida Not Normalized"
+            },
+            {
+                u"i": u"A04",
+                u"q": u"Mexico",
+                u"p": u"MX",
+                u"s": u"Yucatán",
+                u"_": u"Secretaría de Salud de Yucatán"
             }
         ]
 
@@ -1758,13 +1765,15 @@ class ArticleTests(unittest.TestCase):
 
         result_index = u''.join([i['index'] for i in sorted(amc,  key=lambda k: k['index'])])
         result_country = u''.join([i['country'] for i in sorted(amc,  key=lambda k: k['index'])])
+        result_country_iso = u''.join([i['country_iso_3166'] for i in sorted(amc,  key=lambda k: k['index']) if 'country_iso_3166' in i])
         result_status = u''.join([str(i['normalized']) for i in sorted(amc,  key=lambda k: k['index'])])
         result_state = u''.join([i['state'] for i in sorted(amc,  key=lambda k: k['index'])])
 
-        self.assertEqual(result_index, u'A01A02A03')
-        self.assertEqual(result_country, u'BrazilBrazilUS')
-        self.assertEqual(result_status, u'TrueTrueFalse')
-        self.assertEqual(result_state, u'São PauloSão Paulo')
+        self.assertEqual(result_index, u'A01A02A03A04')
+        self.assertEqual(result_country, u'BrazilBrazilUSMexico')
+        self.assertEqual(result_country_iso, u'BRBRMX')
+        self.assertEqual(result_status, u'TrueTrueFalseFalse')
+        self.assertEqual(result_state, u'São PauloSão PauloYucatán')
 
     def test_without_normalized_affiliations(self):
         article = self.article
@@ -1992,6 +2001,41 @@ class ArticleTests(unittest.TestCase):
 
         self.assertEqual(article.affiliations, expected)
 
+    def test_affiliation_with_country_iso_3166(self):
+
+        article = self.article
+
+        del(article.data['article']['v70'])
+
+        article.data['article']['v70'] = [
+            {
+                u"1": u"Escuela Nacional de Enfermería y Obstetricia",
+                u"2": u"División de Estudios de Posgrado e Investigación",
+                u"q": u"Mexico",
+                u"c": u"México",
+                u"i": u"A01",
+                u"l": u"a",
+                u"p": u"MX",
+                u"s": u"D.F.",
+                u"_": u"Universidad Nacional Autónoma de México"
+           }
+        ]
+
+        expected = [
+            {
+                'index': u'A01',
+                'city': u'México',
+                'state': u'D.F.',
+                'country': u'Mexico',
+                'country_iso_3166': u'MX',
+                'orgdiv1': u'Escuela Nacional de Enfermería y Obstetricia',
+                'orgdiv2': u'División de Estudios de Posgrado e Investigación',
+                'institution': u'Universidad Nacional Autónoma de México'
+            }
+        ]
+
+        self.assertEqual(article.affiliations, expected)
+
     def test_without_scielo_domain(self):
         article = self.article