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.

193 lines
6.0 KiB

  1. # Copyright 2019 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. # pylint: disable=consider-merging-classes-inherited
  5. import logging
  6. from werkzeug.exceptions import BadRequest, NotFound
  7. from odoo import _
  8. from odoo.fields import Date
  9. from odoo.addons.base_rest.http import wrapJsonException
  10. from odoo.addons.component.core import Component
  11. from . import schemas
  12. _logger = logging.getLogger(__name__)
  13. class SubscriptionRequestService(Component):
  14. _inherit = "base.rest.service"
  15. _name = "subscription.request.services"
  16. _usage = "subscription-request"
  17. _collection = "emc.services"
  18. _description = """
  19. Subscription requests
  20. """
  21. def get(self, _id):
  22. sr = self.env["subscription.request"].search(
  23. [("external_id", "=", _id)]
  24. )
  25. if sr:
  26. return self._to_dict(sr)
  27. else:
  28. raise wrapJsonException(
  29. NotFound(_("No subscription request for id %s") % _id)
  30. )
  31. def search(self, date_from=None, date_to=None):
  32. _logger.info("search from {} to {}".format(date_from, date_to))
  33. domain = []
  34. if date_from:
  35. date_from = Date.from_string(date_from)
  36. domain.append(("date", ">=", date_from))
  37. if date_to:
  38. date_to = Date.from_string(date_to)
  39. domain.append(("date", "<=", date_to))
  40. requests = self.env["subscription.request"].search(domain)
  41. response = {
  42. "count": len(requests),
  43. "rows": [self._to_dict(sr) for sr in requests],
  44. }
  45. return response
  46. def create(self, **params): # pylint: disable=method-required-super
  47. params = self._prepare_create(params)
  48. sr = self.env["subscription.request"].create(params)
  49. return self._to_dict(sr)
  50. def update(self, _id, **params):
  51. params = self._prepare_update(params)
  52. sr = self.env["subscription.request"].search(
  53. [("external_id", "=", _id)]
  54. )
  55. if not sr:
  56. raise wrapJsonException(
  57. NotFound(_("No subscription request for id %s") % _id)
  58. )
  59. sr.write(params)
  60. return self._to_dict(sr)
  61. def _to_dict(self, sr):
  62. sr.ensure_one()
  63. return {
  64. "id": sr.get_external_id(),
  65. "name": sr.name,
  66. "email": sr.email,
  67. "state": sr.state,
  68. "date": Date.to_string(sr.date),
  69. "ordered_parts": sr.ordered_parts,
  70. "share_product": {
  71. "id": sr.share_product_id.product_tmpl_id.id,
  72. "name": sr.share_product_id.product_tmpl_id.name,
  73. },
  74. "address": {
  75. "street": sr.address,
  76. "zip_code": sr.zip_code,
  77. "city": sr.city,
  78. "country": sr.country_id.code,
  79. },
  80. "lang": sr.lang,
  81. }
  82. def _get_country(self, code):
  83. country = self.env["res.country"].search([("code", "=", code)])
  84. if country:
  85. return country
  86. else:
  87. raise wrapJsonException(
  88. BadRequest(_("No country for isocode %s") % code)
  89. )
  90. def _get_share_product(self, template_id):
  91. product = self.env["product.product"].search(
  92. [("product_tmpl_id", "=", template_id)]
  93. )
  94. if product:
  95. return product
  96. else:
  97. raise wrapJsonException(
  98. BadRequest(_("No share for id %s") % template_id)
  99. )
  100. def _prepare_create(self, params):
  101. """Prepare a writable dictionary of values"""
  102. address = params["address"]
  103. country = self._get_country(address["country"])
  104. share_product_id = self._get_share_product(params["share_product"])
  105. return {
  106. "name": params["name"],
  107. "email": params["email"],
  108. "ordered_parts": params["ordered_parts"],
  109. "share_product_id": share_product_id.id,
  110. "address": address["street"],
  111. "zip_code": address["zip_code"],
  112. "city": address["city"],
  113. "country_id": country.id,
  114. "lang": params["lang"],
  115. }
  116. def _prepare_update(self, params):
  117. if "address" in params:
  118. address = params["address"]
  119. if "country" in address:
  120. country = self._get_country(address["country"]).id
  121. address["country"] = country.id
  122. else:
  123. address = {}
  124. if "share_product" in params:
  125. share_product_id = self._get_share_product(
  126. params["share_product"]
  127. ).id
  128. else:
  129. share_product_id = None
  130. params = {
  131. "name": params.get("name"),
  132. "email": params.get("email"),
  133. "state": params.get("state"),
  134. "ordered_parts": params.get("ordered_parts"),
  135. "share_product_id": share_product_id,
  136. "address": address.get("street"),
  137. "zip_code": address.get("zip_code"),
  138. "city": address.get("city"),
  139. "country_id": address.get("country"),
  140. "lang": params.get("lang"),
  141. }
  142. params = {k: v for k, v in params.items() if v is not None}
  143. return params
  144. def _validator_get(self):
  145. return schemas.S_SUBSCRIPTION_REQUEST_GET
  146. def _validator_return_get(self):
  147. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  148. def _validator_search(self):
  149. return schemas.S_SUBSCRIPTION_REQUEST_SEARCH
  150. def _validator_return_search(self):
  151. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_SEARCH
  152. def _validator_create(self):
  153. return schemas.S_SUBSCRIPTION_REQUEST_CREATE
  154. def _validator_return_create(self):
  155. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  156. def _validator_update(self):
  157. return schemas.S_SUBSCRIPTION_REQUEST_UPDATE
  158. def _validator_return_update(self):
  159. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET