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.

167 lines
5.2 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"].browse(_id)
  23. if sr:
  24. return self._to_dict(sr)
  25. else:
  26. raise wrapJsonException(
  27. NotFound(_("No subscription request for id %s") % _id)
  28. )
  29. def search(self, date_from=None, date_to=None):
  30. _logger.info("search from {} to {}".format(date_from, date_to))
  31. domain = []
  32. if date_from:
  33. date_from = Date.from_string(date_from)
  34. domain.append(("date", ">=", date_from))
  35. if date_to:
  36. date_to = Date.from_string(date_to)
  37. domain.append(("date", "<=", date_to))
  38. requests = self.env["subscription.request"].search(domain)
  39. response = {
  40. "count": len(requests),
  41. "rows": [self._to_dict(sr) for sr in requests],
  42. }
  43. return response
  44. def create(self, **params): # pylint: disable=method-required-super
  45. params = self._prepare_create(params)
  46. sr = self.env["subscription.request"].create(params)
  47. return self._to_dict(sr)
  48. def update(self, _id, **params):
  49. params = self._prepare_update(params)
  50. sr = self.env["subscription.request"].browse(_id)
  51. if not sr:
  52. raise wrapJsonException(
  53. NotFound(_("No subscription request for id %s") % _id)
  54. )
  55. sr.write(params)
  56. return self._to_dict(sr)
  57. def _to_dict(self, sr):
  58. sr.ensure_one()
  59. return {
  60. "id": sr.id,
  61. "name": sr.name,
  62. "email": sr.email,
  63. "state": sr.state,
  64. "date": Date.to_string(sr.date),
  65. "ordered_parts": sr.ordered_parts,
  66. "share_product": {
  67. "id": sr.share_product_id.id,
  68. "name": sr.share_product_id.name,
  69. },
  70. "address": {
  71. "street": sr.address,
  72. "zip_code": sr.zip_code,
  73. "city": sr.city,
  74. "country": sr.country_id.code,
  75. },
  76. "lang": sr.lang,
  77. }
  78. def _get_country(self, code):
  79. country = self.env["res.country"].search([("code", "=", code)])
  80. if country:
  81. return country
  82. else:
  83. raise wrapJsonException(
  84. BadRequest(_("No country for isocode %s") % code)
  85. )
  86. def _prepare_create(self, params):
  87. address = params["address"]
  88. country = self._get_country(address["country"])
  89. return {
  90. "name": params["name"],
  91. "email": params["email"],
  92. "ordered_parts": params["ordered_parts"],
  93. "share_product_id": params["share_product"],
  94. "address": address["street"],
  95. "zip_code": address["zip_code"],
  96. "city": address["city"],
  97. "country_id": country.id,
  98. "lang": params["lang"],
  99. }
  100. def _prepare_update(self, params):
  101. if "address" in params:
  102. address = params["address"]
  103. if "country" in address:
  104. country = self._get_country(address["country"]).id
  105. address["country"] = country
  106. else:
  107. address = {}
  108. params = {
  109. "name": params.get("name"),
  110. "email": params.get("email"),
  111. "state": params.get("state"),
  112. "ordered_parts": params.get("ordered_parts"),
  113. "share_product_id": params.get("share_product"),
  114. "address": address.get("street"),
  115. "zip_code": address.get("zip_code"),
  116. "city": address.get("city"),
  117. "country_id": address.get("country"),
  118. "lang": params.get("lang"),
  119. }
  120. params = {k: v for k, v in params.items() if v is not None}
  121. return params
  122. def _validator_get(self):
  123. return schemas.S_SUBSCRIPTION_REQUEST_GET
  124. def _validator_return_get(self):
  125. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  126. def _validator_search(self):
  127. return schemas.S_SUBSCRIPTION_REQUEST_SEARCH
  128. def _validator_return_search(self):
  129. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_SEARCH
  130. def _validator_create(self):
  131. return schemas.S_SUBSCRIPTION_REQUEST_CREATE
  132. def _validator_return_create(self):
  133. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
  134. def _validator_update(self):
  135. return schemas.S_SUBSCRIPTION_REQUEST_UPDATE
  136. def _validator_return_update(self):
  137. return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET