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.

49 lines
1.4 KiB

  1. # Copyright 2018 Creu Blanca
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import api, models, fields
  4. import socket
  5. import logging
  6. class ResRemote(models.Model):
  7. _name = 'res.remote'
  8. _description = 'Remotes'
  9. name = fields.Char(
  10. required=True,
  11. string='Hostname',
  12. index=True,
  13. readonly=True
  14. )
  15. ip = fields.Char(required=True)
  16. in_network = fields.Boolean(
  17. required=True,
  18. help='Shows if the remote can be found through the socket'
  19. )
  20. _sql_constraints = [
  21. ('name_unique', 'unique(name)', 'Hostname must be unique')
  22. ]
  23. @api.model
  24. def _create_vals(self, addr, hostname):
  25. return {
  26. 'name': hostname or addr,
  27. 'ip': addr,
  28. 'in_network': bool(hostname),
  29. }
  30. @api.model
  31. def _get_remote(self, addr):
  32. try:
  33. hostname, alias, ips = socket.gethostbyaddr(addr)
  34. except socket.herror:
  35. logging.warning('Remote with ip %s could not be found' % addr)
  36. hostname = False
  37. remote = self.search([('name', '=ilike', hostname or addr)])
  38. if not remote:
  39. remote = self.create(self._create_vals(addr, hostname))
  40. if remote.ip != addr:
  41. # IPs can change through time, but hostname should not change
  42. remote.write({'ip': addr})
  43. return remote