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.

125 lines
4.2 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 ValidationError
  7. class ExternalSystem(models.Model):
  8. _name = 'external.system'
  9. _description = 'External System'
  10. name = fields.Char(
  11. required=True,
  12. help='This is the canonical (humanized) name for the system.',
  13. )
  14. host = fields.Char(
  15. help='This is the domain or IP address that the system can be reached '
  16. 'at.',
  17. )
  18. port = fields.Integer(
  19. help='This is the port number that the system is listening on.',
  20. )
  21. username = fields.Char(
  22. help='This is the username that is used for authenticating to this '
  23. 'system, if applicable.',
  24. )
  25. password = fields.Char(
  26. help='This is the password that is used for authenticating to this '
  27. 'system, if applicable.',
  28. )
  29. private_key = fields.Text(
  30. help='This is the private key that is used for authenticating to '
  31. 'this system, if applicable.',
  32. )
  33. private_key_password = fields.Text(
  34. help='This is the password to unlock the private key that was '
  35. 'provided for this sytem.',
  36. )
  37. fingerprint = fields.Text(
  38. help='This is the fingerprint that is advertised by this system in '
  39. 'order to validate its identity.',
  40. )
  41. ignore_fingerprint = fields.Boolean(
  42. default=True,
  43. help='Set this to `True` in order to ignore an invalid/unknown '
  44. 'fingerprint from the system.',
  45. )
  46. remote_path = fields.Char(
  47. help='Restrict to this directory path on the remote, if applicable.',
  48. )
  49. company_ids = fields.Many2many(
  50. string='Companies',
  51. comodel_name='res.company',
  52. required=True,
  53. default=lambda s: [(6, 0, s.env.user.company_id.ids)],
  54. help='Access to this system is restricted to these companies.',
  55. )
  56. system_type = fields.Selection(
  57. selection='_get_system_types',
  58. required=True,
  59. )
  60. interface = fields.Reference(
  61. selection='_get_system_types',
  62. readonly=True,
  63. help='This is the interface that this system represents. It is '
  64. 'created automatically upon creation of the external system.',
  65. )
  66. _sql_constraints = [
  67. ('name_uniq', 'UNIQUE(name)', 'Connection name must be unique.'),
  68. ]
  69. @api.model
  70. def _get_system_types(self):
  71. """Return the adapter interface models that are installed."""
  72. adapter = self.env['external.system.adapter']
  73. return [
  74. (m, self.env[m]._description) for m in adapter._inherit_children
  75. ]
  76. @api.multi
  77. @api.constrains('fingerprint', 'ignore_fingerprint')
  78. def check_fingerprint_ignore_fingerprint(self):
  79. """Do not allow a blank fingerprint if not set to ignore."""
  80. for record in self:
  81. if not record.ignore_fingerprint and not record.fingerprint:
  82. raise ValidationError(_(
  83. 'Fingerprint cannot be empty when Ignore Fingerprint is '
  84. 'not checked.',
  85. ))
  86. @api.multi
  87. @contextmanager
  88. def client(self):
  89. """Client object usable as a context manager to include destruction.
  90. Yields the result from ``external_get_client``, then calls
  91. ``external_destroy_client`` to cleanup the client.
  92. Yields:
  93. mixed: An object representing the client connection to the remote
  94. system.
  95. """
  96. with self.interface.client() as client:
  97. yield client
  98. @api.model
  99. def create(self, vals):
  100. """Create the interface for the record and assign to ``interface``."""
  101. record = super(ExternalSystem, self).create(vals)
  102. interface = self.env[vals['system_type']].create({
  103. 'system_id': record.id,
  104. })
  105. record.interface = interface
  106. return record
  107. @api.multi
  108. def action_test_connection(self):
  109. """Test the connection to the external system."""
  110. self.ensure_one()
  111. self.interface.external_test_connection()