Skip to content

Commit

Permalink
[MIG] account_statement_import_online_ponto: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed Mar 16, 2023
1 parent 0ada459 commit 68849ed
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class OnlineBankStatementProvider(models.Model):
journal_id = fields.Many2one(
comodel_name="account.journal",
required=True,
readonly=True,
ondelete="cascade",
domain=[("type", "=", "bank")],
)
Expand All @@ -49,7 +48,6 @@ class OnlineBankStatementProvider(models.Model):
service = fields.Selection(
selection=lambda self: self._selection_service(),
required=True,
readonly=True,
)
interval_type = fields.Selection(
selection=[
Expand Down
2 changes: 1 addition & 1 deletion account_statement_import_online_ponto/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Online Bank Statements: MyPonto.com",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Account",
"website": "https://github.com/OCA/bank-statement-import",
"author": "Florent de Labarre, Therp BV, Odoo Community Association (OCA)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class OnlineBankStatementProvider(models.Model):
("execution_date", "Execution Date"),
("value_date", "Value Date"),
],
string="Ponto Date Field",
default="execution_date",
help="Select the Ponto date field that will be used for "
"the Odoo bank statement line date. If you change this parameter "
Expand All @@ -50,6 +49,7 @@ def _pull(self, date_since, date_until):
stop retrieving when either we get before date_since or there is
no more data available.
"""
# pylint: disable=missing-return
ponto_providers = self.filtered(lambda provider: provider.service == "ponto")
super(OnlineBankStatementProvider, self - ponto_providers)._pull(
date_since, date_until
Expand All @@ -63,21 +63,27 @@ def _ponto_pull(self, date_since, date_until):
is_scheduled = self.env.context.get("scheduled")
if is_scheduled:
_logger.debug(
_("Ponto obtain statement data for journal %s from %s to %s"),
self.journal_id.name,
date_since,
date_until,
_(
"Ponto obtain statement data for journal {journal}"
" from {date_since} to {date_until}"
),
journal=self.journal_id.name,
date_since=date_since,
date_until=date_until,
)
else:
_logger.debug(
_("Ponto obtain all new statement data for journal %s"),
self.journal_id.name,
)
lines = self._ponto_retrieve_data(date_since, date_until)
# For scheduled runs, store latest identifier.
if is_scheduled and lines:
self.ponto_last_identifier = lines[0].get("id")
self._ponto_store_lines(lines)
if not lines:
_logger.info(_("No lines were retrieved from Ponto"))
else:
# For scheduled runs, store latest identifier.
if is_scheduled:
self.ponto_last_identifier = lines[0].get("id")
self._ponto_store_lines(lines)

def _ponto_retrieve_data(self, date_since, date_until):
"""Fill buffer with data from Ponto.
Expand Down
37 changes: 25 additions & 12 deletions account_statement_import_online_ponto/models/ponto_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def _login(self, username, password):
url = PONTO_ENDPOINT + "/oauth2/token"
if not (username and password):
raise UserError(_("Please fill login and key."))
login = "%s:%s" % (username, password)
login = ":".join([username, password])
login = base64.b64encode(login.encode("UTF-8")).decode("UTF-8")
login_headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"Authorization": "Basic %s" % login,
"Authorization": "Basic {login}".format(login=login),
}
_logger.debug(_("POST request on %s"), url)
_logger.debug(_("POST request on {url}"), url=url)
response = requests.post(
url,
params={"grant_type": "client_credentials"},
Expand Down Expand Up @@ -63,13 +63,15 @@ def _get_request_headers(self, access_data):
access_data.update(updated_data)
return {
"Accept": "application/json",
"Authorization": "Bearer %s" % access_data["access_token"],
"Authorization": "Bearer {access_token}".format(
access_token=access_data["access_token"]
),
}

def _set_access_account(self, access_data, account_number):
"""Set ponto account for bank account in access_data."""
url = PONTO_ENDPOINT + "/accounts"
_logger.debug(_("GET request on %s"), url)
_logger.debug(_("GET request on {}"), url)
response = requests.get(
url,
params={"limit": 100},
Expand All @@ -86,8 +88,9 @@ def _set_access_account(self, access_data, account_number):
return
# If we get here, we did not find Ponto account for bank account.
raise UserError(
_("Ponto : wrong configuration, account %s not found in %s")
% (account_number, data)
_(
"Ponto : wrong configuration, account {account} not found in {data}"
).format(account=account_number, data=data)
)

def _get_transactions(self, access_data, last_identifier):
Expand Down Expand Up @@ -116,7 +119,7 @@ def _get_transactions_from_data(self, data):
transactions = data.get("data", [])
if not transactions:
_logger.debug(
_("No transactions where found in data %s"),
_("No transactions where found in data {}"),
data,
)
else:
Expand All @@ -130,7 +133,10 @@ def _get_request(self, access_data, url, params):
"""Interact with Ponto to get next page of data."""
headers = self._get_request_headers(access_data)
_logger.debug(
_("GET request to %s with headers %s and params %s"), url, params, headers
_("GET request to {url} with headers {headers} and params {params}"),
url=url,
headers=headers,
params=params,
)
response = requests.get(
url,
Expand All @@ -142,10 +148,17 @@ def _get_request(self, access_data, url, params):

def _get_response_data(self, response):
"""Get response data for GET or POST request."""
_logger.debug(_("HTTP answer code %s from Ponto"), response.status_code)
_logger.debug(
_("HTTP answer code {response_code} from Ponto"),
response_code=response.status_code,
)
if response.status_code not in (200, 201):
raise UserError(
_("Server returned status code %s: %s")
% (response.status_code, response.text)
_(
"Server returned status code {response_code}: {response_text}"
).format(
response_code=response.status_code,
response_text=response.text,
)
)
return json.loads(response.text)
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,18 @@ def setUp(self):
"code": "BANK",
"currency_id": self.currency_eur.id,
"bank_statements_source": "online",
"online_bank_statement_provider": "ponto",
"bank_account_id": self.bank_account.id,
}
)
self.provider = self.journal.online_bank_statement_provider_id
# To get all the moves in a month at once
self.provider.statement_creation_mode = "monthly"
self.provider = self.OnlineBankStatementProvider.create(
{
"name": "Ponto Provider",
"service": "ponto",
"journal_id": self.journal.id,
# To get all the moves in a month at once
"statement_creation_mode": "monthly",
}
)

self.mock_login = lambda: mock.patch(
_interface_class + "._login",
Expand Down
6 changes: 6 additions & 0 deletions setup/account_statement_import_online_ponto/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 68849ed

Please sign in to comment.