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.

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