Browse Source

[REF] emcc: abstract adapter

pull/115/head
robin.keunen 4 years ago
parent
commit
9c99e34ed1
  1. 99
      easy_my_coop_connector/models/emc_adapters.py
  2. 24
      easy_my_coop_connector/models/subscription_request.py

99
easy_my_coop_connector/models/subscription_request_adapter.py → easy_my_coop_connector/models/emc_adapters.py

@ -11,26 +11,55 @@ from odoo.exceptions import UserError, ValidationError
from odoo.fields import Date from odoo.fields import Date
class SubscriptionRequestAdapter:
_model = "subscription.request"
class AbstractEMCAdapter:
_model = "set in implementation class"
_root = "api" _root = "api"
_service = "subscription-request"
_service = "set in implementation class"
def __init__(self, backend): def __init__(self, backend):
self.backend = backend self.backend = backend
def get_url(self, args):
def _get_url(self, args):
"""args is a list of path elements """args is a list of path elements
:return the complete route to the service :return the complete route to the service
""" """
return join("/", self._root, self._service, *args) return join("/", self._root, self._service, *args)
def search(self, **params):
raise NotImplementedError
def read(self, id_):
# pylint: disable=method-required-super
url = self._get_url([str(id_)])
api_dict = self.backend.http_get_content(url)
return self.to_write_values(api_dict)
def create(self): def create(self):
# pylint: disable=method-required-super # pylint: disable=method-required-super
raise NotImplementedError raise NotImplementedError
def update(self):
raise NotImplementedError
def delete(self):
raise NotImplementedError
def to_write_values(self, api_dict):
"""
:return a tuple with
- the external id
- a writable dictionary for _model
received from the api
"""
raise NotImplementedError
class SubscriptionRequestAdapter(AbstractEMCAdapter):
_model = "subscription.request"
_service = "subscription-request"
def search(self, date_from=None, date_to=None): def search(self, date_from=None, date_to=None):
url = self.get_url([])
url = self._get_url([])
params = {} params = {}
if date_from: if date_from:
params.update({"date_from": Date.to_string(date_from)}) params.update({"date_from": Date.to_string(date_from)})
@ -38,22 +67,13 @@ class SubscriptionRequestAdapter:
params.update({"date_to": Date.to_string(date_to)}) params.update({"date_to": Date.to_string(date_to)})
sr_list = self.backend.http_get_content(url, params=params) sr_list = self.backend.http_get_content(url, params=params)
return sr_list
def read(self, id_):
# pylint: disable=method-required-super
url = self.get_url([str(id_)])
sr = self.backend.http_get_content(url)
return sr
def update(self):
raise NotImplementedError
def delete(self):
raise NotImplementedError
return {
"count": sr_list["count"],
"rows": [self.to_write_values(row) for row in sr_list["rows"]],
}
def validate(self, id_): def validate(self, id_):
url = self.get_url([str(id_), "validate"])
url = self._get_url([str(id_), "validate"])
data = {} data = {}
try: try:
invoice_dict = self.backend.http_post_content(url, data) invoice_dict = self.backend.http_post_content(url, data)
@ -65,22 +85,19 @@ class SubscriptionRequestAdapter:
"with your system administrator." "with your system administrator."
) )
) )
return invoice_dict
ai_adapter = AccountInvoiceAdapter(backend=self.backend)
return ai_adapter.to_write_values(invoice_dict)
def to_write_values(self, request):
"""
:return a writable dictionary of values from the dictionary
received from the api
"""
def to_write_values(self, api_dict):
Country = self.backend.env["res.country"] Country = self.backend.env["res.country"]
ProductTemplateBinding = self.backend.env[ ProductTemplateBinding = self.backend.env[
"emc.binding.product.template" "emc.binding.product.template"
] ]
address = request["address"]
address = api_dict["address"]
country = Country.search([("code", "=", address["country"])]) country = Country.search([("code", "=", address["country"])])
external_product_id = request["share_product"]["id"]
external_product_id = api_dict["share_product"]["id"]
share_product_binding = ProductTemplateBinding.search_binding( share_product_binding = ProductTemplateBinding.search_binding(
self.backend, external_product_id self.backend, external_product_id
) )
@ -90,17 +107,18 @@ class SubscriptionRequestAdapter:
"No binding exists for share product %s. Please contact " "No binding exists for share product %s. Please contact "
"system administrator " "system administrator "
) )
% request["share_product"]["name"]
% api_dict["share_product"]["name"]
) )
product_product = share_product_binding.internal_id.product_variant_id product_product = share_product_binding.internal_id.product_variant_id
return {
"email": request["email"],
"name": request["name"],
"date": request["date"],
"state": request["state"],
"lang": request["lang"],
"ordered_parts": request["ordered_parts"],
external_id = api_dict["id"]
writable_dict = {
"email": api_dict["email"],
"name": api_dict["name"],
"date": api_dict["date"],
"state": api_dict["state"],
"lang": api_dict["lang"],
"ordered_parts": api_dict["ordered_parts"],
"address": address["street"], "address": address["street"],
"zip_code": address["zip_code"], "zip_code": address["zip_code"],
"city": address["city"], "city": address["city"],
@ -108,3 +126,14 @@ class SubscriptionRequestAdapter:
"share_product_id": product_product.id, "share_product_id": product_product.id,
"source": "emc_api", "source": "emc_api",
} }
return external_id, writable_dict
class AccountInvoiceAdapter(AbstractEMCAdapter):
_model = "account.invoice"
_service = "invoice"
def to_write_values(self, api_dict):
external_id = api_dict.pop("id")
writable_dict = api_dict
return external_id, writable_dict

