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.

60 lines
2.2 KiB

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