diff --git a/jarbas/chamber_of_deputies/models.py b/jarbas/chamber_of_deputies/models.py index 3ea3d4be5..99204d2cc 100644 --- a/jarbas/chamber_of_deputies/models.py +++ b/jarbas/chamber_of_deputies/models.py @@ -20,20 +20,36 @@ def twitter(self): class Receipt: + ELETRONIC_RECEIPT_TYPE = 4 - def __init__(self, year, applicant_id, document_id): + def __init__(self, year, applicant_id, document_id, document_type): self.year = year self.applicant_id = applicant_id self.document_id = document_id + self.document_type = document_type @property def url(self): + if self.document_type == self.ELETRONIC_RECEIPT_TYPE: + return self.eletronic_url() + + return self.pdf_url() + + def eletronic_url(self): + return ( + 'https://www.camara.leg.br/cota-parlamentar/' + 'nota-fiscal-eletronica?ideDocumentoFiscal={}' + ).format(self.document_id) + + def pdf_url(self): args = (self.applicant_id, self.year, self.document_id) + return ( 'http://www.camara.gov.br/' 'cota-parlamentar/documentos/publ/{}/{}/{}.pdf' ).format(*args) + @property def exists(self): status = head(self.url).status_code @@ -107,7 +123,7 @@ def get_receipt_url(self, force=False, bulk=False): if self.receipt_fetched and not force: return None - receipt = Receipt(self.year, self.applicant_id, self.document_id) + receipt = Receipt(self.year, self.applicant_id, self.document_id, self.document_type) if receipt.exists: self.receipt_url = receipt.url self.receipt_fetched = True diff --git a/jarbas/chamber_of_deputies/tests/test_receipt_class.py b/jarbas/chamber_of_deputies/tests/test_receipt_class.py index 3318cf760..5e143e993 100644 --- a/jarbas/chamber_of_deputies/tests/test_receipt_class.py +++ b/jarbas/chamber_of_deputies/tests/test_receipt_class.py @@ -9,12 +9,18 @@ class TestReceipt(TestCase): def setUp(self): - self.receipt = Receipt(1970, 13, 42) + self.receipt = Receipt(1970, 13, 42, 1) + self.eletronic_receipt = Receipt(1970, 13, 42, 4) def test_url(self): expected = 'http://www.camara.gov.br/cota-parlamentar/documentos/publ/13/1970/42.pdf' self.assertEqual(expected, self.receipt.url) + def test_eletronic_url(self): + expected = ('https://www.camara.leg.br/cota-parlamentar/nota-fiscal-eletronica?' + 'ideDocumentoFiscal=42') + self.assertEqual(expected, self.eletronic_receipt.url) + @patch('jarbas.chamber_of_deputies.models.head') def test_existing_url(self, mocked_head): mocked_head.return_value.status_code = 200