lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.9-4-g6e776ae

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.9-4-g6e776ae



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  6e776aef6bea971c674920e1940aac8a560206c8 (commit)
      from  04f78fb6613cccbd78e32a569ccf4ec2e5e1d478 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=6e776aef6bea971c674920e1940aac8a560206c8

commit 6e776aef6bea971c674920e1940aac8a560206c8
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sat May 19 12:39:34 2018 +0200

    Switch the NCF online check to the new form
    
    This tries to screen-scrape the new DGII form that is used to validate
    the new format of NCF numbers.
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/71

diff --git a/stdnum/do/ncf.py b/stdnum/do/ncf.py
index 5258026..335ab4a 100644
--- a/stdnum/do/ncf.py
+++ b/stdnum/do/ncf.py
@@ -42,10 +42,8 @@ Traceback (most recent call last):
 InvalidFormat: ...
 """
 
-import json
-
 from stdnum.exceptions import *
-from stdnum.util import clean, get_soap_client
+from stdnum.util import clean
 
 
 def compact(number):
@@ -99,10 +97,14 @@ def _convert_result(result):  # pragma: no cover
         'MENSAJE_VALIDACION': 'validation_message',
         'RNC': 'rnc',
         'NCF': 'ncf',
+        u'RNC/Cédula': 'rnc',
+        u'Nombre/Razón Social': 'name',
+        'Estado': 'status',
+        'Tipo de comprobante': 'type',
     }
     return dict(
         (translation.get(key, key), value)
-        for key, value in json.loads(result.replace('\t', '\\t')).items())
+        for key, value in result.items())
 
 
 def check_dgii(rnc, ncf, timeout=30):  # pragma: no cover
@@ -117,25 +119,41 @@ def check_dgii(rnc, ncf, timeout=30):  # pragma: no cover
 
         {
             'name': 'The registered name',
-            'proof': 'Source of the information',
-            'is_valid': '1',
-            'validation_message': '',
+            'status': 'VIGENTE',
+            'type': 'FACTURAS DE CREDITO FISCAL',
             'rnc': '123456789',
-            'ncf': 'A020010210100000005'
+            'ncf': 'A020010210100000005',
+            'validation_message': 'El NCF digitado es válido.',
         }
 
     Will return None if the number is invalid or unknown."""
+    import requests
+    try:
+        from bs4 import BeautifulSoup
+    except ImportError:
+        from BeautifulSoup import BeautifulSoup
     from stdnum.do.rnc import compact as rnc_compact
-    from stdnum.do.rnc import dgii_wsdl
     rnc = rnc_compact(rnc)
     ncf = compact(ncf)
-    client = get_soap_client(dgii_wsdl, timeout)
-    result = client.GetNCF(
-        RNC=rnc,
-        NCF=ncf,
-        IMEI='')
-    if result and 'GetNCFResult' in result:
-        result = result['GetNCFResult']  # PySimpleSOAP only
-    if result == '0':
-        return
-    return _convert_result(result)
+    url = 'http://www.dgii.gov.do/app/WebApps/ConsultasWeb/consultas/ncf.aspx'
+    headers = {
+        'User-Agent': 'Mozilla/5.0 (python-stdnum)',
+    }
+    data = {
+        '__EVENTVALIDATION': 
'/wEWBAKh8pDuCgK+9LSUBQLfnOXIDAKErv7SBhjZB34//pbvvJzrbkFCGGPRElcd',
+        '__VIEWSTATE': '/wEPDwUJNTM1NDc0MDQ5ZGRCFUYoDcVRgzEntcKfSuvPnC2VhA==',
+        'ctl00$cphMain$btnConsultar': 'Consultar',
+        'ctl00$cphMain$txtNCF': ncf,
+        'ctl00$cphMain$txtRNC': rnc,
+    }
+    result = BeautifulSoup(
+        requests.post(url, headers=headers, data=data, timeout=timeout).text)
+    results = result.find(id='ctl00_cphMain_pResultado')
+    if results:
+        data = {
+            'validation_message': 
result.find(id='ctl00_cphMain_lblInformacion').get_text().strip(),
+        }
+        data.update(zip(
+            [x.get_text().strip().rstrip(':') for x in 
results.find_all('strong')],
+            [x.get_text().strip() for x in results.find_all('span')]))
+        return _convert_result(data)
diff --git a/tests/test_do_ncf.py b/tests/test_do_ncf.py
index dbd7039..8a94bfa 100644
--- a/tests/test_do_ncf.py
+++ b/tests/test_do_ncf.py
@@ -40,9 +40,11 @@ class TestDGII(unittest.TestCase):
         """Test stdnum.do.ncf.check_dgii()"""
         # Test a normal valid number
         result = ncf.check_dgii('130546312', 'A010010011500000038')
-        self.assertTrue(all(
-            key in result.keys()
-            for key in ['name', 'proof', 'is_valid', 'validation_message', 
'rnc', 'ncf']))
+        self.assertTrue(result)
+        self.assertIn('name', result.keys())
+        self.assertIn('rnc', result.keys())
+        self.assertIn('ncf', result.keys())
+        self.assertIn('validation_message', result.keys())
         self.assertEqual(result['rnc'], '130546312')
         self.assertEqual(result['ncf'], 'A010010011500000038')
         # Test an invalid combination
@@ -53,3 +55,12 @@ class TestDGII(unittest.TestCase):
         # These types have not been requested with the regulator
         self.assertFalse(ncf.check_dgii('1-31-56633-2', 'A030010010100000001'))
         self.assertFalse(ncf.check_dgii('1-31-56633-2', 'A010020010100000001'))
+        # Test the new format
+        result = ncf.check_dgii('130546312', 'B0100000005')
+        self.assertTrue(result)
+        self.assertIn('name', result.keys())
+        self.assertIn('rnc', result.keys())
+        self.assertIn('ncf', result.keys())
+        self.assertIn('validation_message', result.keys())
+        self.assertEqual(result['rnc'], '130546312')
+        self.assertEqual(result['ncf'], 'B0100000005')

-----------------------------------------------------------------------

Summary of changes:
 stdnum/do/ncf.py     | 56 ++++++++++++++++++++++++++++++++++------------------
 tests/test_do_ncf.py | 17 +++++++++++++---
 2 files changed, 51 insertions(+), 22 deletions(-)


hooks/post-receive
-- 
python-stdnum
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
https://lists.arthurdejong.org/python-stdnum-commits/