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.

33 lines
930 B

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import uuid
  5. from odoo import api, fields, models
  6. class AuthApiKey(models.Model):
  7. _inherit = "auth.api.key"
  8. def _default_key(self):
  9. return uuid.uuid4()
  10. # overloaded fields
  11. # required is set to false to allow for a computed field,
  12. # it will always be set.
  13. name = fields.Char(required=False, compute="_compute_name", store=True)
  14. key = fields.Char(default=_default_key)
  15. @api.multi
  16. @api.depends("user_id")
  17. def _compute_name(self):
  18. for key in self:
  19. if key.user_id:
  20. now = fields.Datetime.now()
  21. key.name = "{login}-{now}".format(
  22. now=fields.Datetime.to_string(now), login=key.user_id.login
  23. )
  24. else:
  25. key.name = "no-user"