24
easy_my_coop_connector/models/subscription_request.py

@ -7,7 +7,7 @@ from datetime import date, timedelta
from odoo import api, fields, models from odoo import api, fields, models
from .subscription_request_adapter import SubscriptionRequestAdapter
from .emc_adapters import SubscriptionRequestAdapter
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -43,15 +43,13 @@ class SubscriptionRequest(models.Model):
backend = self._get_backend() backend = self._get_backend()
adapter = SubscriptionRequestAdapter(backend=backend) adapter = SubscriptionRequestAdapter(backend=backend)
requests_dict = adapter.search(date_from=date_from, date_to=date_to) requests_dict = adapter.search(date_from=date_from, date_to=date_to)
for request_dict in requests_dict["rows"]:
external_id = request_dict["id"]
request_values = adapter.to_write_values(request_dict)
for external_id, request_dict in requests_dict["rows"]:
sr_binding = SRBinding.search_binding(backend, external_id) sr_binding = SRBinding.search_binding(backend, external_id)
if sr_binding: # update request if sr_binding: # update request
sr_binding.internal_id.write(request_values)
sr_binding.internal_id.write(request_dict)
else: else:
srequest = self.env["subscription.request"].create( srequest = self.env["subscription.request"].create(
request_values
request_dict
) )
SRBinding.create( SRBinding.create(
{ {
@ -60,7 +58,9 @@ class SubscriptionRequest(models.Model):
"internal_id": srequest.id, "internal_id": srequest.id,
} }
) )
external_ids = [row["id"] for row in requests_dict["rows"]]
external_ids = [
external_id for external_id, _ in requests_dict["rows"]
]
srequests = SRBinding.search( srequests = SRBinding.search(
[ [
("backend_id", "=", backend.id), ("backend_id", "=", backend.id),
@ -76,9 +76,7 @@ class SubscriptionRequest(models.Model):
backend = self._get_backend() backend = self._get_backend()
adapter = SubscriptionRequestAdapter(backend) adapter = SubscriptionRequestAdapter(backend)
sr_data = adapter.read(external_id)
request_values = adapter.to_write_values(sr_data)
_, request_values = adapter.read(external_id)
sr_binding = SRBinding.search_binding(backend, external_id) sr_binding = SRBinding.search_binding(backend, external_id)
if sr_binding: # update request if sr_binding: # update request
@ -120,13 +118,15 @@ class SubscriptionRequest(models.Model):
if self.source == "emc_api": if self.source == "emc_api":
backend = self._get_backend() backend = self._get_backend()
sr_adapter = SubscriptionRequestAdapter(backend=backend) sr_adapter = SubscriptionRequestAdapter(backend=backend)
invoice_dict = sr_adapter.validate(self.binding_id.external_id)
external_id, invoice_dict = sr_adapter.validate(
self.binding_id.external_id
)
InvoiceBinding = self.env["emc.binding.account.invoice"] InvoiceBinding = self.env["emc.binding.account.invoice"]
InvoiceBinding.create( InvoiceBinding.create(
{ {
"backend_id": backend.id, "backend_id": backend.id,
"external_id": invoice_dict["id"],
"external_id": external_id,
"internal_id": invoice.id, "internal_id": invoice.id,
} }
) )

Loading…
Cancel
Save