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.

74 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Akretion Sebastien Beau
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import models, fields
  5. from odoo.tools.config import config
  6. class KeychainBackend(models.AbstractModel):
  7. _name = 'keychain.backend'
  8. _backend_name = None
  9. name = fields.Char(required=True)
  10. password = fields.Char(
  11. compute="_compute_password",
  12. inverse="_inverse_password",
  13. required=True)
  14. data = fields.Serialized(
  15. compute="_compute_keychain",
  16. inverse="_inverse_keychain")
  17. def _get_technical_name(self):
  18. return '%s,%s' % (self._name, self.id)
  19. def _get_existing_keychain(self):
  20. self.ensure_one()
  21. return self.env['keychain.account'].retrieve([
  22. ('namespace', '=', self._backend_name),
  23. ('technical_name', '=', self._get_technical_name())
  24. ])
  25. def _prepare_keychain(self):
  26. env = config.get('running_env')
  27. return {
  28. 'name': "%s %s" % (self.name, env),
  29. 'technical_name': self._get_technical_name(),
  30. 'namespace': self._backend_name,
  31. 'environment': env,
  32. }
  33. def _get_keychain_account(self):
  34. self.ensure_one()
  35. account = self._get_existing_keychain()
  36. if not account:
  37. vals = self._prepare_keychain()
  38. account = self.env['keychain.account'].create(vals)
  39. return account
  40. def _inverse_password(self):
  41. for record in self:
  42. account = self._get_keychain_account()
  43. if record.password and record.password != '******':
  44. account.clear_password = record.password
  45. def _compute_password(self):
  46. for record in self:
  47. account = record._get_existing_keychain()
  48. if account and account.password:
  49. record.password = "******"
  50. else:
  51. record.password = ""
  52. def _inverse_keychain(self):
  53. for record in self:
  54. account = record._get_keychain_account()
  55. account.data = account._serialize_data(record.data)
  56. def _compute_keychain(self):
  57. for record in self:
  58. account = record._get_existing_keychain()
  59. if account:
  60. record.data = account.get_data()
  61. else:
  62. record.data = {}