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.

59 lines
2.2 KiB

7 years ago
8 years ago
  1. # Copyright 2015 ABF OSIELL <https://osiell.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import models, fields, api
  4. from odoo.http import request
  5. class AuditlogtHTTPSession(models.Model):
  6. _name = 'auditlog.http.session'
  7. _description = "Auditlog - HTTP User session log"
  8. _order = "create_date DESC"
  9. display_name = fields.Char(
  10. "Name", compute="_compute_display_name", store=True)
  11. name = fields.Char("Session ID", index=True)
  12. user_id = fields.Many2one(
  13. 'res.users', string="User", index=True)
  14. http_request_ids = fields.One2many(
  15. 'auditlog.http.request', 'http_session_id', string="HTTP Requests")
  16. @api.depends('create_date', 'user_id')
  17. def _compute_display_name(self):
  18. for httpsession in self:
  19. create_date = fields.Datetime.from_string(httpsession.create_date)
  20. tz_create_date = fields.Datetime.context_timestamp(
  21. httpsession, create_date)
  22. httpsession.display_name = "%s (%s)" % (
  23. httpsession.user_id and httpsession.user_id.name or '?',
  24. fields.Datetime.to_string(tz_create_date))
  25. @api.multi
  26. def name_get(self):
  27. return [(session.id, session.display_name) for session in self]
  28. @api.model
  29. def current_http_session(self):
  30. """Create a log corresponding to the current HTTP user session, and
  31. returns its ID. This method can be called several times during the
  32. HTTP query/response cycle, it will only log the user session on the
  33. first call.
  34. If no HTTP user session is available, returns `False`.
  35. """
  36. if not request:
  37. return False
  38. httpsession = request.session
  39. if httpsession:
  40. existing_session = self.search(
  41. [('name', '=', httpsession.sid),
  42. ('user_id', '=', request.uid)],
  43. limit=1)
  44. if existing_session:
  45. return existing_session.id
  46. vals = {
  47. 'name': httpsession.sid,
  48. 'user_id': request.uid,
  49. }
  50. httpsession.auditlog_http_session_id = self.create(vals).id
  51. return httpsession.auditlog_http_session_id
  52. return False