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.

222 lines
6.9 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 = "emc.rest.service"
  15. _name = "subscription.request.services"
  16. _usage = "subscription-request"
  17. _description = """
  18. Subscription Request Services
  19. """
  20. def get(self, _id):
  21. sr = self.env["subscription.request"].search(
  22. [("_api_external_id", "=", _id)]
  23. )
  24. if sr:
  25. return self._to_dict(sr)
  26. else:
  27. raise wrapJsonException(
  28. NotFound(_("No subscription request for id %s") % _id)
  29. )
  30. def search(self, date_from=None, date_to=None):
  31. _logger.info("search from {} to {}".format(date_from, date_to))
  32. domain = []
  33. if date_from:
  34. date_from = Date.from_string(date_from)
  35. domain.append(("date", ">=", date_from))
  36. if date_to:
  37. date_to = Date.from_string(date_to)
  38. domain.append(("date", "<=", date_to))
  39. requests = self.env["subscription.request"].search(domain)
  40. response = {
  41. "count": len(requests),
  42. "rows": [self._to_dict(sr) for sr in requests],
  43. }
  44. return response
  45. def create(self, **params): # pylint: disable=method-required-super
  46. params = self._prepare_create(params)
  47. sr = self.env["subscription.request"].create(params)
  48. return self._to_dict(sr)
  49. def update(self, _id, **params):
  50. params = self._prepare_update(params)
  51. sr = self.env["subscription.request"].search(
  52. [("_api_external_id", "=", _id)]
  53. )
  54. if not sr:
  55. raise wrapJsonException(
  56. NotFound(_("No subscription request for id %s") % _id)
  57. )
  58. sr.write(params)
  59. return self._to_dict(sr)
  60. def validate(self, _id, **params):
  61. sr = self.env["subscription.request"].search(
  62. [("_api_external_id", "=", _id)]
  63. )
  64. if not sr:
  65. raise wrapJsonException(
  66. NotFound(_("No subscription request for id %s") % _id)
  67. )
  68. if sr.state != "draft":
  69. raise wrapJsonException(
  70. BadRequest(
  71. _("Subscription request %s is not in draft state") % _id
  72. )
  73. )
  74. sr.validate_subscription_request()
  75. return self._to_dict(sr)
  76. def _to_dict(self, sr):
  77. sr.ensure_one()
  78. if sr.capital_release_request:
  79. invoice_ids = [
  80. invoice.get_api_external_id()
  81. for invoice in sr.capital_release_request
  82. ]
  83. else:
  84. invoice_ids = []
  85. share_product = sr.share_product_id.product_tmpl_id
  86. return {
  87. "id": sr.get_api_external_id(),
  88. "name": sr.name,
  89. "email": sr.email,
  90. "state": sr.state,
  91. "date": Date.to_string(sr.date),
  92. "ordered_parts": sr.ordered_parts,
  93. "share_product": self._one_to_many_to_dict(share_product),
  94. "address": {
  95. "street": sr.address,
  96. "zip_code": sr.zip_code,
  97. "city": sr.city,
  98. "country": sr.country_id.code,
  99. },
  100. "lang": sr.lang,
  101. "capital_release_request": invoice_ids,
  102. }
  103. def _get_country(self, code):
  104. country = self.env["res.country"].search([("code", "=", code)])
  105. if country:
  106. return country
  107. else:
  108. raise wrapJsonException(
  109. BadRequest(_("No country for isocode %s") % code)
  110. )
  111. def _get_share_product(self, template_id):
  112. product = self.env["product.product"].search(
  113. [("product_tmpl_id", "=", template_id)]
  114. )
  115. if product:
  116. return product
  117. else:
  118. raise wrapJsonException(
  119. BadRequest(_("No share for id %s") % template_id)
  120. )
  121. def _prepare_create(self, params):
  122. """Prepare a writable dictionary of values"""
  123. address = params["address"]
  124. country = self._get_country(address["country"])
  125. share_product_id = self._get_share_product(params["share_product"])
  126. return {
  127. "name": params["name"],
  128. "email": params["email"],
  129. "ordered_parts": params["ordered_parts"],
  130. "share_product_id": share_product_id.id,
  131. "address": address["street"],
  132. "zip_code": address["zip_code"],
  133. "city": address["city"],
  134. "country_id": country.id,
  135. "lang": params["lang"],
  136. }
  137. def _prepare_update(self, params):
  138. if "address" in params:
  139. address = params["address"]
  140. if "country" in address:
  141. country = self._get_country(address["country"]).id
  142. address["country"] = country.id
  143. else:
  144. address = {}
  145. if "share_product" in params:
  146. share_product_id = self._get_share_product(
  147. params["share_product"]
  148. ).id
  149. else:
  150. share_product_id = None
  151. params = {
  152. "name": params.get("name"),
  153. "email": params.get("email"),
  154. "state": params.get("state"),
  155. "ordered_parts": params.get("ordered_parts"),
  156. "share_product_id": share_product_id,
  157. "address": address.get("street"),
  158. "zip_code": address.get("zip_code"),
  159. "city": address.get("city"),
  160. "country_id": address.get("country"),
  161. "lang": params.get("lang"),
  162. }
  163. params = {k: v for k, v in params.items() if v is not None}
  164. return params
  165. def _validator_get(self):
  166. return schemas.S_SUBSCRIPTION_REQUEST_GET
  167. def _validator_return_get(self):
  168. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  169. def _validator_search(self):
  170. return schemas.S_SUBSCRIPTION_REQUEST_SEARCH
  171. def _validator_return_search(self):
  172. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_SEARCH
  173. def _validator_create(self):
  174. return schemas.S_SUBSCRIPTION_REQUEST_CREATE
  175. def _validator_return_create(self):
  176. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  177. def _validator_update(self):
  178. return schemas.S_SUBSCRIPTION_REQUEST_UPDATE
  179. def _validator_return_update(self):
  180. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  181. def _validator_validate(self):
  182. return schemas.S_SUBSCRIPTION_REQUEST_VALIDATE
  183. def _validator_return_validate(self):
  184. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET