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.

110 lines
3.4 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 SubscriptionRequestAdapter:
  10. _model = "subscription.request"
  11. _root = "api"
  12. _service = "subscription-request"
  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 create(self):
  21. # pylint: disable=method-required-super
  22. raise NotImplementedError
  23. def search(self, date_from=None, date_to=None):
  24. url = self.get_url([])
  25. params = {}
  26. if date_from:
  27. params.update({"date_from": Date.to_string(date_from)})
  28. if date_to:
  29. params.update({"date_to": Date.to_string(date_to)})
  30. sr_list = self.backend.http_get_content(url, params=params)
  31. return sr_list
  32. def read(self, id_):
  33. # pylint: disable=method-required-super
  34. url = self.get_url([str(id_)])
  35. sr = self.backend.http_get_content(url)
  36. return sr
  37. def update(self):
  38. raise NotImplementedError
  39. def delete(self):
  40. raise NotImplementedError
  41. def validate(self, id_):
  42. url = self.get_url([str(id_), "validate"])
  43. data = {}
  44. try:
  45. invoice_dict = self.backend.http_post_content(url, data)
  46. except BadRequest:
  47. raise ValidationError(
  48. _(
  49. "The request was already validated on the "
  50. "platform. Please check data consistency "
  51. "with your system administrator."
  52. )
  53. )
  54. return invoice_dict
  55. def to_write_values(self, request):
  56. """
  57. :return a writable dictionary of values from the dictionary
  58. received from the api
  59. """
  60. Country = self.backend.env["res.country"]
  61. ProductTemplateBinding = self.backend.env[
  62. "emc.binding.product.template"
  63. ]
  64. address = request["address"]
  65. country = Country.search([("code", "=", address["country"])])
  66. external_product_id = request["share_product"]["id"]
  67. share_product_binding = ProductTemplateBinding.search_binding(
  68. self.backend, external_product_id
  69. )
  70. if not share_product_binding:
  71. raise UserError(
  72. _(
  73. "No binding exists for share product %s. Please contact "
  74. "system administrator "
  75. )
  76. % request["share_product"]["name"]
  77. )
  78. product_product = share_product_binding.internal_id.product_variant_id
  79. return {
  80. "email": request["email"],
  81. "name": request["name"],
  82. "date": request["date"],
  83. "state": request["state"],
  84. "lang": request["lang"],
  85. "ordered_parts": request["ordered_parts"],
  86. "address": address["street"],
  87. "zip_code": address["zip_code"],
  88. "city": address["city"],
  89. "country_id": country.id,
  90. "share_product_id": product_product.id,
  91. "source": "emc_api",
  92. }