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.

73 lines
2.8 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 psycopg2.extensions import AsIs
  5. from odoo import models, fields, api
  6. from odoo.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. display_name = fields.Char(
  12. u"Name", compute="_compute_display_name", store=True)
  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.depends('create_date', 'name')
  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.multi
  32. def name_get(self):
  33. return [(request.id, request.display_name) for request in self]
  34. @api.model
  35. def current_http_request(self):
  36. """Create a log corresponding to the current HTTP request, and returns
  37. its ID. This method can be called several times during the
  38. HTTP query/response cycle, it will only log the request on the
  39. first call.
  40. If no HTTP request is available, returns `False`.
  41. """
  42. if not request:
  43. return False
  44. http_session_model = self.env['auditlog.http.session']
  45. httprequest = request.httprequest
  46. if httprequest:
  47. if hasattr(httprequest, 'auditlog_http_request_id'):
  48. # Verify existence. Could have been rolled back after a
  49. # concurrency error
  50. self.env.cr.execute(
  51. "SELECT id FROM %s WHERE id = %s", (
  52. AsIs(self._table),
  53. httprequest.auditlog_http_request_id))
  54. if self.env.cr.fetchone():
  55. return httprequest.auditlog_http_request_id
  56. vals = {
  57. 'name': httprequest.path,
  58. 'root_url': httprequest.url_root,
  59. 'user_id': request.uid,
  60. 'http_session_id': http_session_model.current_http_session(),
  61. 'user_context': request.context,
  62. }
  63. httprequest.auditlog_http_request_id = self.create(vals).id
  64. return httprequest.auditlog_http_request_id
  65. return False