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.

72 lines
2.8 KiB

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