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.

71 lines
2.1 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 contextlib import contextmanager
  5. from odoo import api, fields, models, _
  6. from odoo.exceptions import UserError
  7. class ExternalSystemAdapter(models.AbstractModel):
  8. """This is the model that should be inherited for new external systems.
  9. Methods provided are prefixed with ``external_`` in order to keep from
  10. """
  11. _name = 'external.system.adapter'
  12. _description = 'External System Adapter'
  13. _inherits = {'external.system': 'system_id'}
  14. system_id = fields.Many2one(
  15. string='System',
  16. comodel_name='external.system',
  17. required=True,
  18. ondelete='cascade',
  19. )
  20. @api.multi
  21. @contextmanager
  22. def client(self):
  23. """Client object usable as a context manager to include destruction.
  24. Yields the result from ``external_get_client``, then calls
  25. ``external_destroy_client`` to cleanup the client.
  26. Yields:
  27. mixed: An object representing the client connection to the remote
  28. system.
  29. """
  30. client = self.external_get_client()
  31. try:
  32. yield client
  33. finally:
  34. self.external_destroy_client(client)
  35. @api.multi
  36. def external_get_client(self):
  37. """Return a usable client representing the remote system."""
  38. self.ensure_one()
  39. @api.multi
  40. def external_destroy_client(self, client):
  41. """Perform any logic necessary to destroy the client connection.
  42. Args:
  43. client (mixed): The client that was returned by
  44. ``external_get_client``.
  45. """
  46. self.ensure_one()
  47. @api.multi
  48. def external_test_connection(self):
  49. """Adapters should override this method, then call super if valid.
  50. If the connection is invalid, adapters should raise an instance of
  51. ``odoo.ValidationError``.
  52. Raises:
  53. odoo.exceptions.UserError: In the event of a good connection.
  54. """
  55. raise UserError(_('The connection was a success.'))