From eabe83c0fedb2ba765bce3863fe485271c9c3736 Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Thu, 20 Aug 2020 15:20:08 +0200 Subject: [PATCH] [IMP] emca: timestamp exported data --- easy_my_coop_api/models/__init__.py | 1 + easy_my_coop_api/models/external_id_mixin.py | 17 ++++++-------- .../models/subscription_request.py | 18 +++++++++++++++ .../services/subscription_request_service.py | 2 ++ .../tests/test_account_invoice.py | 22 ++++++++++++++----- .../tests/test_subscription_requests.py | 2 ++ .../components/emc_backend.py | 8 +++---- 7 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 easy_my_coop_api/models/subscription_request.py diff --git a/easy_my_coop_api/models/__init__.py b/easy_my_coop_api/models/__init__.py index 6740752..3e72c0b 100644 --- a/easy_my_coop_api/models/__init__.py +++ b/easy_my_coop_api/models/__init__.py @@ -1,2 +1,3 @@ from . import auth_api_key from . import external_id_mixin +from . import subscription_request diff --git a/easy_my_coop_api/models/external_id_mixin.py b/easy_my_coop_api/models/external_id_mixin.py index 1e699fa..bed33e3 100644 --- a/easy_my_coop_api/models/external_id_mixin.py +++ b/easy_my_coop_api/models/external_id_mixin.py @@ -28,6 +28,12 @@ class ExternalIdMixin(models.AbstractModel): string="External ID Sequence", required=False, ) + first_api_export_date = fields.Datetime( + string="First API Export Date", required=False + ) + last_api_export_date = fields.Datetime( + string="Last API Export Date", required=False + ) @api.multi def set_external_sequence(self): @@ -55,11 +61,7 @@ class ExternalIdMixin(models.AbstractModel): while True: try: next_id = self.external_id_sequence_id._next() - self.sudo().write( - { - "_api_external_id": next_id - } - ) + self.sudo().write({"_api_external_id": next_id}) break except IntegrityError as e: if n > 0: @@ -74,11 +76,6 @@ class ResPartner(models.Model): _inherit = ["res.partner", "external.id.mixin"] -class SubscriptionRequest(models.Model): - _name = "subscription.request" - _inherit = ["subscription.request", "external.id.mixin"] - - class AccountAccount(models.Model): _name = "account.account" _inherit = ["account.account", "external.id.mixin"] diff --git a/easy_my_coop_api/models/subscription_request.py b/easy_my_coop_api/models/subscription_request.py new file mode 100644 index 0000000..97b6c76 --- /dev/null +++ b/easy_my_coop_api/models/subscription_request.py @@ -0,0 +1,18 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import api, models +from odoo.fields import Datetime + + +class SubscriptionRequest(models.Model): + _name = "subscription.request" + _inherit = ["subscription.request", "external.id.mixin"] + + @api.multi + def _timestamp_export(self): + self.write({"last_api_export_date": Datetime.now()}) + self.filtered(lambda sr: not sr.first_api_export_date).write( + {"first_api_export_date": Datetime.now()} + ) diff --git a/easy_my_coop_api/services/subscription_request_service.py b/easy_my_coop_api/services/subscription_request_service.py index ed05258..c966043 100644 --- a/easy_my_coop_api/services/subscription_request_service.py +++ b/easy_my_coop_api/services/subscription_request_service.py @@ -31,6 +31,7 @@ class SubscriptionRequestService(Component): [("_api_external_id", "=", _id)] ) if sr: + sr._timestamp_export() return self._to_dict(sr) else: raise wrapJsonException( @@ -49,6 +50,7 @@ class SubscriptionRequestService(Component): domain.append(("date", "<=", date_to)) requests = self.env["subscription.request"].search(domain) + requests._timestamp_export() response = { "count": len(requests), diff --git a/easy_my_coop_api/tests/test_account_invoice.py b/easy_my_coop_api/tests/test_account_invoice.py index 4919ef5..e6117ca 100644 --- a/easy_my_coop_api/tests/test_account_invoice.py +++ b/easy_my_coop_api/tests/test_account_invoice.py @@ -30,11 +30,20 @@ class TestAccountInvoiceController(BaseEMCRestCase): today = Date.to_string(Date.today()) self.demo_invoice_dict = { - "id": 1, + "id": self.capital_release.get_api_external_id(), "number": "xxx", # can't guess it - "partner": {"id": 1, "name": "Catherine des Champs"}, - "account": {"id": 1, "name": "Cooperators"}, - "journal": {"id": 1, "name": "Subscription Journal"}, + "partner": { + "id": self.coop_candidate.get_api_external_id(), + "name": self.coop_candidate.name, + }, + "account": { + "id": self.cooperator_account.get_api_external_id(), + "name": self.cooperator_account.name, + }, + "journal": { + "id": self.subscription_journal.get_api_external_id(), + "name": self.subscription_journal.name, + }, "subscription_request": {}, "state": "open", "date": today, @@ -47,7 +56,10 @@ class TestAccountInvoiceController(BaseEMCRestCase): "product": {"id": 1, "name": "Part A - Founder"}, "price_unit": 100.0, "quantity": 2.0, - "account": {"id": 2, "name": "Equity"}, + "account": { + "id": self.equity_account.get_api_external_id(), + "name": self.equity_account.name, + }, } ], } diff --git a/easy_my_coop_api/tests/test_subscription_requests.py b/easy_my_coop_api/tests/test_subscription_requests.py index 8abb081..824166d 100644 --- a/easy_my_coop_api/tests/test_subscription_requests.py +++ b/easy_my_coop_api/tests/test_subscription_requests.py @@ -76,6 +76,8 @@ class TestSRController(BaseEMCRestCase): date_sr = self.sr_service.search(date_from=date_from, date_to=date_to) self.assertTrue(date_sr) + self.assertTrue(self.demo_request_1.first_api_export_date) + self.assertTrue(self.demo_request_1.last_api_export_date) def test_route_get(self): external_id = self.demo_request_1.get_api_external_id() diff --git a/easy_my_coop_connector/components/emc_backend.py b/easy_my_coop_connector/components/emc_backend.py index 96fb821..6c06005 100644 --- a/easy_my_coop_connector/components/emc_backend.py +++ b/easy_my_coop_connector/components/emc_backend.py @@ -9,7 +9,7 @@ import requests from werkzeug.exceptions import BadRequest, InternalServerError, NotFound from odoo import _, api, fields, models -from odoo.exceptions import AccessDenied +from odoo.exceptions import AccessDenied, Warning as UserError _logger = logging.getLogger(__name__) @@ -113,12 +113,12 @@ class EMCBackend(models.Model): response = requests.get(url) except Exception as e: _logger.error(e) - raise Warning(_("Failed to connect to backend: %s" % str(e))) + raise UserError(_("Failed to connect to backend: %s" % str(e))) if response.status_code == 200: content = json.loads(response.content.decode("utf-8")) - raise Warning(_("Success: %s") % content["message"]) + raise UserError(_("Success: %s") % content["message"]) else: - raise Warning( + raise UserError( _("Failed to connect to backend: %s" % str(response.content)) )