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.

56 lines
2.1 KiB

  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 openerp import models, fields, api
  5. from openerp.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. _rec_name = 'display_name'
  11. display_name = fields.Char(u"Name", compute="_compute_display_name")
  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.multi
  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.model
  27. def current_http_session(self):
  28. """Create a log corresponding to the current HTTP user session, and
  29. returns its ID. This method can be called several times during the
  30. HTTP query/response cycle, it will only log the user session on the
  31. first call.
  32. If no HTTP user session is available, returns `False`.
  33. """
  34. if not request:
  35. return False
  36. httpsession = request.httpsession
  37. if httpsession:
  38. existing_session = self.search(
  39. [('name', '=', httpsession.sid),
  40. ('user_id', '=', request.uid)],
  41. limit=1)
  42. if existing_session:
  43. return existing_session.id
  44. vals = {
  45. 'name': httpsession.sid,
  46. 'user_id': request.uid,
  47. }
  48. httpsession.auditlog_http_session_id = self.create(vals).id
  49. return httpsession.auditlog_http_session_id
  50. return False