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.

93 lines
2.8 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 odoo import _
  6. from odoo.exceptions import UserError
  7. from odoo.fields import Date
  8. class SubscriptionRequestAdapter:
  9. _model = "subscription.request"
  10. _root = "api"
  11. _service = "subscription-request"
  12. def __init__(self, backend):
  13. self.backend = backend
  14. def get_url(self, args):
  15. """args is a list of path elements
  16. :return the complete route to the service
  17. """
  18. return join("/", self._root, self._service, *args)
  19. def create(self):
  20. # pylint: disable=method-required-super
  21. raise NotImplementedError
  22. def search(self, date_from=None, date_to=None):
  23. url = self.get_url([])
  24. params = {}
  25. if date_from:
  26. params.update({"date_from": Date.to_string(date_from)})
  27. if date_to:
  28. params.update({"date_to": Date.to_string(date_to)})
  29. sr_list = self.backend.http_get_content(url, params=params)
  30. return sr_list
  31. def read(self, id_):
  32. # pylint: disable=method-required-super
  33. url = self.get_url([str(id_)])
  34. sr = self.backend.http_get_content(url)
  35. return sr
  36. def update(self):
  37. raise NotImplementedError
  38. def delete(self):
  39. raise NotImplementedError
  40. def to_write_values(self, request):
  41. """
  42. :return a writable dictionary of values from the dictionary
  43. received from the api
  44. """
  45. Country = self.backend.env["res.country"]
  46. ProductTemplateBinding = self.backend.env[
  47. "emc.binding.product.template"
  48. ]
  49. address = request["address"]
  50. country = Country.search([("code", "=", address["country"])])
  51. external_product_id = request["share_product"]["id"]
  52. share_product_binding = ProductTemplateBinding.search_binding(
  53. self.backend, external_product_id
  54. )
  55. if not share_product_binding:
  56. raise UserError(
  57. _(
  58. "No binding exists for share product %s. Please contact "
  59. "system administrator "
  60. )
  61. % request["share_product"]["name"]
  62. )
  63. product_product = share_product_binding.internal_id.product_variant_id
  64. return {
  65. "email": request["email"],
  66. "name": request["name"],
  67. "date": request["date"],
  68. "state": request["state"],
  69. "lang": request["lang"],
  70. "ordered_parts": request["ordered_parts"],
  71. "address": address["street"],
  72. "zip_code": address["zip_code"],
  73. "city": address["city"],
  74. "country_id": country.id,
  75. "share_product_id": product_product.id,
  76. "source": "emc_api",
  77. }