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.3 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 AuditlogHTTPRequest(models.Model):
  7. _name = 'auditlog.http.request'
  8. _description = u"Auditlog - HTTP request log"
  9. _order = "create_date DESC"
  10. _rec_name = 'display_name'
  11. display_name = fields.Char(u"Name", compute="_display_name")
  12. name = fields.Char(u"Path")
  13. root_url = fields.Char(u"Root URL")
  14. user_id = fields.Many2one(
  15. 'res.users', string=u"User")
  16. http_session_id = fields.Many2one(
  17. 'auditlog.http.session', string=u"Session")
  18. user_context = fields.Char(u"Context")
  19. log_ids = fields.One2many(
  20. 'auditlog.log', 'http_request_id', string=u"Logs")
  21. @api.multi
  22. def _display_name(self):
  23. for httprequest in self:
  24. create_date = fields.Datetime.from_string(httprequest.create_date)
  25. tz_create_date = fields.Datetime.context_timestamp(
  26. httprequest, create_date)
  27. httprequest.display_name = u"%s (%s)" % (
  28. httprequest.name or '?',
  29. fields.Datetime.to_string(tz_create_date))
  30. @api.model
  31. def current_http_request(self):
  32. """Create a log corresponding to the current HTTP request, and returns
  33. its ID. This method can be called several times during the
  34. HTTP query/response cycle, it will only log the request on the
  35. first call.
  36. If no HTTP request is available, returns `False`.
  37. """
  38. if not request:
  39. return False
  40. http_session_model = self.env['auditlog.http.session']
  41. httprequest = request.httprequest
  42. if httprequest:
  43. if hasattr(httprequest, 'auditlog_http_request_id'):
  44. return httprequest.auditlog_http_request_id
  45. vals = {
  46. 'name': httprequest.path,
  47. 'root_url': httprequest.url_root,
  48. 'user_id': request.uid,
  49. 'http_session_id': http_session_model.current_http_session(),
  50. 'user_context': request.context,
  51. }
  52. httprequest.auditlog_http_request_id = self.create(vals).id
  53. return httprequest.auditlog_http_request_id
  54. return False