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.

84 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from odoo import api, fields, models
  5. class AddressValidate(models.Model):
  6. """Child modules should inherit this model to create new validators.
  7. In the ``_get_interface_types`` method, new interfaces should add
  8. themselves::
  9. @api.model
  10. def _get_interface_types(self):
  11. res = super()._get_interface_types()
  12. return res + [('easypost', 'EasyPost')]
  13. The following methods must be implemented for each adapter, where
  14. ``<name>`` is equal to the interface name (``easypost`` in above):
  15. * ``<name>_get_client``: Return a usable API client for the
  16. validator.
  17. * ``<name>_test_connector``: Test the connection to the API, and
  18. escalate to super if success.
  19. * ``<name>_get_address``: Return the suggested address for a
  20. partner using the API. It should accept an authenticated API
  21. client as the first parameter and a ``res.partner`` singleton
  22. as the second.
  23. If the remote system does not disconnect automatically, destruction can
  24. be implemented in:
  25. * ``<name>_destroy_client``: Destroy the connection. It should accept
  26. an instance of the client as returned by ``<name>_get_client``.
  27. """
  28. _name = 'address.validate'
  29. _description = 'Address Validator Interface'
  30. _inherit = 'external.system.adapter'
  31. interface_type = fields.Selection(
  32. selection='_get_interface_types',
  33. required=True,
  34. )
  35. @api.model
  36. def _get_interface_types(self):
  37. """Child modules should add themselves to these selection values."""
  38. return []
  39. @api.multi
  40. def get_address(self, partner):
  41. """Returns an address suggestion from the interface.
  42. Args:
  43. partner (ResPartner): Partner record to get address for.
  44. Returns:
  45. dict: The suggested address. Should contain keys: ``street``,
  46. ``street2``, ``city``, ``state_id``, ``zip``, ``country_id``,
  47. ``is_valid``, and ``validation_messages``.
  48. """
  49. self.ensure_one()
  50. with self.interface.client() as client:
  51. return self.__interface_method('get_address')(client, partner)
  52. @api.multi
  53. def external_get_client(self):
  54. return self.__interface_method('get_client')()
  55. @api.multi
  56. def external_destroy_client(self, client):
  57. try:
  58. return self.__interface_method('destroy_client')(client)
  59. except AttributeError:
  60. pass
  61. @api.multi
  62. def external_test_connector(self):
  63. return self.__interface_method('test_connector')()
  64. def __interface_method(self, method_name):
  65. return getattr(self, '%s_%s' % (self.interface_type, method_name))