Skip to content

Commit

Permalink
Fix receipt url for eletronic receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
giovanisleite committed Oct 16, 2019
1 parent a637c76 commit cdceafc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
20 changes: 18 additions & 2 deletions jarbas/chamber_of_deputies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion jarbas/chamber_of_deputies/tests/test_receipt_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cdceafc

Please sign in to comment.