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.

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