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.

95 lines
2.8 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
  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(self, _id):
  41. # fixme remove sudo
  42. sr = self.env["subscription.request"].sudo().search([("id", "=", _id)])
  43. if sr:
  44. return self._to_dict(sr)
  45. else:
  46. raise wrapJsonException(
  47. NotFound(_("No subscription request for id %s") % _id)
  48. )
  49. def search(self, date_from=None, date_to=None):
  50. # fixme remove sudo
  51. _logger.info("search from %s to %s" % (date_from, date_to))
  52. domain = []
  53. if date_from:
  54. date_from = Date.from_string(date_from)
  55. domain.append(("date", ">=", date_from))
  56. if date_to:
  57. date_to = Date.from_string(date_to)
  58. domain.append(("date", "<=", date_to))
  59. requests = self.env["subscription.request"].sudo().search(domain)
  60. response = {
  61. "count": len(requests),
  62. "rows": [self._to_dict(sr) for sr in requests],
  63. }
  64. return response
  65. def _validator_get(self):
  66. return {"_id": {"type": "integer"}}
  67. def _validator_return_get(self):
  68. return schemas.S_SUBSCRIPTION_REQUEST
  69. def _validator_search(self):
  70. return {
  71. "date_from": {
  72. "type": "string",
  73. "check_with": schemas.date_validator,
  74. },
  75. "date_to": {
  76. "type": "string",
  77. "check_with": schemas.date_validator,
  78. },
  79. }
  80. def _validator_return_search(self):
  81. return schemas.S_SUBSCRIPTION_REQUEST_LIST