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.

131 lines
3.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. 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 _to_dict(self, sr):
  21. sr.ensure_one()
  22. return {
  23. "id": sr.id,
  24. "name": sr.name,
  25. "email": sr.email,
  26. "date": Date.to_string(sr.date),
  27. "ordered_parts": sr.ordered_parts,
  28. "share_product": {
  29. "id": sr.share_product_id.id,
  30. "name": sr.share_product_id.name,
  31. },
  32. "address": {
  33. "street": sr.address,
  34. "zip_code": sr.zip_code,
  35. "city": sr.city,
  36. "country": sr.country_id.code,
  37. },
  38. "lang": sr.lang,
  39. }
  40. def _get_country(self, code):
  41. country = self.env["res.country"].search([("code", "=", code)])
  42. if country:
  43. return country
  44. else:
  45. raise wrapJsonException(
  46. BadRequest(_("No country for isocode %s") % code)
  47. )
  48. def _prepare_create(self, params):
  49. address = params["address"]
  50. country = self._get_country(address["country"])
  51. return {
  52. "name": params["name"],
  53. "email": params["email"],
  54. "ordered_parts": params["ordered_parts"],
  55. "share_product_id": params["share_product"],
  56. "address": address["street"],
  57. "zip_code": address["zip_code"],
  58. "city": address["city"],
  59. "country_id": country.id,
  60. "lang": params["lang"],
  61. }
  62. def get(self, _id):
  63. # fixme remove sudo
  64. sr = self.env["subscription.request"].sudo().search([("id", "=", _id)])
  65. if sr:
  66. return self._to_dict(sr)
  67. else:
  68. raise wrapJsonException(
  69. NotFound(_("No subscription request for id %s") % _id)
  70. )
  71. def search(self, date_from=None, date_to=None):
  72. # fixme remove sudo
  73. _logger.info("search from %s to %s" % (date_from, date_to))
  74. domain = []
  75. if date_from:
  76. date_from = Date.from_string(date_from)
  77. domain.append(("date", ">=", date_from))
  78. if date_to:
  79. date_to = Date.from_string(date_to)
  80. domain.append(("date", "<=", date_to))
  81. requests = self.env["subscription.request"].sudo().search(domain)
  82. response = {
  83. "count": len(requests),
  84. "rows": [self._to_dict(sr) for sr in requests],
  85. }
  86. return response
  87. def create(self, **params):
  88. params = self._prepare_create(params)
  89. sr = self.env["subscription.request"].create(params)
  90. return self._to_dict(sr)
  91. def _validator_get(self):
  92. return {"_id": {"type": "integer"}}
  93. def _validator_return_get(self):
  94. return schemas.S_SUBSCRIPTION_REQUEST_GET
  95. def _validator_search(self):
  96. return {
  97. "date_from": {
  98. "type": "string",
  99. "check_with": schemas.date_validator,
  100. },
  101. "date_to": {
  102. "type": "string",
  103. "check_with": schemas.date_validator,
  104. },
  105. }
  106. def _validator_return_search(self):
  107. return schemas.S_SUBSCRIPTION_REQUEST_LIST
  108. def _validator_create(self):
  109. return schemas.S_SUBSCRIPTION_REQUEST_CREATE
  110. def _validator_return_create(self):
  111. return schemas.S_SUBSCRIPTION_REQUEST_GET