You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.3 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from os.path import join
  5. from werkzeug.exceptions import BadRequest
  6. from odoo import _
  7. from odoo.exceptions import UserError, ValidationError
  8. from odoo.fields import Date
  9. class AbstractEMCAdapter:
  10. _model = "set in implementation class"
  11. _root = "api"
  12. _service = "set in implementation class"
  13. def __init__(self, backend):
  14. self.backend = backend
  15. def _get_url(self, args):
  16. """args is a list of path elements
  17. :return the complete route to the service
  18. """
  19. return join("/", self._root, self._service, *args)
  20. def search(self, **params):
  21. raise NotImplementedError
  22. def read(self, id_):
  23. # pylint: disable=method-required-super
  24. url = self._get_url([str(id_)])
  25. api_dict = self.backend.http_get_content(url)
  26. return self.to_write_values(api_dict)
  27. def create(self):
  28. # pylint: disable=method-required-super
  29. raise NotImplementedError
  30. def update(self):
  31. raise NotImplementedError
  32. def delete(self):
  33. raise NotImplementedError
  34. def to_write_values(self, api_dict):
  35. """
  36. :return a tuple with
  37. - the external id
  38. - a writable dictionary for _model
  39. received from the api
  40. """
  41. raise NotImplementedError
  42. class SubscriptionRequestAdapter(AbstractEMCAdapter):
  43. _model = "subscription.request"
  44. _service = "subscription-request"
  45. def search(self, date_from=None, date_to=None):
  46. url = self._get_url([])
  47. params = {}
  48. if date_from:
  49. params.update({"date_from": Date.to_string(date_from)})
  50. if date_to:
  51. params.update({"date_to": Date.to_string(date_to)})
  52. sr_list = self.backend.http_get_content(url, params=params)
  53. return {
  54. "count": sr_list["count"],
  55. "rows": [self.to_write_values(row) for row in sr_list["rows"]],
  56. }
  57. def validate(self, id_):
  58. url = self._get_url([str(id_), "validate"])
  59. data = {}
  60. try:
  61. invoice_dict = self.backend.http_post_content(url, data)
  62. except BadRequest:
  63. raise ValidationError(
  64. _(
  65. "The request was already validated on the "
  66. "platform. Please check data consistency "
  67. "with your system administrator."
  68. )
  69. )
  70. ai_adapter = AccountInvoiceAdapter(backend=self.backend)
  71. return ai_adapter.to_write_values(invoice_dict)
  72. def to_write_values(self, api_dict):
  73. Country = self.backend.env["res.country"]
  74. ProductTemplateBinding = self.backend.env[
  75. "emc.binding.product.template"
  76. ]
  77. address = api_dict["address"]
  78. country = Country.search([("code", "=", address["country"])])
  79. external_product_id = api_dict["share_product"]["id"]
  80. share_product_binding = ProductTemplateBinding.search_binding(
  81. self.backend, external_product_id
  82. )
  83. if not share_product_binding:
  84. raise UserError(
  85. _(
  86. "No binding exists for share product %s. Please contact "
  87. "system administrator "
  88. )
  89. % api_dict["share_product"]["name"]
  90. )
  91. product_product = share_product_binding.internal_id.product_variant_id
  92. external_id = api_dict["id"]
  93. writable_dict = {
  94. "email": api_dict["email"],
  95. "name": api_dict["name"],
  96. "date": api_dict["date"],
  97. "state": api_dict["state"],
  98. "lang": api_dict["lang"],
  99. "ordered_parts": api_dict["ordered_parts"],
  100. "address": address["street"],
  101. "zip_code": address["zip_code"],
  102. "city": address["city"],
  103. "country_id": country.id,
  104. "share_product_id": product_product.id,
  105. "source": "emc_api",
  106. }
  107. return external_id, writable_dict
  108. class AccountInvoiceAdapter(AbstractEMCAdapter):
  109. _model = "account.invoice"
  110. _service = "invoice"
  111. def to_write_values(self, api_dict):
  112. external_id = api_dict.pop("id")
  113. writable_dict = api_dict
  114. return external_id, writable_dict